0

I was trying to build a game on Pygame. Now the jump animation that I added gets over in like 0.01 seconds. What could I do to make it last for a second at least ?

the player class -

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     

the game loop

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

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

runs 60 times per second.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I am already using Clock. So while the running and attack animations are seamless, jump goes very slowly. Also I am a beginner, so please pardon me if I seem ridiculous – Faizan Haider Mar 19 '21 at 05:56
  • In your comment here you say jump goes slowly, yet your original question says it's over to quickly. Being a beginner makes no question ridiculous. Arcade games are not simple until you fully understand event processing, frame processing, handling multiple states of multiple entities.... You'll get there. – RufusVS Mar 19 '21 at 16:56