Skip to main content

Python - Pygame Simple SpriteSheet Animation


Body

In this tutorial we will create a Simple SpriteSheet Animation. Pygame is a cross-platform set of Python modules designed for creating games. It includes computer graphics and sound libraries designed to be used with the Python programming language. This is a good opportunity for beginners to learn Pygame first when it comes in developing some 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

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, sys, os
  2. from pygame.locals import *

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. #Game Initialization
  2. pygame.init()
  3.  
  4. #Center the Game Application
  5. os.environ['SDL_VIDEO_CENTERED']='1'
  6.  
  7. # Game Resolution
  8. screen_width=800
  9. screen_height=600
  10. screen=pygame.display.set_mode((screen_width, screen_height))
  11.  
  12. # Color
  13. black=(0, 0, 0)
  14. white=(255, 255, 255)
  15.  
  16. # Fonts
  17. font="fonts/Gamer.ttf"
  18.  
  19. # Framerate
  20. clock=pygame.time.Clock()
  21. fps=12
  22.  
  23.  
  24. # Image Files
  25. image=pygame.image.load("images/sprite.png")
  26. imageSize=image.get_size()
  27. horiz_cell=6
  28. vert_cell=1
  29. cell_width=imageSize[0]/horiz_cell
  30. cell_height=imageSize[1]/vert_cell
  31.  
  32. cell_list=[]
  33.  
  34. cell_position=0
  35.  
  36. # Slicing The Sprite Sheet
  37. for y in range(0, imageSize[1], int(cell_height)):
  38. for x in range(0, imageSize[0], int(cell_width)):
  39. surface = pygame.Surface((cell_width, cell_height))
  40. surface.blit(image, (0, 0),
  41. (x, y, cell_width, cell_height))
  42. cell_list.append(surface)

Declaring Methods

We will now declare some methods to make it easier to call it in the main functions. This contains several function that can be use all through.

  1. # Methods
  2. def text_format(text, textFont, textSize, textColor):
  3. font=pygame.font.Font(textFont, textSize)
  4. newText=font.render(text, 0, textColor)
  5. return newText

Main Function

This is the Main Code for the Pygame application. The code contains a several function that will allow to render the application. To do that just write this code inside the IDLE text editor.

  1. # Main Loop
  2. while True:
  3. for event in pygame.event.get():
  4. if event.type==pygame.QUIT:
  5. pygame.quit()
  6. sys.exit()
  7.  
  8. if cell_position < len(cell_list)-1:
  9. cell_position+=1
  10. else:
  11. cell_position=0
  12. title=text_format("Python - Pygame Simple SpriteSheet Animation", font, 40, white)
  13. titleRect=title.get_rect()
  14. screen.blit(title, (screen_width/2 - (titleRect[2]/2), 50))
  15. screen.blit(cell_list[cell_position], (300, 200))
  16. pygame.display.update()
  17. pygame.display.set_caption("Python - Pygame Simple SpriteSheet Animation")
  18. clock.tick(fps)

There you have it we successfully created a Simple SpriteSheet Animation using Pygame. I hope that this simple tutorial help you to what you are looking for and enhance your programming capabilities. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.