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
-
16:38
MetatronGaming
11 hours agoAnno 117 Pax Romana looks INCREDIBLE
65.2K6 -
9:26
MattMorseTV
1 day ago $17.27 earnedPam Bondi is in HOT WATER.
14.2K145 -
13:46
Nikko Ortiz
13 hours agoYour Humor Might Be Broken...
11K2 -
2:20:13
Side Scrollers Podcast
18 hours agoVoice Actor VIRTUE SIGNAL at Award Show + Craig’s HORRIBLE Take + More | Side Scrollers
49.4K13 -
18:49
GritsGG
14 hours agoI Was Given a Warzone Sniper Challenge! Here is What Happened!
10K -
19:02
The Pascal Show
1 day ago $0.74 earnedNOT SURPRISED! Pam Bondi Is Lying To Us Again About Releasing The Epstein Files
9.9K6 -
6:05
Blabbering Collector
17 hours agoRowling On Set, Bill Nighy To Join Cast, HBO Head Comments On Season 2 Of Harry Potter HBO!
12.4K2 -
57:44
TruthStream with Joe and Scott
2 days agoShe's of Love podcast & Joe:A co-Hosted interview, Mother and Daughter (300,000+Facebook page) Travel, Home School, Staying Grounded, Recreating oneself, SolarPunk #514
24.1K1 -
30:49
MetatronHistory
1 day agoThe Truth about Women Warriors Based on Facts, Evidence and Sources
23K12 -
2:59:08
FreshandFit
13 hours agoA Sugar Baby & A Feminist ALMOST Fight Each Other
259K54