Python - Pygame Simple Animation

In this tutorial we will create a Simple Animation Using Pygame. 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...

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. tut1 Wait for the Pygame to be downloaded and installed at the same time. When its completed type "python -m pygame.examples.oldaliens" and hit enter to check if Pygame is good to go. 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 *
Then write these several line of codes to make the application work properly
  1. #Center the Application
  2. os.environ['SDL_VIDEO_CENTERED'] = '1'
  3.  
  4. #Initialize the modules
  5. pygame.init()

Assigning Variables

We will assign the a certain variables that we will need to declare later in the main function. To do that just kindly write this code inside your text editor
  1. y1 = 0
  2. y2 = 0
  3. y3 = 0
  4. x1 = 0
  5. speed1 = 0.5
  6. speed2 = 0.55
  7. speed3 = 0.6
  8. speed4 = 0.6
  9. bgColor = 0, 0, 0
  10. barHeight = 75
  11. barWidth = 25
  12. screen_width = 800
  13. screen_height = 600
  14. display = pygame.display.set_mode((screen_width, screen_height))
  15. pygame.display.set_caption("Pygame Simple Animation")
  16. progress = True
  17. barcolor1 = (255, 0, 0)
  18. barcolor2 = (0, 255, 0)
  19. barcolor3 = (0, 0, 255)
  20. barcolor4 = (255, 255, 255)

Creating The Main Function

This is the Main Code for the Pygame application. The code contains a several function that will render the objects of the application. To do that just write this code inside the IDLE text editor.
  1. while progress:
  2.     event = pygame.event.poll()
  3.     if event.type == pygame.QUIT:
  4.         progress = False
  5.         pygame.quit()
  6.         quit()
  7.        
  8.     display.fill((bgColor))
  9.    
  10.     for i in range(0, barHeight):
  11.         pygame.draw.line(display, barcolor1, (0, y1+i), (screen_width-1, y1+i))
  12.  
  13.     y1 += speed1
  14.    
  15.     if y1 + barHeight > screen_height-1 or y1 < 0:
  16.         speed1 *= -1
  17.    
  18.     for i in range(0, barHeight):
  19.         pygame.draw.line(display, barcolor2, (0, y2+i), (screen_width-1, y2+i))
  20.  
  21.     y2 += speed2
  22.    
  23.     if y2 + barHeight > screen_height-1 or y2 < 0:
  24.         speed2 *= -1
  25.  
  26.        
  27.     for i in range(0, barHeight):
  28.         pygame.draw.line(display, barcolor3, (0, y3+i), (screen_width-1, y3+i))
  29.  
  30.     y3 += speed3
  31.    
  32.     if y3 + barHeight > screen_height-1 or y3 < 0:
  33.         speed3 *= -1
  34.  
  35.        
  36.     for i in range(0, barWidth):
  37.         pygame.draw.line(display, barcolor4, (x1+i, 0), (x1+i, screen_height-1))
  38.    
  39.    
  40.     x1 += speed4
  41.    
  42.  
  43.     if x1 + barWidth > screen_width-1 or x1 < 0:
  44.         speed4 *= -1
  45.    
  46.     pygame.display.flip()
Then try to run it and see if it works. tut3 There you have it we just created a Simple Animation Using Pygame. I hope that this tutorial help you understand about Pygame. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Tags

Add new comment