Skip to content Skip to sidebar Skip to footer

How To Make A Circular Object Jump Using Pygame?

I have just started using pygame, and I'm stuck. I'm not getting any syntax errors, but I'm sure there is some problem with the below code. import pygame import sys pygame.init()

Solution 1:

You have to compute m dependnet on v < 0:

m = -1if v < 0else1

The jump has to end if v < -5 and when the jump ends, then v has to be reset (v = 5):

jump_h = 5# try 10 for higher jumps
v = jump_h

while the_game_is_on:
    # [...]ifnot is_jump:
        # [...]else:
        if v >= -jump_h:
            m = -1if v < 0else1
            f = (1/2)*m*(v**2)
            v -=1
            ball_pos_y -= f
        else:
            is_jump = False
            v = jump_h

Solution 2:

I think you are hitting Python2 vs. Python3 difference:

$ python3
Python 3.7.7 (default, Mar 132020, 21:39:43) 
[GCC 9.2.120190827 (Red Hat 9.2.1-1)] on linux
Type"help", "copyright", "credits"or"license"for more information.
>>> m = 1>>> v = 5>>> (1/2)*m*(v**2)
12.5

In Python2, 1 / 2 equals 0, but when you use float: 1.0 / 2 is 0.5:

$ python2
Python 2.7.18 (default, Apr 212020, 18:49:31) 
[GCC 9.3.120200408 (Red Hat 9.3.1-2)] on linux2
Type"help", "copyright", "credits"or"license"for more information.
>>> m = 1>>> v = 5>>> (1/2)*m*(v**2)
0>>> (float(1)/2)*m*(v**2)
12.5

Post a Comment for "How To Make A Circular Object Jump Using Pygame?"