Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Making an object jump in PyGame
Next article icon

To make a game or animation inPythonusingPyGame, moving an object on the screen is one of the first things to learn. We will see how to move an object such that it moveshorizontallywhen pressing the right arrow key or left arrow key on the keyboard and it movesverticallywhen pressing up arrow key or down arrow key. We’ll create a game window, draw an object and update its position based on user input.

Change in Co-ordinates for respective keys pressed:

Left arrow key: Decrement in x co-ordinate
Right arrow key: Increment in x co-ordinate
Up arrow key:Decrement in y co-ordinate
Down arrow key:Increment in y co-ordinate

Setting Up PyGame

Before starting, make sure PyGame is installed. We can install it using:

pip install pygame

Example: Moving a Rectangle

In this code, we create a window using PyGame and draw a rectangle on the screen. The rectangle can be moved using the arrow keys and its position updates as we press the keys. This helps in learning how to handle movement and user input in PyGame.

Python
importpygamepygame.init()win=pygame.display.set_mode((500,500))pygame.display.set_caption("Moving rectangle")x=200y=200width=20height=20vel=10run=True# infinite loopwhilerun:pygame.time.delay(10)foreventinpygame.event.get():ifevent.type==pygame.QUIT:run=Falsekeys=pygame.key.get_pressed()ifkeys[pygame.K_LEFT]andx>0:x-=velifkeys[pygame.K_RIGHT]andx<500-width:x+=velifkeys[pygame.K_UP]andy>0:y-=velifkeys[pygame.K_DOWN]andy<500-height:y+=velwin.fill((0,0,0))pygame.draw.rect(win,(255,0,0),(x,y,width,height))pygame.display.update()pygame.quit()

Output

Explanation: This code -

  • Imports and initializesPygameto use its features.
  • Creates a 500x500 game window and sets the title.
  • Defines rectangle position(x, y), size and speed.
  • Runs a loop to keep the window active until closed.
  • Usespygame.event.get() to detect the quit event.
  • Usespygame.key.get_pressed() to detect arrow key presses.
  • Updates the rectangle’s position based on key input.
  • Clears the screen and redraws the rectangle every frame.
  • Refreshes the display usingpygame.display.update().
  • Exits Pygame when the loop ends.

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