如何在 Pygame 中让角色跳跃?
- 2025-02-18 09:23:00
- admin 原创
- 93
问题描述:
我想让我的角色跳跃。在我目前的尝试中,只要我按住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
。我该如何一步一步地完成这个任务?
解决方案 1:
要使角色跳跃,您必须使用KEYDOWN
事件,但不能使用pygame.key.get_pressed()
。pygame.key.get_pressed ()
是按住某个键时持续移动。键盘事件用于触发单个动作或启动诸如跳跃之类的动画。另请参阅如何在 pygame 中获取键盘输入?
pygame.key.get_pressed()
返回包含每个键状态的序列。如果某个键被按下,则该键的状态为True
,否则为False
。pygame.key.get_pressed()
用于评估按钮的当前状态并获得连续移动。
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
jump = True
使用pygame.time.Clock
(“此方法应每帧调用一次。”)您可以控制每秒的帧数,从而控制游戏速度和跳跃的持续时间。
clock = pygame.time.Clock()
while True:
clock.tick(100)
跳跃应该独立于玩家的移动或游戏的总体控制流程。因此,应用程序循环中的跳跃动画必须与正在运行的游戏并行执行。
当你扔球或某物跳跃时,物体会形成抛物线。物体一开始会迅速上升,但速度会减慢,直到物体再次开始越来越快地下降。跳跃物体的高度变化可以用以下序列来描述:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
这样的系列可以用以下算法生成(y
是对象的 y 坐标):
jumpMax = 10
if jump:
y -= jumpCount
if jumpCount > -jumpMax:
jumpCount -= 1
else:
jump = False
一种更复杂的方法是定义玩家跳跃时的重力和玩家加速度常量:
acceleration = 10
gravity = 0.5
每帧中对玩家施加的加速度是重力常数,如果玩家跳跃,则加速度变为单帧的“跳跃”加速度:
acc_y = gravity
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if vel_y == 0 and event.key == pygame.K_SPACE:
acc_y = -acceleration
在每一帧中,垂直速度根据加速度而变化,y 坐标根据速度而变化。当玩家接触地面时,垂直运动将停止:
vel_y += acc_y
y += vel_y
if y > ground_y:
y = ground_y
vel_y = 0
acc_y = 0
另请参阅跳转
示例 1: replit.com/@Rabbid76/PyGame-Jump
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
rect = pygame.Rect(135, 220, 30, 30)
vel = 5
jump = False
jumpCount = 0
jumpMax = 15
run = True
while run:
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if not jump and event.key == pygame.K_SPACE:
jump = True
jumpCount = jumpMax
keys = pygame.key.get_pressed()
rect.centerx = (rect.centerx + (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel) % 300
if jump:
rect.y -= jumpCount
if jumpCount > -jumpMax:
jumpCount -= 1
else:
jump = False
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()
示例 2: replit.com/@Rabbid76/PyGame-JumpAcceleration
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
player = pygame.sprite.Sprite()
player.image = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.draw.circle(player.image, (255, 0, 0), (15, 15), 15)
player.rect = player.image.get_rect(center = (150, 235))
all_sprites = pygame.sprite.Group([player])
y, vel_y = player.rect.bottom, 0
vel = 5
ground_y = 250
acceleration = 10
gravity = 0.5
run = True
while run:
clock.tick(100)
acc_y = gravity
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if vel_y == 0 and event.key == pygame.K_SPACE:
acc_y = -acceleration
keys = pygame.key.get_pressed()
player.rect.centerx = (player.rect.centerx + (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel) % 300
vel_y += acc_y
y += vel_y
if y > ground_y:
y = ground_y
vel_y = 0
acc_y = 0
player.rect.bottom = round(y)
window.fill((0, 0, 64))
pygame.draw.rect(window, (64, 64, 64), (0, 250, 300, 100))
all_sprites.draw(window)
pygame.display.flip()
pygame.quit()
exit()
扫码咨询,免费领取项目管理大礼包!