【发布时间】:2021-03-19 05:05:35
【问题描述】:
我试图在 Pygame 上构建游戏。现在我添加的跳跃动画在 0.01 秒内完成。我该怎么做才能让它至少持续一秒钟?
播放器类 -
class player_level1(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.move_frame = 0
self.image = pygame.image.load("Player_Sprite_R.png")
self.pos = vec(int(width/2),286-53)
self.vel = vec(0,40)
self.acc = vec(0,8)
self.jumping = False
self.rect = self.image.get_rect()
def move(self):
run_ani_R = [pygame.image.load("Player_Sprite_R.png"),
pygame.image.load("Player_Sprite2_R.png"),
pygame.image.load("Player_Sprite3_R.png"),
pygame.image.load("Player_Sprite4_R.png"),
pygame.image.load("Player_Sprite5_R.png"),
pygame.image.load("Player_Sprite6_R.png"),
pygame.image.load("Player_Sprite_R.png")
]
self.move_frame += 1
if self.move_frame > 6 :
self.move_frame = 0
self.image = run_ani_R[self.move_frame]
def render(self):
print(self.pos)
self.rect.bottomleft = self.pos
displaysurface.blit(self.image,self.pos)
def jump(self):
self.rect.y += 1
print("jump")
#check to see if player is in contact with the ground
hits = pygame.sprite.spritecollide(self,floor_group,False)
self.rect.y -= 1
#If touching the ground, and not currently jumping -> jump
if hits and not self.jumping:
print("Jump exec")
self.jumping = True
self.vel.y = 12
displaysurface.blit(self.image,self.rect)
self.jumping = False
def gravity(self):
hits = pygame.sprite.spritecollide(player,floor_group,False)
if self.vel.y > 0 :
if hits :
lowest = hits[0]
if self.pos.y < lowest.rect.bottom :
self.pos.y = lowest.rect.top + 1
self.vel.y = 0
self.jumping = False
#lines to incorporate platformer genre
游戏循环
while True:
if Levels.level == 1:
player.gravity()
.....
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.jumping == True
player.jump()
【问题讨论】:
-
你在哪里限制更新率?
-
呃,对不起,这是什么意思?
-
@FaizanHaider 表示你在哪里打电话
pygame.time.Clock.tick()或pygame.time.delay -
哦。我在游戏循环结束时限制它。我已将其计时至 60 以生成 60 fps。其他动画是无缝的,只是跳跃让我很烦
-
你永远不会改变你的玩家速度,因为你在调用你的 jump() 例程之前设置了
self.jumping == True。我不认为它永远不会执行 if 块。
标签: python pygame game-physics gravity