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
-
11:47
Liberty Hangout
1 day agoAnti-Trump Beta FARTS On Me Instead of Debating
34.9K54 -
1:37:20
FreshandFit
3 hours agoShe’s Not Your Girl It’s Just Your Turn
16.3K3 -
5:26:18
Akademiks
5 hours agoEbro has MELTDOWN after Drake tells him to *** SLOWLY! Radio Over? NLE vs Youngboy part 805?
17.6K -
1:35:27
Inverted World Live
7 hours agoLost Satellites, Wild Horses, and 3i/Atlas
126K3 -
2:53:42
TimcastIRL
5 hours agoCandace Owens IMPLODES, Audience IN REVOLT, Claim SHES A CLONE Or GOT THE CALL | Timcast IRL
262K193 -
2:49:53
Barry Cunningham
5 hours agoLIVE BREAKING NEWS: President Trump Celebrates Hanukkah! And More News
44.3K9 -
1:29:40
Anthony Rogers
12 hours agoEpisode 394 - Isaac Butterfield
18.9K1 -
8:02
China Uncensored
11 hours agoChina Just Took The First Step Towards WAR
23.6K14 -
1:20:04
Flyover Conservatives
23 hours agoWhy Did Jesus Really Come? It’s NOT What You Think - Pedro Adao | FOC Show
24.6K2 -
LIVE
DLDAfterDark
3 hours ago $1.28 earnedYo Homie! Is That My Briefcase?? EDC & Gun Talk - Blue Waffle Giveaway Pre Stream
316 watching