微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当pygame中的player_rect和door_rect接触时,我如何显示开门

如何解决当pygame中的player_rect和door_rect接触时,我如何显示开门

我想在关闭的门(门)的x和y坐标处显示打开的门图像,但是我不知道在这些情况下显示功能如何工作。抱歉,我的代码有点混乱,它是几种不同代码阶梯的混合。我的一些代码来自:https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125

代码

from pygame.locals import *
import pygame
import sys
import glob

map2 = """wwwwwwd wwwww
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
w           w
wwwwwwpwwwwww"""









pygame.display.set_icon(pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Icon.png"))
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 = [0,0]#remember its a fucking list
door_list = []
door_location = [100,-20]

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

open_door = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Open door.png").convert()

floor_tile = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/floor.png").convert()


door = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Door.png").convert()#if you dont convert it colorkey wont work
door_rect = pygame.Rect(door_location[0],door_location[1],door.get_width(),door.get_height())
door.set_colorkey((255,255,255))

wall = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Wall.png").convert()
wall_rect = wall.get_rect(center=(100,256))

player = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Player.png").convert()
player_rect = pygame.Rect(player_location[0],player_location[1],player.get_width(),player.get_height())
player.set_colorkey((255,255))

enemy = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Enemy.png").convert()
enemy_rect = enemy.get_rect(center=(100,250))
enemy.set_colorkey((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
            pygame.display(open_door)








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


def tiles(map2):
    global wall,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,y * 15))



map2 = map2.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
    screen.fill((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()
            pygame.quit()

        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)

解决方法

添加变量player_at_door并使用False对其进行初始化。当玩家在门口时,设置变量True

player_at_door = False

def check_collision(door,player):
    global player_at_door

    player_at_door = False
    for door_rect in door:
                
        if player_rect.colliderect(door_rect):
            player_at_door = True

根据door的状态绘制open_doorplayer_at_door

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":
                
                door_image = open_door if player_at_door else door # <---
                rect = screen.blit(door_image,(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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。