【问题标题】:Pygame - jump animation gets over too quickly [duplicate]Pygame - 跳跃动画过快[重复]
【发布时间】: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


【解决方案1】:

使用pygame.time.Clock 控制每秒帧数,从而控制游戏速度。

pygame.time.Clock 对象的方法tick() 以这种方式延迟游戏,即循环的每次迭代消耗相同的时间段。见pygame.time.Clock.tick():

这个方法应该每帧调用一次。

这意味着循环:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(60)

每秒运行 60 次。

【讨论】:

  • 我已经在使用时钟了。因此,虽然跑步和攻击动画是无缝的,但跳跃却非常缓慢。我也是初学者,如果我看起来很可笑,请原谅我
  • 在你的评论中你说跳跃很慢,但你原来的问题说它很快就结束了。作为一个初学者,毫无疑问是荒谬的。在您完全了解事件处理、帧处理、处理多个实体的多种状态之前,街机游戏并不简单……您会到达那里。
猜你喜欢
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多