player
is not pygame.Rect
object but player_rect
is. Use player_rect
instead of player
for the collision test:
def check_collision(doors):
for pipe in doors:
#for pipr in pipes = checks forall the rects inside pipe list
if player_rect.colliderect(pipe): # <---
#colliderect = checks for collision
print('collision')
door_list
is an empty list and player_rect
is not in the player's position. Update player_rect
and door_list
in function tiles
. blit
returns the rectangular area of the affected pixels. Use it to update player_rect
and to fill door_list
:
def tiles(map1):
global tile, door, player, enemy, player_rect # <---
door_list.clear() # <---
for y, line in enumerate(map1):
#counts lines
for x, c in enumerate(line):
#counts caracters
if c == "w":
#caracter is w
screen.blit(tile, (x * 16.18, y * 15))
if c == "d":
rect = screen.blit(door, (x * 16.2, y * 15)) # <---
door_list.append(rect) # <---
if c == "p":
player_rect = screen.blit(player, player_location) # <---
if c == "e":
screen.blit(enemy, (x * 16, y * 15))
Don't forget to use the global statement, when you want to change a variable in the global namespace:
def tiles(map1):
global player_rect
# [...]