0

now I'm trying to make good-looking jump animation in python, but it is too fast now. Is there a way to include gravity in the jump?

import pygame
W, H = 600, 600
playerx1 = 300
playery1 = 300
pygame.init()
screen = pygame.display.set_mode((W, H))
bg = pygame.image.load('bg.jpg').convert()
player1 = pygame.image.load('player1.png').convert_alpha()

velocity = [-7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2,
            -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5,
     5, 5.5, 6, 6.5, 7, 7.5]

i = 0


def player1blit():
    global playery1, i
    playery1 += velocity[i]
    screen.blit(player1,(playerx1, playery1))
    i+=1
    if i>(len(velocity)-1):
        i=0


def main():
    global playery1, playerx1, jump_status, velocity
    running = True
    while running:
        pygame.mouse.set_visible(False)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        screen.blit(bg, (0, 0))
        player1blit()



        pygame.display.flip()
        pygame.display.update()

Now the jump works, but it is very artificial

alec_djinn
  • 10,104
  • 8
  • 46
  • 71
witolo
  • 49
  • 6
  • I'm not sure if `pygame` has builtin physics methods (I doubt it does). You may have to come up with your own model. I found this talk quite interesting: https://www.youtube.com/watch?v=hG9SzQxaCm8 – SyntaxVoid Jan 08 '20 at 18:48
  • The formula for gravitational acceleration is pretty simple. Basically `acc_y = -9.8; vel_y += acc_y; pos_y += vel_y;` after you normalize for time deltas. – 0x5453 Jan 08 '20 at 19:27
  • `pygame` does not have any built-in physics. You need to write the motion equations and fine-tune the parameters by yourself. – Valentino Jan 08 '20 at 20:22

0 Answers0