gpt4 book ai didi

python - 如何使用pygame使圆形物体跳跃?

转载 作者:行者123 更新时间:2023-12-04 09:31:29 25 4
gpt4 key购买 nike

我刚开始使用 ,我被卡住了。
我没有收到任何语法错误,但我确定下面的代码存在一些问题。

import pygame
import sys

pygame.init()
pygame.display.set_caption('Jumper Game')

display_width = 500
display_height = 500
the_game_is_on = True

ball_pos_x = 200
ball_pos_y = 500
ball_radius = 20
ball_color = [0,0,255]
speed = 1

is_jump = False
m = 1
v = 5

dis = pygame.display.set_mode((display_width,display_height)) #screen

while the_game_is_on:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and ball_pos_x > 20:
ball_pos_x-= speed
if keys[pygame.K_RIGHT] and ball_pos_x < (display_width - (ball_radius)):
ball_pos_x+= speed
if not (is_jump):
if keys[pygame.K_UP] and ball_pos_y > 20:
ball_pos_y-= speed
if keys[pygame.K_DOWN] and ball_pos_y < (display_height - (ball_radius)):
ball_pos_y+= speed
if keys[pygame.K_SPACE]:
is_jump = True
else:
f = (1/2)*m*(v**2)
ball_pos_y-=f
v-=1
if v < 0:
m = -1
if v >= ((v+1)*-1): #to check the initial position
is_jump = False
pygame.time.delay(10)
dis.fill((0,0,0))
pygame.draw.circle(dis,ball_color,(ball_pos_x,int(ball_pos_y)),ball_radius)
pygame.display.update()
pygame.display.quit()
关于跳转代码的编写,我引用了这个 website ,本网站的源代码完美运行。

最佳答案

你必须计算 m依赖于 v < 0 :

m = -1 if v < 0 else 1
如果 v < -5,跳转必须结束当跳转结束时,然后 v必须重置( v = 5 ):
jump_h = 5 # try 10 for higher jumps
v = jump_h

while the_game_is_on:
# [...]

if not is_jump:
# [...]

else:
if v >= -jump_h:
m = -1 if v < 0 else 1
f = (1/2)*m*(v**2)
v -=1
ball_pos_y -= f
else:
is_jump = False
v = jump_h

关于python - 如何使用pygame使圆形物体跳跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62822322/

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