我在我的覆盆子圆周率上用python编码。Python不是我最好的语言,所以请原谅我。
我需要一个简单的代码,以响应键盘上的按键笔触。我会这样做,这样我可以设置脉宽调制,但我不需要那个代码,我已经有了。我最关心的是我很难理解pygame
我的任务所需的功能。
我希望能够键入一个键,如“向上箭头”↑,并具有程序输出。"up pressed"
每隔一毫秒,向上的箭头就会被按下。
伪代码看起来应该是:
double x = 1
while x == 1:
if input.key == K_UP:
print("Up Arrow Pressed")
if input.key == K_q
x = 2
wait 1ms
pygame.quit()
同样,由于不知道语法,我不知道要导入或调用什么。
发布于 2018-06-26 08:53:34
下面的代码将检查是否按下了↑键:
import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([320,240])
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
print("Up Arrow Pressed")
elif keys[pygame.K_q]:
done = True
clock.tick(1000)
pygame.quit()
在我的个人电脑上,我只看到大约六百帧的速率,也许你应该看一下按键,按下事件键,然后切换你的输出?
import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([320,240])
done = False
output = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
output = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
output = False
elif event.key == pygame.K_q:
done = True
pygame.display.set_caption(f"Output Status {output}")
clock.tick(60)
pygame.quit()
如果运行此操作,将看到窗口的标题在按下↑键时会发生更改。
https://stackoverflow.com/questions/-100008695
复制相似问题