-1

我的代码应该在玩家 rect 每次触摸门 rect 时打印冲突,但由于某种原因它会不断打印。我正在使用库:pygame、glob、sys、pygame locals。大部分代码来自这里:https ://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125

代码:

map2 = """wwwwwwd wwwww
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           p
w           w
w           w
w           w
w           w
w           w
w           w
w           w
wwwwwwpwwwwwwp"""









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((226, 318))
moving_right = False
moving_left = False
moving_up = False
moving_down = False
player_location = [50,50]#remember its a fucking list
door_list = []

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


floor_tile = pygame.image.load("C:/Users/cuerv/Downloads/floor.png").convert()
floor_rect = floor_tile.get_rect(center=(100, 250))

door = pygame.image.load("C:/Users/cuerv/Downloads/Door.png").convert()#if you dont convert it colorkey wont work
door_rect = door.get_rect(center=(100, 250))
door.set_colorkey((255, 255, 255))

wall = pygame.image.load("C:/Users/cuerv/Downloads/Wall.png").convert()
wall_rect = wall.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, player):
    for player in door:
        #for pipr in pipes = checks forall the rects inside pipe list
        if player_rect.colliderect(door_rect):
            #colliderect = checks for collision
            print('collision')








def init_display():
    global screen, wall, door, player, enemy, floor_tile


def tiles(map2):
    global wall, door, player, enemy, floor_tile

    door_list.clear()

    for y, line in enumerate(map2):
        #counts lines
        for x, c in enumerate(line):
            #counts caracters
            if c == "p":
                player_rect = screen.blit(player, player_location)
            if c == "w":
                #caracter is w
                screen.blit(wall, (x * 16.18, y * 15))
            if c == "d":
                rect = screen.blit(door, (x * 16.2, y * 15))
                door_list.append(rect)
            if c == "e":
                screen.blit(enemy, (x * 16, y * 15))
            if c == "f":
                screen.blit(floor_tile, (x * 16, y * 15))



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

    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_rect, player_rect)


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

1 回答 1

2

您错过global player_rect了功能tiles

def tiles(map2):
    global wall, door, player, enemy, floor_tile, player_rect

    door_list.clear()

    for y, line in enumerate(map2):
        #counts lines
        for x, c in enumerate(line):
            #counts caracters
            if c == "p":
                player_rect = screen.blit(player, player_location)
            if c == "w":
                #caracter is w
                screen.blit(wall, (x * 16.18, y * 15))
            if c == "d":
                rect = screen.blit(door, (x * 16.2, y * 15))
                door_list.append(rect)
            if c == "e":
                screen.blit(enemy, (x * 16, y * 15))
            if c == "f":
                screen.blit(floor_tile, (x * 16, y * 15))

当您想要更改全局命名空间中的变量时,您必须使用global语句。请参阅上一个问题的答案:为什么我的矩形不起作用并使玩家与门交互,因为它应该在 pygame

于 2020-11-02T19:23:50.030 回答