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
-
43:16
Mike Rowe
4 days agoHe Manages Billions WITHOUT A College Degree?! | David Bahnsen #431 | The Way I Heard It
42.2K14 -
15:51
DeVory Darkins
1 day ago $14.14 earnedDemocrats suffer HUGE BLOW in court as contempt order paused
39.5K109 -
15:53
The Connect: With Johnny Mitchell
13 hours ago $6.86 earnedThe Truth About Ross Ulbricht & The Silk Road: How One Man Became The Internet's BIGGEST Drug Dealer
39.2K9 -
22:53
The Brett Cooper Show
2 days ago $9.34 earnedThe Rise of Fake Gurus & Online Cults | Episode 23
45.4K29 -
14:59:18
Total Horse Channel
17 hours agoWinnemucca Spring Spectacular Cow Horse Show and Derby - Saturday
46.2K5 -
LIVE
After Work Gaming
6 hours ago $1.34 earned“🍺 SOJU SATURDAY “🍺 THE FINALS SEASON 6
141 watching -
19:01
Russell Brand
13 hours agoTheo Von Just SHOCKED EVERYBODY With His Take on Trump Tariffs
94K107 -
14:22
Tundra Tactical
7 hours ago $5.07 earnedTRUMP DOJ Decides CZ Scorpion Is A Decepticon.
43.3K14 -
4:53:37
Culturama Podcast
5 hours ago $3.62 earnedEaster Special! Hollow Knight Stream!
33.3K4 -
1:04:29
Sarah Westall
8 hours agoThe Philippines and Massive Stores of Gold for all the People of the World w/ Joseph Allain
57.1K43