gpt4 book ai didi

python - Pygame 使用箭头键指向 "Look Around"

转载 作者:行者123 更新时间:2023-12-02 09:02:11 25 4
gpt4 key购买 nike

我目前正在尝试对一系列房间进行编码,这里称为节点。您从朝北的第一个房间/节点 (node1) 开始,然后显示北向的图像。如果按向左/向右/向下箭头键,您将面朝指南针上的相应点,并且会显示该方向的新图像。如果在第一个节点中朝北按向上箭头键,您将进入第二个节点。

但是,当我执行此代码时,任何按键都会导致我进入第二个节点。我浏览了一下,觉得使用 AND 语句应该不会出现问题,但显然我在这些循环中遗漏了一些东西。

预先感谢您的帮助...

def node1():
node1_here = True
node1_look_north = True
node1_look_south = False
node1_look_east = False
node1_look_west = False
node1_north_image = pygame.image.load('node1north.jpg')
node1_south_image = pygame.image.load('node1south.jpg')
node1_east_image = pygame.image.load('node1east.jpg')
node1_west_image = pygame.image.load('node1west.jpg')
while node1_here:
while node1_look_north:
screen.blit(node1_north_image, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN and pygame.K_UP:
node2()
elif event.type == pygame.KEYDOWN and pygame.K_DOWN:
node1_look_south = True and not node1_look_north
elif event.type == pygame.KEYDOWN and pygame.K_LEFT:
node1_look_east = True and not node1_look_north
elif event.type == pygame.KEYDOWN and pygame.K_RIGHT:
node1_look_west = True and not node1_look_north
while node1_look_south:
screen.blit(node1_south_image, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN and pygame.K_DOWN:
node1_look_north = True and not node1_look_south
elif event.type == pygame.KEYDOWN and pygame.K_LEFT:
node1_look_west = True and not node1_look_south
elif event.type == pygame.KEYDOWN and pygame.K_RIGHT:
node1_look_east = True and not node1_look_south
while node1_look_east:
screen.blit(node1_east_image, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN and pygame.K_DOWN:
node1_look_west = True and not node1_look_east
elif event.type == pygame.KEYDOWN and pygame.K_LEFT:
node1_look_south = True and not node1_look_east
elif event.type == pygame.KEYDOWN and pygame.K_RIGHT:
node1_look_north = True and not node1_look_east
while node1_look_west:
screen.blit(node1_west_image, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN and pygame.K_DOWN:
node1_look_east = True and not node1_look_west
elif event.type == pygame.KEYDOWN and pygame.K_LEFT:
node1_look_north = True and not node1_look_west
elif event.type == pygame.KEYDOWN and pygame.K_RIGHT:
node1_look_south = True and not node1_look_west

def node2():
node2_here = True
node2_look_north = True
node2_north_image = pygame.image.load('node2north.jpg')
while node2_here:
while node2_look_north:
screen.blit(node2_north_image, (0, 0))
pygame.display.update()

最佳答案

说明:

pygame.K_UP

^这是一个常数并且始终为真。因此,

elif event.type == pygame.KEYDOWN and pygame.K_UP:

^当您按任意键时,这将始终解析为 True,从而导致

node2()

被处决。

解决方案:

elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:

^这将检查按下的键是否为K_UP

希望有帮助!

引用: Pygame key docs

关于python - Pygame 使用箭头键指向 "Look Around",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59698253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com