Aug-16-2022, 06:21 PM
(This post was last modified: Aug-16-2022, 06:25 PM by deanhystad.)
You collision detection fails because you pass playerx and playery to move() when you should pass player_speed and player_y_momentum.
move() should look like this:
You'll also want to change the code that calls move()
move() should look like this:
def move(rect, xstep, ystep, tiles): collisions = {'top': False, 'bottom': False, 'right': False, 'left': False} # Move in x and check for collisions rect.x += xstep for tile in tiles: if rect.colliderect(tile): if xstep > 0: collisions['right'] = True elif xstep < 0: collisions['left'] = True break # Move in y and check for collisions rect.x -= xstep rect.y += ystep for tile in tiles: if ystep > 0: collisions['bottom'] = True elif ystep < 0: collisions['top'] = True break return collisionsYou weren't using the returned rect value, so don't return it. After checking for x collisions you want to move rect back (rect.x -= xstep) otherwise an x collision is also reported as a y collision.
You'll also want to change the code that calls move()
playery += player_y_momentum player_y_momentum = min(3, player_y_momentum + 0.2) collisions_type = move(playerrect, player_speed, player_y_momentum , tile_rects)