Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Adding Boundary to an Object in Pygame
Next article icon
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. Ifisjumpvariable is True or False it indicates object is jumping or not. Ifisjumpis True, object position will be updated according to the above formula.Below is the implementation:Python3
# import pygame module in this programimportpygame# 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 namepygame.display.set_caption("Jump Game")# object current co-ordinatesx=200y=200# dimensions of the objectwidth=30height=40# Stores if player is jumping or notisjump=False# Force (v) up and mass m.v=5m=1# Indicates pygame is runningrun=True# infinite loopwhilerun:# completely fill the surface object# with black colourwin.fill((0,0,0))# drawing object on screen which is rectangle herepygame.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.foreventinpygame.event.get():# if event object type is QUIT# then quitting the pygame# and program both.ifevent.type==pygame.QUIT:# it will make exit the while looprun=False# stores keys pressedkeys=pygame.key.get_pressed()ifisjump==False:# if space bar is pressedifkeys[pygame.K_SPACE]:# make isjump equal to Trueisjump=Trueifisjump:# calculate force (F). F = 1 / 2 * mass * velocity ^ 2.F=(1/2)*m*(v**2)# change in the y co-ordinatey-=F# decreasing velocity while going up and become negative while coming downv=v-1# object reached its maximum heightifv<0:# negative sign is added to counter negative velocitym=-1# objected reaches its original stateifv==-6:# making isjump equal to falseisjump=False# setting original values to v and mv=5m=1# creates time delay of 10mspygame.time.delay(10)# it refreshes the windowpygame.display.update()# closes the pygame windowpygame.quit()
Output :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp