gpt4 book ai didi

python - 如何使用KEYDOWN?

转载 作者:行者123 更新时间:2023-12-04 11:35:16 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?

(1 个回答)



How to get keyboard input in pygame?

(10 个回答)


4 个月前关闭。




我试图在按下转义键时退出程序。我在这样做时遇到了一些麻烦。
当我取出 door() 线时,它起作用了;

while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print("key pressed")
if event.key == pygame.K_ESCAPE:
print("key pressed")
pygame.quit()
sys.exit()
#door()
这是命令提示符读数
C:\Users\Me\Documents\Fan game>python testing_file.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
key pressed
key pressed

C:\Users\Me\Documents\Fan game>
但是一旦我重新添加该行,它就会停止工作。
我的门功能;
def door():

global var_door

image_door_1 = pygame.image.load(r'textures\door\frame_1.png')
image_door_2 = pygame.image.load(r'textures\door\frame_2.png')
image_door_3 = pygame.image.load(r'textures\door\frame_3.png')
image_door_4 = pygame.image.load(r'textures\door\frame_4.png')
image_door_5 = pygame.image.load(r'textures\door\frame_5.png')
image_door_6 = pygame.image.load(r'textures\door\frame_6.png')

image_door_1_size = image_door_1.get_rect().size
image_door_2_size = image_door_2.get_rect().size
image_door_3_size = image_door_3.get_rect().size
image_door_4_size = image_door_4.get_rect().size
image_door_5_size = image_door_5.get_rect().size
image_door_6_size = image_door_6.get_rect().size

centered_image_door_1 = [(display_size[0] - image_door_1_size[0])/2, (display_size[1] - image_door_1_size[1])/2]
centered_image_door_2 = [(display_size[0] - image_door_2_size[0])/2, (display_size[1] - image_door_2_size[1])/2]
centered_image_door_3 = [(display_size[0] - image_door_3_size[0])/2, (display_size[1] - image_door_3_size[1])/2]
centered_image_door_4 = [(display_size[0] - image_door_4_size[0])/2, (display_size[1] - image_door_4_size[1])/2]
centered_image_door_5 = [(display_size[0] - image_door_5_size[0])/2, (display_size[1] - image_door_5_size[1])/2]
centered_image_door_6 = [(display_size[0] - image_door_6_size[0])/2, (display_size[1] - image_door_6_size[1])/2]

mouse_down = False

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
mouse_down = True

x, y = pygame.mouse.get_pos()
if 0 < x < 253 and 0 < y < 226:
if mouse_down:
if var_door == 0:
screen.blit(image_door_1, centered_image_door_1)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_2, centered_image_door_2)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_3, centered_image_door_3)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_4, centered_image_door_4)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_5, centered_image_door_5)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_6, centered_image_door_6)
pygame.display.update()

var_door = 1

else:
screen.blit(image_door_6, centered_image_door_6)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_5, centered_image_door_5)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_4, centered_image_door_4)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_3, centered_image_door_3)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_2, centered_image_door_2)
pygame.display.update()
time.sleep(0.01)

screen.blit(image_door_1, centered_image_door_1)
pygame.display.update()

var_door = 0
我不明白为什么它不起作用。我尝试重新排列 while 循环检查的顺序,但即使这样也不起作用。我的猜测是出于某种原因,它从不检查是否按下了退出键并卡在了门功能上,但门功能中没有任何循环。

最佳答案

当您调用时 pygame.event.get()door() ,事件队列在主循环中将为空。
一个可能的解决方案是存储从 pygame.event.get() 返回的列表。并在两个循环中使用它。

关于python - 如何使用KEYDOWN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67782206/

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