gpt4 book ai didi

python - 如何检查按钮是否按下两次?

转载 作者:行者123 更新时间:2023-12-01 00:54:14 25 4
gpt4 key购买 nike

我可以使用 Pygame 在 Python 中按 Enter 按钮。现在,每次我按下按钮时,它都会在控制台中打印“一次”。我如何检测按钮是否被按下多次并打印“多次”?

  press = False
if event.key == pygame.K_RETURN:
press = True

print("once")
if press == True:
print("more than once")

最佳答案

你就快到了。只需使用 if/else block 并在打印后将 press 设置为 True:

import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
run = True
press = False

while run:
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_RETURN:
if not press:
print('once')
else:
print('more than once')
press = True

screen.fill((30, 30, 30))
pygame.display.flip()

关于python - 如何检查按钮是否按下两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56340872/

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