I made an explosion animation using PyGame with Spritesheets. The following code serves as an example which shows how to use sprite sheets for making animations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys | |
import pygame | |
from pygame.constants import QUIT, K_ESCAPE, KEYDOWN | |
def sprite( w, h ): | |
animation_frames = [] | |
timer = pygame.time.Clock() | |
screen = pygame.display.set_mode( ( 200, 200 ), 0, 32 ) | |
image = pygame.image.load( "BombExploding.png" ).convert_alpha() | |
width, height = image.get_size() | |
for i in range( int( width / w ) ): | |
animation_frames.append( image.subsurface( ( i * w, 0, w, h ) ) ) | |
counter = 0 | |
while True: | |
for evt in pygame.event.get(): | |
if evt.type == QUIT or ( evt.type == KEYDOWN and evt.key == K_ESCAPE ) : | |
sys.exit() | |
screen.fill( ( 27, 27, 27 ) ) | |
screen.blit( animation_frames[counter], ( 90, 60 ) ) | |
counter = ( counter + 1 ) % 13 | |
pygame.display.update() | |
timer.tick( 10 ) | |
if __name__ == "__main__": | |
sprite( 32, 64 ) |
Output