gpt4 book ai didi

python - 如何在 pygame 中为我的角色添加加速?

转载 作者:行者123 更新时间:2023-12-02 05:28:30 25 4
gpt4 key购买 nike

我对 python 和 pygame 还算陌生,我需要有关加速的帮助。我遵循了 YouTube 上关于如何为平台游戏制作基础的教程,并且我一直在使用它来创建一款像星之卡比游戏一样的游戏。我在《卡比》游戏中注意到的一个小细节是,当你朝一个方向移动,然后快速转向另一个方向时,他会打滑,在过去的几天里,我一直在研究如何让它发挥作用。我想出的解决方案是让角色不再是在按下某个键时就移动,而是会加速,然后在达到最大速度后停止加速,然后在按下另一个键时快速减速并再次加速方向键。问题是,我不知道如何编写加速程序。谁能帮我解决这个问题吗?

这是我为游戏编写的代码(第一位用于碰撞,第二位用于实际移动玩家):

def move(rect, movement, tiles):
collide_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
rect.x += movement[0]
hit_list = collide_test(rect, tiles)
for tile in hit_list:
if movement[0] > 0:
rect.right = tile.left
collide_types['right'] = True
if movement[0] < 0:
rect.left = tile.right
collide_types['left'] = True
rect.y += movement[1]
hit_list = collide_test(rect, tiles)
for tile in hit_list:
if movement[1] > 0:
rect.bottom = tile.top
collide_types['bottom'] = True
if movement[1] < 0:
rect.top = tile.bottom
collide_types['top'] = True

return rect, collide_types

第二位:

player_move = [0, 0]
if move_right:
player_move[0] += 2.5
elif run:
player_move[0] += -3
if move_left:
player_move[0] -= 2
elif run:
player_move[0] -= -3
player_move[1] += verticle_momentum
verticle_momentum += 0.4
if verticle_momentum > 12:
verticle_momentum = 12
elif slow_fall == True:
verticle_momentum = 1

if fly:
verticle_momentum = -2
slow_fall = True
if verticle_momentum != 0:
if ground_snd_timer == 0:
ground_snd_timer = 20

最佳答案

您不应该在按下按钮时直接更改角色的位置,而应该更改速度。例如,仅在 X 轴上移动:

acc = 0.02 # the rate of change for velocity
if move_right:
xVel += acc
if move_left:
xVel -= acc

# And now change your character position based on the current velocity
character.pose.x += xVel

您可以添加的其他内容:当您不按任何键时,您会失去动力,这样您就可以停下来。您可以通过从速度中减去或添加一定的衰减因子(该衰减因子小于您的加速度常数,但您必须在尝试游戏时对其进行调整)来做到这一点。

关于python - 如何在 pygame 中为我的角色添加加速?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59810697/

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