Difference between revisions of "CSC111 Animating A Stick Figure"

From dftwiki3
Jump to: navigation, search
(10 Gif Images)
Line 6: Line 6:
 
</center>
 
</center>
  
 +
=Original Image=
 +
<br />
 +
<center>
 +
[[Image:walkerAnimatedGif.gif]]
 +
</center>
 +
<br />
 
=10 Gif Images=
 
=10 Gif Images=
 
[[Image:walk0.gif]]
 
[[Image:walk0.gif]]
Line 17: Line 23:
 
[[Image:walk8.gif]]
 
[[Image:walk8.gif]]
 
[[Image:walk9.gif]]
 
[[Image:walk9.gif]]
 +
 
=Source Code=
 
=Source Code=
 
<source lang="python">
 
<source lang="python">

Revision as of 18:34, 5 December 2011

--D. Thiebaut 21:49, 2 December 2011 (EST)



StickFigureCapture.png

Original Image


WalkerAnimatedGif.gif


10 Gif Images

Walk0.gif Walk1.gif Walk2.gif Walk3.gif Walk4.gif Walk5.gif Walk6.gif Walk7.gif Walk8.gif Walk9.gif

Source Code

from graphics import *
import time
import random

class Walker:

    def __init__( self, x, y, index=0 ):
        self.current = index
        self.image = None
        self.x  = x
        self.y  = y
        
    def move( self, dx, dy ):
        x, y = self.x, self.y
        if self.image != None:
            x = self.image.getAnchor().getX()
            y = self.image.getAnchor().getY()
            self.image.undraw()

        if dx > 0:
            self.current = ( self.current + 1 ) % 10
        else:
            self.current = ( self.current + 10 - 1 ) % 10
            
        self.image = Image( Point( x+dx, y+dy ),
                            "walk30_%d.gif" % self.current )
        self.image.draw( self.win )

    def draw( self, win ):
        self.win = win
        if self.image == None:
            self.image = Image( Point( self.x, self.y ),
                            "walk%d.gif" % self.current )
            self.image.draw( win )
                            
                            

def justOne():
    W = 800
    H = 600
    win = GraphWin( "walker", W, H )
    
    walker = Walker( W/2, H/ 2 )
    walker.draw( win )
    
    while True:
        walker.move( 5, 0 )
        time.sleep( 0.2 )

    win.getMouse()
    win.close()

def several():
    W = 800
    H = 600
    win = GraphWin( "walker", W, H )

    walkers = []
    for i in range( 4 ):
        walkers.append( Walker( W/2, 140 * i + 70, (i*2)%10 ) )

    for walker in walkers:
        walker.draw( win )
    
    while True:
        for i, walker in enumerate( walkers ):
            if i == 0:
                walker.move( -5, 0 )
            else:
                walker.move( 5+random.choice( [0,1,2,3] ), 0 )

    win.getMouse()
    win.close()

def main():
    #justOne()
    several()
                        
main()