Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit.
So, in this article, we will learn how to make an object jump using PyGame library in Python.
There is basic formula from classical mechanics to make an object jump.
F = 1/2 * m * v^2
Where F is the force up/down, m is the mass of the object and v is the velocity. The velocity goes down over time because when the object jumps the velocity will not increase more in this simulation. When object reaches the ground, the jump ends. If isjump
variable is True or False it indicates object is jumping or not. If isjump
is True, object position will be updated according to the above formula.
# import pygame module in this program import pygame # activate the pygame library . # initiate pygame and give permission # to use pygame's functionality. pygame.init() # create the display surface object # of specific dimension..e(500, 500). win = pygame.display.set_mode(( 500 , 500 )) # set the pygame window name pygame.display.set_caption( "Jump Game" ) # object current co-ordinates x = 200 y = 200 # dimensions of the object width = 30 height = 40 # Stores if player is jumping or not isjump = False # Force (v) up and mass m. v = 5 m = 1 # Indicates pygame is running run = True # infinite loop while run: # completely fill the surface object # with black colour win.fill(( 0 , 0 , 0 )) # drawing object on screen which is rectangle here pygame.draw.rect(win, ( 255 , 0 , 0 ), (x, y, width, height)) # iterate over the list of Event objects # that was returned by pygame.event.get() method. for event in pygame.event.get(): # if event object type is QUIT # then quitting the pygame # and program both. if event. type = = pygame.QUIT: # it will make exit the while loop run = False # stores keys pressed keys = pygame.key.get_pressed() if isjump = = False : # if space bar is pressed if keys[pygame.K_SPACE]: # make isjump equal to True isjump = True if isjump : # calculate force (F). F = 1 / 2 * mass * velocity ^ 2. F = ( 1 / 2 ) * m * (v * * 2 ) # change in the y co-ordinate y - = F # decreasing velocity while going up and become negative while coming down v = v - 1 # object reached its maximum height if v< 0 : # negative sign is added to counter negative velocity m = - 1 # objected reaches its original state if v = = - 6 : # making isjump equal to false isjump = False # setting original values to v and m v = 5 m = 1 # creates time delay of 10ms pygame.time.delay( 10 ) # it refreshes the window pygame.display.update() # closes the pygame window pygame.quit() |
Output :
<!–
–>
Please Login to comment…