Premium Only Content
 
			Pygame tutorial | Learn to code with python | Animations and Tile mapping
In this tutorial, we will be walking through some animation ideas and functions that can help you better understand how to place things on the screen with Pygame in Python.
Got an error? Compare your code below. Angle brackets aren't allow in description so I will replace them with "less than" and "greater than"
main.py code:
import math
import pygame
win_width = 800
win_height = 600
FPS = 60
tilesize = 32
speed = 32
def get_sprite(sheet, x, y, w, h):
sprite = pygame.Surface([w, h])
sprite.blit(sheet, (0, 0), (x, y, w, h))
sprite.set_colorkey('white')
return sprite
animation_list = [
[0, 0],
[32, 0],
[64, 0],
[96, 0],
[128, 0],
[0, 32],
[32, 32],
[64, 32],
[96, 32],
[128, 32],
]
def main():
pygame.init()
screen = pygame.display.set_mode((win_width, win_height), pygame.SCALED)
pygame.display.set_caption('Animation')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((100, 100, 100))
screen.blit(background, (0, 0))
clock = pygame.time.Clock()
spritesheet = pygame.image.load('numbers.png').convert()
animation_loop = 0
pic_x = 0
pic_y = 0
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
pic_y -= speed
elif event.key == pygame.K_DOWN:
pic_y += speed
elif event.key == pygame.K_LEFT:
pic_x -= speed
elif event.key == pygame.K_RIGHT:
pic_x += speed
if animation_loop "less than" 9:
pic = get_sprite(spritesheet, animation_list[math.floor(animation_loop)][0], animation_list[math.floor(animation_loop)][1], tilesize, tilesize)
animation_loop += 1
else:
pic = get_sprite(spritesheet, animation_list[math.floor(animation_loop)][0], animation_list[math.floor(animation_loop)][1], tilesize, tilesize)
animation_loop = 0
max_x = win_width / tilesize
max_y = win_height / tilesize
if pic_y "less than" win_height:
if pic_x "less than" win_width:
pic_x += speed
else:
pic_y += speed
pic_x = 0
else:
pic_x = 0
pic_y = 0
# screen.blit(background, (0, 0))
screen.blit(pic, (pic_x, pic_y))
pygame.display.flip()
if __name__ == '__main__':
main()
#python #pygame #animation
- 	
				 LIVE LIVEThe Jimmy Dore Show2 hours agoPentagon: “We Don’t Know Who We’re Killing!” Feds Question TX Man Over Israel Posts! w/James Corbett6,609 watching
- 	
				 1:12:57 1:12:57Kim Iversen2 hours agoBill Gates Suddenly Says “Don’t Worry About Climate Change”?49.6K15
- 	
				 1:05:12 1:05:12Michael Franzese2 hours agoI Waited 50 Years to Tell You What Happened on Halloween 19758.58K2
- 	
				 1:07:15 1:07:15Candace Show Podcast2 hours agoINFILTRATION: Charlie Kirk Was Being Tracked For Years. | Candace Ep 25628.8K86
- 	
				 LIVE LIVERallied1 hour agoWarzone Solo Challenges then RedSec Domination82 watching
- 	
				 LIVE LIVERed Pill News3 hours agoBoomerang Time - DOJ Investigating BLM Fraud on Red Pill News Live3,510 watching
- 	
				 1:46:14 1:46:14Roseanne Barr4 hours ago“The Over Emotional Are Always Under Informed” | The Roseanne Barr Podcast #12162.9K25
- 	
				 LIVE LIVENerdrotic5 hours ago $5.48 earnedThe WitcHER DOA | Box Office Massacre | Massive Industry Layoffs - Friday Night Tights 3781,354 watching
- 	
				 14:54 14:54IsaacButterfield13 hours ago $0.95 earnedShe Called Out “Creepy Men It Didn’t End Well12.8K33
- 	
				 LIVE LIVELFA TV21 hours agoLIVE & BREAKING NEWS! | FRIDAY 10/31/25681 watching