Sunday, June 21, 2015

Explosion Animation using Spritesheets in PyGame

[Image: odTjucw.gif]

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.

#! /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 )
SpriteSheet Used

[Image: w7RG3qc.png]


Output

[Image: odTjucw.gif]


Related Posts:

Follow Me!

Blog Archive

Followers

Visitor Map