gpt4 book ai didi

python - 如何在按键事件后将函数blit到屏幕(pygame)

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

我试图通过按空格键将屏幕从介绍屏幕切换到说明屏幕。但是,当您按空格键时,说明屏幕仅在您按下时保持打开状态

    for event in pygame.event.get():
intro_screen()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
instructions()
pygame.display.flip()

我该如何解决这个问题,让它最后出现在屏幕上?另外,我是否可以使用同一个按键来显示不同的功能?

我是 pygame 新手:)

最佳答案

代码需要维护显示的状态。目前,主循环似乎总是绘制“介绍屏幕”,并且正如您所说,仅在空格键事件上绘制“说明屏幕”。这也被放入事件处理程序代码中,它可能应该位于该部分之外。

for event in pygame.event.get():
intro_screen() # <-- Draw intro
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
instructions() # <-- Draw Instructions
pygame.display.flip()

最好将要显示的屏幕存储在变量中。

current_screen = 'introduction'              # enumerated type would be better
done = False

# main loop
while not done:
# handle events
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.KEYDOWN ):
if ( event.key == pygame.K_SPACE ):
current_screen = 'instructions' # switch screens
elif ( event.key == pygame.K_BACKSPACE ):
current_screen = 'introduction' # switch screens back

# paint the screen
if ( current_screen = 'introduction' ):
intro_screen() # draw the introduction screen
elif ( current_screen = 'instructions' ):
instructions() # draw the instructions
else:
# TODO - more screen types
pass

pygame.display.flip()

关于python - 如何在按键事件后将函数blit到屏幕(pygame),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59725462/

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