ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:part2:pygame:step008

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:part2:pygame:step008 [2010/06/15 11:19]
Horst JENS
— (current)
Line 1: Line 1:
-^ [[:​en:​part2:​pygame:​step007| ← previous]] ^ [[en:​part2:​pygame:​start| ↑ Overview]] ^ [[:​en:​part2:​pygame:​step009| → next ]] ^ 
-====== step 008 - animation and sprite sheet ====== 
- 
-Remember the Subsurfaces from the last step ? And remember that the snake had always the same grinning facial expression ? Now, let's make some animated **sprites**((a 2d-object moving around is referred as a [[wp>​Sprite_(computer_graphics)]])) - not pygame sprites, but surfaces that show different images of an animation cycle, like a bird flapping it wings. 
- 
-{{ :​en:​part2:​spritesheet008.jpg?​300|}} 
-Instead of painting all the single pictures of an animation cycle myself i prefer to use ready-made sprite sheets. 
-In the data folder, you will find a spritesheet((a graphic file with lot's of sprites)) from Ari Feldmann Spritelib ( http://​www.flyingyogi.com ) showing several pictures of an animal. If you use Ari's sprite'​s,​ make sure to include his [[en:​resources:​licenses:​spritelib|license information]] into your program. 
- 
-With the help of pygame'​s subsurface command, i will show you how to extract single images from the sprite sheet and use them. 
- 
- 
-==== discussion ==== 
- 
- 
- 
- 
- 
- 
-==== additional resources ==== 
-{{:​en:​part2:​data008.tar.gz|}} 
-==== source code ==== 
- 
-<code python step008.py>​ 
-# -*- coding: utf-8 -*- 
-"""​ 
-step008.py 
-animation & spritesheet 
-url: http://​thepythongamebook.com/​en:​part2:​pygame:​step008 
-author: horst.jens@spielend-programmieren.at 
- 
-spritesheet from 
-http://​www.flyingyogi.com 
-"""​ 
-import pygame 
-import random 
-import os 
-pygame.init() 
-screen=pygame.display.set_mode((800,​480)) # try out larger values and see what happens ! 
-screenrect = screen.get_rect() 
-background = pygame.Surface((screen.get_size())) 
-backgroundrect = background.get_rect() 
-background.fill((255,​255,​255)) # fill white 
-background = background.convert() 
-screen.blit(background,​(0,​0)) 
-# mypicture = pygame.image.load("​picturefile.jpg"​) # simple method if picture in same folder 
-folder = "​data"​ # replace with "​."​ if pictures lay in the same folder as program 
-spritesheet = pygame.image.load(os.path.join(folder,​ "​char9.bmp"​)).convert() # all in one line 
-lions = [] # a list for the lion images 
-# the spritesheet has lions, 128 x 64 pixels 
-for nbr in range(1,​5,​1):​ # first line contains 4 picutres of lions 
-   ​lions.append(spritesheet.subsurface((127*(nbr-1),​64,​127,​127))) 
-for nbr in range(5,​7,​1):​ # second line contains 2 pictures of lions 
-   ​lions.append(spritesheet.subsurface((127*(nbr-5),​262-64,​127,​127))) 
-print "​len:",​len(lions) 
- 
-for nbr in range(len(lions)):​ 
-   ​lions[nbr].set_colorkey((0,​0,​0)) # black transparent 
-   ​lions[nbr] = lions[nbr].convert_alpha() 
-   print "​converted nbr", nbr 
- 
-for nbr in range(len(lions)):​ 
-    screen.blit(lions[nbr],​ (nbr*127, 0))  #blit the ball surface on the screen (on top of background) 
-    print "​blitted nbr", nbr 
- 
-screen.blit(lions[nbr],​ (nbr*127, 0))  #blit the ball surface on the screen (on top of background) 
-#​screen.blit(lions[1],​ (x,  
-clock = pygame.time.Clock() ​       #create pygame clock object 
-mainloop = True 
-FPS = 60                           # desired max. framerate in frames per second. ​ 
-playtime = 0 
-cycletime = 0  
-newnr = 0 # index of the first lionimage to display 
-oldnr = 0 # needed to compare if image has changed 
-interval = .15 # how long one single images should be displayed in seconds ​ 
-while mainloop: 
-    milliseconds = clock.tick(FPS) ​ # milliseconds passed since last frame 
-    seconds = milliseconds / 1000.0 # seconds passed since last frame (float) 
-    playtime += seconds 
-    cycletime += seconds 
-    if cycletime > interval: 
-        cycletime = 0 
-        newnr += 1 
-    picnr = int(newnr % 5) # the remainder of a division by 5, because 6 pictures are present (0...5) 
-    mypicture = lions[picnr] 
-    if newnr > oldnr: 
-        #print "​change to picture nr %i" % picnr 
-        # clean dirty rec (a bit larger than original rect): 
-        screen.blit(background.subsurface((300,​300,​128,​66)),​(300,​300)) ​ 
-        # blit new lion picture: 
-        screen.blit(mypicture,​ (300,​300)) ​ 
-    oldnr = newnr # save oldnr  
-    ​ 
-    for event in pygame.event.get():​ 
-        if event.type == pygame.QUIT:​ 
-            mainloop = False # pygame window closed by user 
-        elif event.type == pygame.KEYDOWN:​ 
-            if event.key == pygame.K_ESCAPE:​ 
-                mainloop = False # user pressed ESC 
-  
-    pygame.display.set_caption("​[FPS]:​ %.2f picture: %i" % (clock.get_fps(),​ picnr)) 
-    #this would repaint the whole screen (secure, but slow) 
-    #​screen.blit(background,​ (0,​0)) ​    #draw background on screen (overwriting all) 
- 
-    pygame.display.flip() ​         # flip the screen 30 times a second 
-print "This '​game'​ was played for %.2f seconds"​ % playtime 
- 
-</​code>​ 
- 
-^ [[:​en:​part2:​pygame:​step007| ← previous]] ^ [[en:​part2:​pygame:​start| ↑ Overview]] ^ [[:​en:​part2:​pygame:​step009| → next ]] ^ 
  
/var/www/horst/thepythongamebook.com/data/attic/en/part2/pygame/step008.1276593556.txt.gz · Last modified: 2014/01/09 11:07 (external edit)