python - 如何检查,如果按钮按下两次?
问题描述
我可以使用 Pygame 在 Python 中按下 Enter 按钮。现在,每次我按下按钮时,它都会在控制台中打印“一次”。如何检测按钮是否被多次按下并打印“多次”?
press = False
if event.key == pygame.K_RETURN:
press = True
print("once")
if press == True:
print("more than once")
解决方案
你快到了。只需使用if
/else
块并在打印后设置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()
推荐阅读
- string - 在互联网上获取文本文件的内容,颤动
- kubernetes - kubernetes helm 何时触发 pod 重新创建?
- geometry - 寻找点云最密集区域的质心
- javascript - 材质ui选择组件下拉定位
- logstash - Grok 无法创建可选字段
- reactjs - 子状态为真时如何将父状态设置为假
- python - Pandas DataFrame 的多循环和多拆分
- django - 由于 manage.py 文件中的错误,无法运行 django 项目
- cloudflare-workers - 牧马人工人:HTMLRewriter.on 与 ElementHandler 不工作
- python - 如何使用 tkinter 添加和移动图像?