Skip to content Skip to sidebar Skip to footer

Why Are My Rects Not Working And Making The Player Interact With The Door As It Is Supposed To In Pygame

I am trying to make the player sprite act when he touches the door but it is not working. I am trying to make it print the word collision but it is not appearing.Most of the code c

Solution 1:

player is not pygame.Rect object but player_rect is. Use player_rect instead of player for the collision test:

defcheck_collision(doors):
    for pipe in doors:
        #for pipr in pipes = checks forall the rects inside pipe listif player_rect.colliderect(pipe):                           # <---#colliderect = checks for collisionprint('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:

deftiles(map1):
    global tile, door, player, enemy, player_rect                   # <---

    door_list.clear()                                               # <---for y, line inenumerate(map1):
        #counts linesfor x, c inenumerate(line):
            #counts caractersif 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:

deftiles(map1):
    global player_rect

    # [...]

Post a Comment for "Why Are My Rects Not Working And Making The Player Interact With The Door As It Is Supposed To In Pygame"