gpt4 book ai didi

python - 如何让游戏角色(用户) Action 流畅?

转载 作者:行者123 更新时间:2023-12-01 06:24:09 35 4
gpt4 key购买 nike

在我的游戏中,我已将角色设置为移动。它的设置方式是:

if game_over_state == False:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause = True
game_paused()
if event.key == pygame.K_LEFT:
player_x_change = -2
if event.key == pygame.K_RIGHT:
player_x_change = 2
if event.key == pygame.K_UP:
player_y_change = -2
if event.key == pygame.K_DOWN:
player_y_change = 2

if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT
player_x_change = 0
if event.type == pygame.KEYUP:
player_y_change = 0

是否可以将其更改为 while 函数或其他函数,以便在释放箭头键时角色不会停止移动(即使按住另一个箭头键)?

最佳答案

问题是 event.type == pygame.KEYUP 中的条件。

if event.type == pygame.KEYUP:
# [...]

if event.type == pygame.KEYUP:
player_y_change = 0

只需重置 player_x_changeplayer_y_change,具体取决于释放的按键:

if game_over_state == False:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause = True
game_paused()
if event.key == pygame.K_LEFT:
player_x_change = -2
if event.key == pygame.K_RIGHT:
player_x_change = 2
if event.key == pygame.K_UP:
player_y_change = -2
if event.key == pygame.K_DOWN:
player_y_change = 2

if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT
player_x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player_y_change = 0
<小时/>

如果你想同时处理按键,这意味着你想在按下 2 个键时移动玩家对角线,并且你不希望玩家在松开 1 个键时停止,那么我建议使用 pygame.key.get_pressed()而不是 KEYDOWN 事件:

for event in  in pygame.event.get():
if game_over_state == False:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause = True
game_paused()

if game_over_state == False:

keys = pygame.key.get_pressed()

player_x_change, player_y_change = 0, 0
if keys[pygame.K_LEFT]:
player_x_change -= 2
if keys[pygame.K_RIGHT]:
player_x_change += 2
if keys[pygame.K_UP]:
player_y_change -= 2
if keys[pygame.K_DOWN]:
player_y_change += 2

关于python - 如何让游戏角色(用户) Action 流畅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60240802/

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