Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Moving an object in PyGame - Python
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.In this article we will see how we can make design in PyGame with help of keys such that design i.e marker moves horizontally when pressing the right arrow key or left arrow key on the keyboard and it moves vertically when pressing up arrow key or down arrow key. We can do this by making a spot(marker) on the respective co-ordinates, which gets changes with the help of keys.
Change in Co-ordinates of marker for respective keys pressed :Left arrow key: Decrement in x co-ordinateRight arrow key: Increment in x co-ordinateUp arrow key: Decrement in y co-ordinateDown arrow key: Increment in y co-ordinate
Below is the implementation -Python3 1==
# 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("Moving rectangle")# marker current co-ordinatesx=200y=200# dimensions of the markerwidth=10height=10# velocity / speed of movementvel=10# Indicates pygame is runningrun=True# infinite loopwhilerun:# creates time delay of 10mspygame.time.delay(10)# 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()# if left arrow key is pressedifkeys[pygame.K_LEFT]andx>0:# decrement in x co-ordinatex-=vel# if left arrow key is pressedifkeys[pygame.K_RIGHT]andx<500-width:# increment in x co-ordinatex+=vel# if left arrow key is pressedifkeys[pygame.K_UP]andy>0:# decrement in y co-ordinatey-=vel# if left arrow key is pressedifkeys[pygame.K_DOWN]andy<500-height:# increment in y co-ordinatey+=vel# drawing spot on screen which is rectangle herepygame.draw.rect(win,(255,0,0),(x,y,width,height))# it refreshes the windowpygame.display.update()# closes the pygame windowpygame.quit()
Output :

Improve
Article Tags :
Practice Tags :

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