Python - Pygame Simple Sprite Movement

In this tutorial we will create a Simple Sprite Movement. Pygame is a Free and Open Source python programming language framework for making game applications. It is highly portable and runs on nearly every platform and operating system. It is highly recommended for beginner to learn Pygame when making basic games. So let's now do the coding... tut1

Getting Started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. After Python IDLE's is installed, open the command prompt then type "python -m pip install pygame", and hit enter. tut2

Importing Modules

After setting up the installation, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor
  1. import pygame, os
  2. from pygame.locals import *
  3.  
  4. pygame.init()

Writing The Variables

We will assign the a certain variables that we will need to declare later in the main loop. To do that just kindly write this code inside your text editor.
  1. #============VARIABLES==============
  2. os.environ['SDL_VIDEO_CENTERED'] = '1'
  3. clock = pygame.time.Clock()
  4. screen_width=800
  5. screen_height=600
  6. bgColor = (255, 255, 255)
  7. size=(screen_width, screen_height)
  8. display = pygame.display.set_mode(size)
  9. progress = True
  10. pygame.display.set_caption('Python - Pygame Simple Sprite Movement')
  11. char = pygame.image.load(spider).convert_alpha()
  12. x=325
  13. y=250
  14. speed = 5
  15. left = False
  16. right = False
  17. up = False
  18. down = False

Main Function

This is the Main Code for the Pygame application. The code contains a several input function that will allow the object to move and render it to the application. To do that just write this code inside the IDLE text editor.
  1. #============MAIN LOOP==============
  2. while progress:
  3.         display.fill(bgColor)
  4.         for event in pygame.event.get():
  5.                 if event.type == pygame.QUIT:
  6.                         progress = False
  7.                         pygame.quit()
  8.                         quit()
  9.                 elif event.type == pygame.KEYDOWN:
  10.                         if event.key == pygame.K_q:
  11.                                 progress = False
  12.                                 pygame.quit()
  13.                                 quit()
  14.                 #Player Input KeyDown
  15.                 if event.type == pygame.KEYDOWN:
  16.                         if event.key == pygame.K_LEFT:
  17.                                 left = True
  18.                         elif event.key == pygame.K_RIGHT:
  19.                                 right = True
  20.                         elif event.key == pygame.K_UP:
  21.                                 up = True
  22.                         elif event.key == pygame.K_DOWN:
  23.                                 down = True
  24.                        
  25.                 #Player Input KeyUP          
  26.                 if event.type == pygame.KEYUP:
  27.                         if event.key == pygame.K_LEFT:
  28.                                 left = False
  29.                         elif event.key == pygame.K_RIGHT:
  30.                                 right = False
  31.                         elif event.key == pygame.K_UP:
  32.                                 up = False
  33.                         elif event.key == pygame.K_DOWN:
  34.                                 down = False
  35.         #Sprite Started To Move
  36.         if left:
  37.                 x-=speed
  38.         elif right:
  39.                 x+=speed
  40.         elif up:
  41.                 y-=speed
  42.         elif down:
  43.                 y+=speed
  44.  
  45.         #Limit The Player Movement within the boundary
  46.         if x > screen_width - char.get_width():
  47.                 x = screen_width - char.get_width()
  48.         if x < 0:
  49.                 x = 0
  50.         if y > screen_height - char.get_height():
  51.                 y = screen_height - char.get_height()
  52.         if y < 0:
  53.                 y = 0
  54.        
  55.        
  56.         display.blit(char, (x, y))
  57.         pygame.display.flip()
  58.  
  59.         #Framerate of the Game
  60.         clock.tick(60)
There you have it we just created a Simple Sprite Movement using Pygame. I hope that this tutorial help you for what you are looking for . For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Tags

Add new comment