Why are my rects not working and making the player interact with the door as it is supposed to in pygame [closed]

☆樱花仙子☆ 提交于 2020-11-29 03:06:31

问题


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 comes from: https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125

code:

import pygame
import sys
import glob
map1 = """wwwwwwwwwwwwwwwwwwwwwwwwwwwww
w                           w
w                           w
w                           w
w                           w
w                           
w                           d
w            p               
w                           
w                           w
w                           w
w                           w
w                           w
w                           w
w                           w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""

map2 = """wwwwwwwwwwwwwwdwwwwwwwwwwwwww
w                           w
w                           w
w                           w
w                           w
w       w            w      w
w                           w
p             e             w
w                           w
w                           w
w       w            w      w
w                           w
w                           w
w                           w
w                           w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""









pygame.display.set_icon(pygame.image.load("C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/favicon.ico"))
pygame.display.set_caption("Knock Knight")

screen = pygame.display.set_mode((480, 250))
moving_right = False
moving_left = False
moving_up = False
moving_down = False
player_location = [50,50]
door_list = []

#-----------------------------

door = pygame.image.load("C:/Users/cuerv/Downloads/Door.png").convert()
door_rect = door.get_rect(center=(100, 250))
door.set_colorkey((255, 255, 255))

tile = pygame.image.load("C:/Users/cuerv/Downloads/Wall.png").convert()
tile_rect = tile.get_rect(center=(100, 256))

player = pygame.image.load("C:/Users/cuerv/Downloads/Player.png").convert()
player_rect = player.get_rect(center=(100, 256))
player.set_colorkey((255, 255, 255))

enemy = pygame.image.load("C:/Users/cuerv/Downloads/Enemy.png").convert()
enemy_rect = enemy.get_rect(center=(100, 250))
enemy.set_colorkey((255, 255, 255))

def check_collision(door):
    for pipe in door:
        #for pipr in pipes = checks forall the rects inside pipe list
        if player.colliderect(pipe):
            #colliderect = checks for collision
            print('collision')








def init_display():
    global screen, tile, door, player, enemy


def tiles(map1):
    global tile, door, player, enemy
    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":
                screen.blit(door, (x * 16.2, y * 15))
            if c == "p":
                screen.blit(player, player_location)
            if c == "e":
                screen.blit(enemy, (x * 16, y * 15))


map1 = map1.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
    screen.fill((0,0,0))
    tiles(map1)

    if moving_right == True:
        player_location[0] += 4
    if moving_left == True:
        player_location[0] -= 4
    if moving_up == True:
        player_location[1] -=4
    if moving_down == True:
        player_location[1] +=4

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
            if event.key == K_UP:
                moving_up = True
            if event.key == K_DOWN:
                moving_down = True
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False
            if event.key == K_UP:
                moving_up = False
            if event.key == K_DOWN:
                moving_down = False


    check_collision (door_list)


    pygame.display.update()
    clock.tick(60)

回答1:


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

    # [...]


来源:https://stackoverflow.com/questions/64636714/why-are-my-rects-not-working-and-making-the-player-interact-with-the-door-as-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!