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 [2011/01/23 06:51]
Horst JENS [step 008 - animation]
en:part2:pygame:step008 [2011/11/04 06:43]
imapiekindaguy
Line 1: Line 1:
 ^ [[:​en:​part2:​pygame:​step007| ← previous]] ^ [[en:​part2:​pygame:​start| ↑ Overview]] ^ [[:​en:​part2:​pygame:​step009| → next ]] ^ ^ [[:​en:​part2:​pygame:​step007| ← previous]] ^ [[en:​part2:​pygame:​start| ↑ Overview]] ^ [[:​en:​part2:​pygame:​step009| → next ]] ^
-====== ​step 008 - animation ​======+====== ​Step 008 - Animation ​======
  
-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.+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 ​its wings.
  
 {{ :​en:​part2:​spritesheet008.jpg?​300|}} {{ :​en:​part2:​spritesheet008.jpg?​300|}}
-Instead of painting all the single pictures of an animation cycle myself ​prefer to use ready-made sprite sheets+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:​glossary:​spritelib-license|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.+In the data folder, you will find a spritesheet((a graphic file with lots of sprites)) from Ari Feldmann'​s ​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:​glossary:​s:​spritelib_license|license information]] in 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 ==== 
  
 +==== Code Discussion ====
  
  
Line 18: Line 18:
  
  
-==== 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 +===== Source Code on Github =====
-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)): +To run this example you need:
-   ​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)):​ +^  file  ^  ​in folder ​ ^  download ​ ^ 
-    ​screen.blit(lions[nbr], (nbr*127, 0))  #blit the ball surface on the screen (on top of background) +|  ​[[https://​github.com/​horstjens/​ThePythonGameBook/​raw/​master/​pygame/​008_animation.py|008_animation.py]]  ​|  ''​pygame'' ​ |  Download ​the whole Archive with all files from Github: ​ \\  https://​github.com/​horstjens/​ThePythonGameBook/​archives/​master ​ | 
-    print "​blitted nbr", nbr+|  [[https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​data/​char9.bmp|char9.bmp]] \\ [[https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​data/​char9.bmp|{{:​en:​part2:​pygame:​char9_150.jpg|Sprite sheet by Ari Feldman}}]] ​ |  ''​pygame/​data'' ​ |  :::  |
  
-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 % 6) # 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 +View/​Edit/​Download the file directly in Github: https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​008_animation.py
-print "This '​game'​ was played for %.2f seconds"​ % playtime+
  
-</code>+//click reload in your browser if you see no code here:// 
 +<​html>​ 
 + <​script src="​http://​gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​pygame/​008_animation.py">​ 
 + </​script></​html> 
 + 
 + 
 +====== Comment this Page ====== 
 +~~DISQUS~~
  
 ^ [[:​en:​part2:​pygame:​step007| ← previous]] ^ [[en:​part2:​pygame:​start| ↑ Overview]] ^ [[:​en:​part2:​pygame:​step009| → next ]] ^ ^ [[:​en:​part2:​pygame:​step007| ← previous]] ^ [[en:​part2:​pygame:​start| ↑ Overview]] ^ [[:​en:​part2:​pygame:​step009| → next ]] ^