How Can I Improve This Jump?

Question:

VIDEO < this is my problem my jump is jumping way to fast and I dont event have time to move beaten where I Jumped from Is there a way I can improve this jump without slowing down my sprite fps? if I tried to slow down my sprite fps that would cause my sprite to play its animation way to many times when I jump. I am trying to make this jump smooth as possibale but right now its jumping way to fast and also the animation plays to fast

where my jump is

# our main loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False


    keys = pygame.key.get_pressed()


    if playerman.direction == "right":
        playerman.direction = "Idle"

    if playerman.direction == "left":
        playerman.direction = "leftid"

    


    if keys[pygame.K_SPACE]: #  -------------- IF WE CLICK SPACE THEN WE SHOULD PLAY JUMP ANIMATION
        playerman.direction = "jump"
        


    # player moving
    if not playerman.isJump: # -------------- make our player fall 
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

        collide = False
        if playerman.rect.bottom >= 605:
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = 605 - playerman.height

        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True

            playerman.fall = 1

            # -------------- this is the jUmp logic where I am having the problem
    else:
        if playerman.JumpCount >= -10:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.4
            playerman.JumpCount -= 1
        else:
            playerman.JumpCount = 10
            playerman.isJump = False
   
            if playerman.direction == "jump":         # if we click jump then it should play the jump then go back to the "idle"
                playerman.direction = "Idle"



my mouse sprite class


# our player class
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.idle = [pygame.image.load("grey_mouse/idle (" + str(i) + ").png") for i in range(1, 21)]
        self.leftid = [pygame.image.load("grey_mouse/id (" + str(i) + ").png") for i in range(1, 21)]

        self.Right = [pygame.image.load("grey_mouse/walk (" + str(i) + ").png") for i in range(1, 9)]
        self.left = [pygame.image.load("grey_mouse/left (" + str(i) + ").png") for i in range(1, 9)]
        self.jump = [pygame.image.load("grey_mouse/jump (" + str(i) + ").png") for i in range(1, 9)]

        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
        self.fps = 60
        self.next_frame_time = 0
        self.clock = pygame.time.Clock()
        self.direction = "jump"

        self.direction = "Idle"
        self.direction = "right"
        self.direction = "left"
        self.direction = "leftid"

        self.speed = 3
        self.anim_index = 0
        self.fall = 0
        self.isJump = False
        self.jumpCount = 10
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
         

            
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        if self.direction == "Idle":
            self.clock.tick(self.fps)
            image_list = self.idle

        elif self.direction == "right":
            self.clock.tick(self.fps)
            image_list = self.Right
                
        elif self.direction == "left":
            self.clock.tick(self.fps)
            image_list = self.left

        elif self.direction == "leftid":
            self.clock.tick(self.fps)
            image_list = self.leftid

        elif self.direction == "jump":
            self.clock.tick(60)
            image_list = self.jump

            
                # Is it time to show the next animation frame?
        time_now = pygame.time.get_ticks()
        if ( time_now > self.next_frame_time ):
                    # set the time for the next animation-frame
            inter_frame_delay = 1990 // self.fps   
            self.next_frame_time = time_now + inter_frame_delay  # in the future
                    # move the current image to the next (with wrap-around)
            self.anim_index += 1
            if self.anim_index >= len( image_list ):
                self.anim_index = 0
                            
        if self.anim_index >= len(image_list):
            self.anim_index = 0
        player_image = image_list[self.anim_index]

        player_image = image_list[self.anim_index]
        self.hitbox = (self.x, self.y + 30, 46,60)

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 0
        player_rect.centery += -20
        window.blit(player_image, player_rect)



Asked By: Habib Ismail

||

Answers:

I am just attempting to answer.
perhaps you can lower the increment of your fall or jump

    if not playerman.isJump: # -------------- make our player fall 
        playerman.y += playerman.fall
        playerman.fall += 0.1         #make the y increase slower, until you find the proper speed
        playerman.isJump = False
Answered By: XyYang

The jump time depends on the constant 10 used in the jump algorithm. Use a jumpCountStart variable instead of this constant.

jumpCountStart = 50

The height of the jump also depends on this constant. Scale the jump with 10 / jumpCountStart so that the height of the jump does not change when the jump is slowed down:

while run:
    # [...]

    if not playerman.isJump: 
        # [...]
        pass

    else:
        if playerman.JumpCount >= -jumpCountStart:
            jumpHeight = playerman.JumpCount * 10 / jumpCountStart
            playerman.y -= (jumpHeight * abs(jumpHeight)) * 0.4 * 10 / jumpCountStart
            playerman.JumpCount -= 1
        else:
            playerman.JumpCount = jumpCountStart
            playerman.isJump = False
   
            # [...]

Don’t forget to initialize the attribute JumpCount with jumpCountStart:

class player:
    def __init__(self,x,y,height,width,color):
        # [...]

        self.JumpCount = jumpCountStart

Animation with jumpCountStart = 50:


You have to show the same animation image multiple times. e.g. When jumpCountStart = 20 you have to show each image 2 times. When jumpCountStart = 50 you have to show each image 5 times.

self.jump = [pygame.image.load("grey_mouse/jump (" + str(i) + ").png")
             for i in range(1, 9)]
repeat = jumpCountStart // 10
self.jump = [self.jump[i // repeat] for i in range(len(self.jump) * repeat)]

Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.