Python - Pygame Simple SpriteSheet Animation
Submitted by razormist on Monday, December 18, 2017 - 22:14.
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...
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!!
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.
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- import pygame, sys, os
- 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.- #Game Initialization
- pygame.init()
- #Center the Game Application
- os.environ['SDL_VIDEO_CENTERED']='1'
- # Game Resolution
- screen_width=800
- screen_height=600
- screen=pygame.display.set_mode((screen_width, screen_height))
- # Color
- black=(0, 0, 0)
- white=(255, 255, 255)
- # Fonts
- font="fonts/Gamer.ttf"
- # Framerate
- clock=pygame.time.Clock()
- fps=12
- # Image Files
- image=pygame.image.load("images/sprite.png")
- imageSize=image.get_size()
- horiz_cell=6
- vert_cell=1
- cell_width=imageSize[0]/horiz_cell
- cell_height=imageSize[1]/vert_cell
- cell_list=[]
- cell_position=0
- # Slicing The Sprite Sheet
- for y in range(0, imageSize[1], int(cell_height)):
- for x in range(0, imageSize[0], int(cell_width)):
- surface = pygame.Surface((cell_width, cell_height))
- surface.blit(image, (0, 0),
- (x, y, cell_width, cell_height))
- 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.- # Methods
- def text_format(text, textFont, textSize, textColor):
- font=pygame.font.Font(textFont, textSize)
- newText=font.render(text, 0, textColor)
- 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.- # Main Loop
- while True:
- for event in pygame.event.get():
- if event.type==pygame.QUIT:
- pygame.quit()
- sys.exit()
- if cell_position < len(cell_list)-1:
- cell_position+=1
- else:
- cell_position=0
- title=text_format("Python - Pygame Simple SpriteSheet Animation", font, 40, white)
- titleRect=title.get_rect()
- screen.blit(title, (screen_width/2 - (titleRect[2]/2), 50))
- screen.blit(cell_list[cell_position], (300, 200))
- pygame.display.update()
- pygame.display.set_caption("Python - Pygame Simple SpriteSheet Animation")
- clock.tick(fps)
Add new comment
- 714 views