gpt4 book ai didi

python - 按下键时pygame继续循环

转载 作者:行者123 更新时间:2023-12-04 08:07:12 24 4
gpt4 key购买 nike

我试图让我的计时器在我们点击 e 时继续添加,但我不知道为什么我必须按住 e 来让计时器继续添加我的计时器名称是(吨)有没有一种方法可以在我们继续添加我的计时器时单击 e 而不是在我们不再单击 e 时停止我尝试了“if event.type == pygame.K_e”,但它与我必须保持 e 相同

        if keys[pygame.K_e]: # if we click e then it should keep adding tons
tons += 1
print(tons)
游戏循环V
run = True
while run:
# Making game run with fps
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False


# telling what to do when we say the word 'key'
keys = pygame.key.get_pressed()


if hit:
if keys[pygame.K_e]: # if we click e then it should keep adding tons
tons += 1
print(tons)

if tons > 10:
playerman.direction = "att"
if health2 > -21:
health2 -= 0.3
else:
playerman.direction = "Idle"
hit = False

if tons > 30:
tons = 0
playerman.direction = "Idle"

最佳答案

but Im not sure why I have to hold e for the timer to keep adding up


因为这就是你编码的方式。看看你的代码:
 if keys[pygame.K_e]: # if we click e then it should keep adding tons
tons += 1
tons当且仅当按下 e 时才会增加。

is there a way I could keep adding my timer when we click e instead of stopping when we are not clicking e anymore


只需设置一个标志,如下所示:
pressed_e = False
run = True
while run:
for event in pygame.event.get():
...
if event.type == pygame.KEYDOWN and event.key == pygame.K_e:
pressed_e = True

if pressed_e: # if we click e then it should keep adding tons
tons += 1
...

关于python - 按下键时pygame继续循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66167165/

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