gpt4 book ai didi

python - 我怎样才能在pygame中进行二段跳?

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

谁能帮我调试我的代码,因为我不明白为什么我不能让我的角色用多个空格键进行双跳。当我运行脚本时,我可以上下左右移动,但只要按一次空格键,对象就会飞出窗口。

这个 if 语句带来了问题,所以我猜测这个 if 语句一直在运行并递增我的 jumpCount,我无法理解,因为按一次空格后 keys[pygame.K_SPACE 不应该] 计算结果为 true 然后再次返回 false 所以这个 if 语句不应该运行除非我按下另一个空格键?

else:
if keys[pygame.K_SPACE]:
jumpCount += 5
number_to_compensate += 1

这是我的脚本:

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 5

isJump = False

jumpCount = 5 #To calculate the height I have to rise by
number_to_compensate = 0 #So the more times I press space(higher I jump) the more I have to fall by

run = True

while run:
pygame.time.delay(20)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and x > vel:
x -= vel

if keys[pygame.K_RIGHT] and x < 500 - vel - width:
x += vel

if not (isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel

if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel

if keys[pygame.K_SPACE]:
isJump = True
number_to_compensate += 1
else:
if keys[pygame.K_SPACE]:
jumpCount += 5
number_to_compensate += 1
if jumpCount >= -5 *number_to_compensate:
y -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 5
isJump = False
number_to_compensate = 0



win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()

pygame.quit()

最佳答案

为了你想要的,你需要改变一些跳跃算法。根据加速度和速度计算跳跃。

定义重力和跳跃加速度的常量:

PLAYER_ACC = 15
PLAYER_GRAV = 1

使用键盘事件进行跳跃,而不是 pygame.key.get_pressed()。键盘事件(参见 pygame.event 模块)仅在键状态改变时发生一次。 KEYDOWN 事件在每次按下一个键时发生一次。 KEYUP 每次释放一个键时发生一次。将键盘事件用于单个操作。
设置按下 space 时的跳跃加速度:

acc_y = PLAYER_GRAV
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
acc_y = -PLAYER_ACC
vel_y = 0

根据加速度改变速度,根据每一帧的速度改变 y 坐标:

vel_y += acc_y
y += vel_y

通过地面限制y坐标:

if y + height > ground_y:
y = ground_y - height
vel_y = 0
acc_y = 0

最小的例子:

import pygame

pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")
clock = pygame.time.Clock()

ground_y = 400
x, y = 200, ground_y
width, height = 40, 60
vel_x, vel_y = 5, 0
acc_y = 0

PLAYER_ACC = 15
PLAYER_GRAV = 1

run = True
while run:
acc_y = PLAYER_GRAV
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
acc_y = -PLAYER_ACC
vel_y = 0

keys = pygame.key.get_pressed()
x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel_x
x = max(0, min(500 - width, x))

vel_y += acc_y
y += vel_y

if y + height > ground_y:
y = ground_y - height
vel_y = 0
acc_y = 0

win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
clock.tick(60)

pygame.quit()

关于python - 我怎样才能在pygame中进行二段跳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67667103/

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