Mercurial > skaapsteker
changeset 90:96ba88d66e41
Add forgetten player.py
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 04 Apr 2011 17:12:15 +0200 |
parents | a46fc252e4fa |
children | 58c080c11eee |
files | skaapsteker/sprites/player.py |
diffstat | 1 files changed, 84 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/skaapsteker/sprites/player.py Mon Apr 04 17:12:15 2011 +0200 @@ -0,0 +1,84 @@ +"""Class for dealing with the player""" + +import pygame.transform +import os + +from skaapsteker.sprites.base import TILE_SIZE +from skaapsteker.physics import Sprite +from skaapsteker.constants import Layers +from skaapsteker.data import get_files, load_image + + +class Player(Sprite): + + def __init__(self): + Sprite.__init__(self) + self.image = None + self._image_dict = {} + # State flags and such + self.running = False + self.jumping = False + self.flying = False + self._load_images() + # We muck with these in load for convience, so ensure they're right + self.tails = 0 + self.set_facing('left') + self.set_image() + self.set_pos((0, 0)) + self._layer = Layers.PLAYER + + def set_image(self): + key = self._make_key() + # TODO: handle animations + self.image = self._image_dict[key][0] + + def set_facing(self, new_facing): + self.facing = new_facing + + def set_pos(self, pos): + self.starting_tile_pos = pos + self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1])) + + + def dispatch(self, ev): + pass + + def _get_action(self): + if self.running: + return 'running' + if self.jumping: + return 'jumpin' + return 'standing' + + + def _make_key(self, action=None): + if action is None: + action = self._get_action() + tails = self.tails + if self.tails >= 4: + tails = 4 + elif self.tails >= 2: + tails = 2 + return '%s %s %d' % (action, self.facing, tails) + + def _load_images(self): + for action in ['standing', 'running', 'jumping']: + for tails in [0, 1, 2, 4]: + self.tails = tails + directory = os.path.join('sprites', + 'kitsune_%s' % action, + 'kitsune_%s_%dtail' % (action, tails)) + for facing in ['left', 'right']: + self.facing = facing + key = self._make_key(action) + self._image_dict[key] = [] + for image_file in get_files(directory): + if image_file.startswith('.'): + # Skip extra junk for now + continue + image = load_image(os.path.join(directory, image_file)) + if facing == 'right': + image = pygame.transform.flip(image, True, False) + self._image_dict[key].append(image) + +