Created the Actor and Animation classes.

This commit is contained in:
2013-01-09 08:41:34 -04:30
parent fa3c92dd2e
commit 063ec6d22b
4 changed files with 79 additions and 2 deletions

20
actor.py Normal file
View File

@@ -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].

55
animation.py Normal file
View File

@@ -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

View File

@@ -1,5 +1,6 @@
# Miguel Angel Astor Romero. Created on 7-1-2013. # ###########################################
################################################### # Created on 1-7-2013. Miguel Angel Astor #
###########################################
import pygame import pygame
try: try:

View File

@@ -1,3 +1,4 @@
#! /usr/bin/env python
########################################### ###########################################
# Created on 1-7-2013. Miguel Angel Astor # # Created on 1-7-2013. Miguel Angel Astor #
########################################### ###########################################