gpt4 book ai didi

python - 如何让pygame只识别一次鼠标点击?

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

我有一个简单的游戏,我希望我的鼠标点击(按钮无关紧要)每次点击只注册一次。现在即使按住它也能工作。

def drawer(x, y):
pygame.draw.circle(screen, (0, 0, 0), [int(x), int(y)],20)

def main():
running = True
while running:

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
playerY_change = -player.vy
if event.type == pygame.MOUSEBUTTONUP:
playerY_change = player.vy
player.y += PlayerY_change
drawer(player.x, player.y)

# Other game code
pygame.display.update()

现在,只要您按住按钮,我希望它仅在每次单击时更改,玩家的运动就会不断变化。我该怎么做?

最佳答案

曾经playerY_change设置后,玩家连续移动。因此不要设置playerY_change ,但更改 player.y当您用鼠标单击时:

def main():
running = True
while running:

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
player.y -= player.vy

drawer(player.x, player.y)

# Other game code
pygame.display.update()

pygame.quit()
exit()

关于python - 如何让pygame只识别一次鼠标点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67265064/

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