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
-
24:05
Glenn Greenwald
4 hours agoAs U.S. Censorship Escalates, New Poll Reveals Declining Support for Israel: UNLOCKED Episode
18.5K41 -
1:00:41
Donald Trump Jr.
6 hours agoMake Main St Great Again, Interviews with Alex Marlow & John Phillips | TRIGGERED Ep.233
73.1K42 -
1:01:53
BonginoReport
4 hours agoPolitical Violence on the Rise in America - Nightly Scroll w/Hayley Caronia (Ep.26) - 04/14/2025
49.9K34 -
LIVE
The Jimmy Dore Show
2 hours agoBill Maher CHARMED by Donald Trump! Speaking English Now a HATE CRIME in Britain! w/ Viva Frei
13,249 watching -
LIVE
Joe Pags
2 hours agoThe Joe Pags Show 4-14-25
735 watching -
56:14
Sarah Westall
2 hours agoGlobal Agenda: Starve Small Business of Funds w/ Bruce De Torres
5.41K -
LIVE
2 MIKES LIVE
5 hours ago2 MIKES LIVE #205 with guest Nick Adams!
146 watching -
DVR
LFA TV
8 hours agoThe Bread of Life | TRUMPET DAILY 4.14.25 7PM
7.2K6 -
37:52
Kimberly Guilfoyle
3 hours agoThe Trump Effect, Plus More Scandals for Leticia James, Live with Roger Stone | Ep213
33.6K15 -
1:13:45
Kim Iversen
3 hours agoWe're LOSING The Tariff War With China, How Our Elites Sold Us Out | Oct 7th Rape COVERUP
61.8K97