# animation.py # Walker M. White (wmw2) # November 17, 2014 """Module to show off how animation works. We make a subclass of game, and override the update, init, and draw methods.""" import colormodel import random import math from game2d import * # Window Size WINDOW_WIDTH = 512 WINDOW_HEIGHT = 512 # Distance from Window Center ANIMATION_RADIUS = 100 # AMOUNT TO CHANGE THE ANGLE ANIMATION_STEP = 0.1 # Ellipse Radius ELLIPSE_RADIUS = 20 class Animation(GameApp): """Application to animate an ellipse about a center. At each step, the update() method computes a new angle. It finds the (x,y) coordinate that corresponds to the polar coordinate (ANIMATION_RADIUS,angle) and puts the ellipse there. Instance Attributes (Not hiding any): view [GView]: the view (inherited from Game) angle [float]: ellipse angle from center ellipse [GEllipse, center is (RADIUS,self.angle) in polar coords]: the ellipse to animate """ def init(self): """Special initialization method to create attributes. Not the SAME as __init__. DO NOT DEFINE __init___.""" self.angle = 0 pos=self._polar_to_coord(ANIMATION_RADIUS,self.angle) self.ellipse = GEllipse(x=pos[0],y=pos[1], width=ELLIPSE_RADIUS,height=ELLIPSE_RADIUS, fillcolor=colormodel.RED) def update(self,dt): """Animate the ellipse.""" # Change the angle self.angle = self.angle+ANIMATION_STEP % (2*math.pi) pos=self._polar_to_coord(ANIMATION_RADIUS,self.angle) #self.ellipse.x = pos[0] #self.ellipse.y = pos[1] def draw(self): """Draw the ellipse""" self.ellipse.draw(self.view) def _polar_to_coord(self,r,a): """Return: Tuple (x,y) equal to polar coord (r,a)""" return (r*math.cos(a)+self.width/2.0,r*math.sin(a)+self.height/2.0) # Application Code if __name__ == '__main__': Animation(width=WINDOW_WIDTH,height=WINDOW_HEIGHT,fps=60.0).run()