From 063ec6d22b4003b68304fc35e1720af0677413bf Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Wed, 9 Jan 2013 08:41:34 -0430 Subject: [PATCH] Created the Actor and Animation classes. --- actor.py | 20 +++++++++++++++++++ animation.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ intro.py | 5 +++-- main.py | 1 + 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 actor.py create mode 100644 animation.py diff --git a/actor.py b/actor.py new file mode 100644 index 0000000..0f1f496 --- /dev/null +++ b/actor.py @@ -0,0 +1,20 @@ +########################################### +# Created on 1-9-2013. Miguel Angel Astor # +########################################### +import pygame +from animation import Animation + +class Actor: + """ Represents any game object. """ + def __init__(self): + """ Initializes the actor object. """ + # All sprites for an actor are stored as a list of pygame sprites. + self.sprites = [] + # Animations are stored as a key (animation name), + # value (list of sprite indices) pairs. + self.animations = {} + # Parameters. + self.angle = 0 + self.speed = [0, 0] # [X speed, Y speed]. + self.acceleration = [0, 0] # Ditto. + self.b_box = [0, 0] # [X length, Y length]. diff --git a/animation.py b/animation.py new file mode 100644 index 0000000..351f26c --- /dev/null +++ b/animation.py @@ -0,0 +1,55 @@ +########################################### +# Created on 1-9-2013. Miguel Angel Astor # +########################################### + +class Animation: + """ This class represents an animated sprite. No sprite information is + stored in this class, only sprite indexes. """ + def __init__(self, name): + """ Initializes an empty animation. """ + self.fps = 0 + self.name = name + self.looping = False + self.current_frame = 0 + self.frames = [] + + def __str__(self): + """ Returns a string representation of this animation. """ + return "Animation :: Name: " + self.name + " :: FPS: " + str(self.fps) + + " :: Looping : " + str(self.looping) + " :: Current frame: " + + str(self.current_frame) + + def add_frame(self, frame): + """ Adds a frame to this animation. Frames should be added in order. """ + self.frames.append(frame) + + def get_next_frame(self): + """ Returns the current frame of the animation. Moves the current frame + count by one. """ + if(self.looping): + self.current_frame = (self.current_frame + 1) % len(self.frames) + elif (self.current_frame == len(self.frames) - 1): + pass + return self.frames[self.current_frame] + + def get_current_frame(self): + """ Returns the current frame of the animation. """ + return self.current_frame + + def set_current_frame(self, frame): + """ Changes the current frame of the animation. """ + # The frame is wrapped modulo the number of frames to ensure it is valid. + self.current_frame = frame % len(self.frames) + + def get_looping(self): + """ Returns wether this animation should loop or not. """ + return self.looping + + def set_looping(self, looping): + self.looping = looping + + def get_frames_per_second(self): + return self.fps + + def set_frames_per_second(self, fps): + self.fps = fps diff --git a/intro.py b/intro.py index cdf4d72..34cfbe3 100644 --- a/intro.py +++ b/intro.py @@ -1,5 +1,6 @@ -# Miguel Angel Astor Romero. Created on 7-1-2013. # -################################################### +########################################### +# Created on 1-7-2013. Miguel Angel Astor # +########################################### import pygame try: diff --git a/main.py b/main.py index b7e76fd..3c75537 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python ########################################### # Created on 1-7-2013. Miguel Angel Astor # ###########################################