如何使角色在Pygame中跳跃?
我想让我的角色跳跃。在我目前的尝试中,只要我按住 space v,播放器就会上升,当我发布 space 时,播放器就会下降。
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
rect = pygame.Rect(135, 220, 30, 30)
vel = 5
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
rect.centerx = (rect.centerx + (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel) % 300
if keys[pygame.K_SPACE]:
rect.y -= 1
elif rect.y < 220:
rect.y += 1
window.fill((0, 0, 64))
pygame.draw.rect(window, (64, 64, 64), (0, 250, 300, 100))
pygame.draw.circle(window, (255, 0, 0), rect.center, 15)
pygame.display.flip()
pygame.quit()
exit()
但是,如果我一次击中 space ,我希望角色跳跃。当 space 按下一次时,我想要一个平滑的跳跃动画。 我该如何逐步进行此?
I want to make my character jump. In my current attempt, the player moves up as long as I hold down SPACEv and falls down when I release SPACE.
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
rect = pygame.Rect(135, 220, 30, 30)
vel = 5
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
rect.centerx = (rect.centerx + (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel) % 300
if keys[pygame.K_SPACE]:
rect.y -= 1
elif rect.y < 220:
rect.y += 1
window.fill((0, 0, 64))
pygame.draw.rect(window, (64, 64, 64), (0, 250, 300, 100))
pygame.draw.circle(window, (255, 0, 0), rect.center, 15)
pygame.display.flip()
pygame.quit()
exit()
However, I want the character to jump if I hit the SPACE once. I want a smooth jump animation to start when SPACE is pressed once.
How would I go about this step by step?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使角色跳跃,您必须使用
KEYDOWN
事件,但不是pygame.key.get_pressed()
。pygame.key.get_pressed ()
用于按住按键时连续移动。键盘事件用于触发单个动作或启动动画(例如跳跃)。请参阅如何在pygame中获取键盘输入?pygame.key.get_pressed()
返回包含每个键状态的序列。如果按住某个键,则该键的状态为True
,否则为False
。使用pygame.key.get_pressed()
评估按钮的当前状态并获得连续移动。使用
pygame.time.Clock
(“此方法应该每帧调用一次。”)您可以控制每秒的帧数,从而控制游戏速度和跳跃的持续时间。跳跃应该独立于玩家的移动或游戏的一般控制流程。因此,应用程序循环中的跳跃动画必须与正在运行的游戏并行执行。
当你扔球或跳跃时,物体会形成抛物线。物体一开始会快速增加高度,但速度会减慢,直到物体再次开始越来越快地下落。跳跃物体的高度变化可以用以下序列来描述:
可以使用以下算法生成这样的序列(
y
是物体的 y 坐标):更复杂的方法是定义玩家跳跃时的重力和玩家加速度的常数:
在每一帧中施加在玩家身上的加速度是重力常数,如果玩家跳跃,那么加速度将变为单帧的“跳跃”加速度:
在每一帧中垂直速度根据加速度和y 坐标根据速度而变化。当玩家接触地面时,垂直运动将停止:
另请参阅 跳转
示例1:replit.com/@Rabbid76/PyGame-Jump
示例 2:replit.com/@Rabbid76/PyGame-JumpAcceleration
To make a character jump you have to use the
KEYDOWN
event, but notpygame.key.get_pressed()
.pygame.key.get_pressed ()
is for continuous movement when a key is held down. The keyboard events are used to trigger a single action or to start an animation such as a jump. See alos How to get keyboard input in pygame?pygame.key.get_pressed()
returns a sequence with the state of each key. If a key is held down, the state for the key isTrue
, otherwiseFalse
. Usepygame.key.get_pressed()
to evaluate the current state of a button and get continuous movement.Use
pygame.time.Clock
("This method should be called once per frame.") you control the frames per second and thus the game speed and the duration of the jump.The jumping should be independent of the player's movement or the general flow of control of the game. Therefore, the jump animation in the application loop must be executed in parallel to the running game.
When you throw a ball or something jumps, the object makes a parabolic curve. The object gains height quickly at the beginning, but this slows down until the object begins to fall faster and faster again. The change in height of a jumping object can be described with the following sequence:
Such a series can be generated with the following algorithm (
y
is the y coordinate of the object):A more sophisticated approach is to define constants for the gravity and player's acceleration as the player jumps:
The acceleration exerted on the player in each frame is the gravity constant, if the player jumps then the acceleration changes to the "jump" acceleration for a single frame:
In each frame the vertical velocity is changed depending on the acceleration and the y-coordinate is changed depending on the velocity. When the player touches the ground, the vertical movement will stop:
See also Jump
Example 1: replit.com/@Rabbid76/PyGame-Jump
Example 2: replit.com/@Rabbid76/PyGame-JumpAcceleration