1

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)



Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Habib Ismail
  • 69
  • 5
  • 16

2 Answers2

1

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
XyYang
  • 31
  • 4
1

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)]

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • What about the jump animation how would that work if my jump is that long wouldnt it play my jump animation fast or is there. A way to slow down the animation of my jump so it can fit this jump – Habib Ismail Jan 15 '21 at 15:19
  • ok I got on my pc to test this out my for some reason the jump isnt working [jumping part](https://pastebin.com/2WRsuddA) and this is what I put in my class [class script](https://pastebin.com/2WRsuddA) I did what you gave me but for some reason it isnt working at all I put the JumpCountStart = 50 above my player class this is what is happening right now [VIDEO](https://gyazo.com/9b870ab7bf613f72723b02e3ec9a7b4b) – Habib Ismail Jan 15 '21 at 17:22
  • could I ask if there is a faster way I could display then going to my folder of images and duplicating them 1 or more times? so I can play the images when the jumpcount is either 20 or 50 sorry if I am asking for to much! – Habib Ismail Jan 15 '21 at 18:12
  • @HabibIsmail Why do you duplicate the images? You just have to put the image multiple times in the list. Load the original images to `self.jump` and them put them twice into the list. e.g if you want to duplicate the images `self.jump = [self.jump[i//2] for i in range(len(self.jump)*2]`. I've extended the answer. – Rabbid76 Jan 15 '21 at 18:15
  • I mean everything works but the jump animation does not look normal at all!! but I will try to figure something out – Habib Ismail Jan 15 '21 at 18:48