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...
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.
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
- import pygame, sys, os
- from pygame.locals import *
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.
- #Game Initialization
- #Center the Game Application
- # Game Resolution
- screen=pygame.display.set_mode((screen_width, screen_height))
- # Color
- black=(0, 0, 0)
- white=(255, 255, 255)
- # Fonts
- # Framerate
- # Image Files
- # Slicing The Sprite Sheet
- for y in range(0, imageSize, int(cell_height)):
- for x in range(0, imageSize, int(cell_width)):
- surface = pygame.Surface((cell_width, cell_height))
- surface.blit(image, (0, 0),
- (x, y, cell_width, cell_height))
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.
- # Methods
- def text_format(text, textFont, textSize, textColor):
- font=pygame.font.Font(textFont, textSize)
- newText=font.render(text, 0, textColor)
- return newText
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.
- # Main Loop
- while True:
- for event in pygame.event.get():
- if event.type==pygame.QUIT:
- if cell_position < len(cell_list)-1:
- title=text_format("Python - Pygame Simple SpriteSheet Animation", font, 40, white)
- screen.blit(title, (screen_width/2 - (titleRect/2), 50))
- screen.blit(cell_list[cell_position], (300, 200))
- pygame.display.set_caption("Python - Pygame Simple SpriteSheet Animation")
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!!
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.