# This file is part of ROBOTGAME # # ROBOTGAME is free software: you can redistribute it and/or modify it under the # terms of the GNU General Public License as published by the Free Software # Foundation, either version 3 of the License, or (at your option) any later # version. # # ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # ROBOTGAME. If not, see . # # ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # # player.py # -------------------- # date created : Tue Aug 7 2012 # copyright : (C) 2012 Sakse Dalum # maintained by : Sakse Dalum """ A generic level. """ import pygame import re import os import worldobject class Player(worldobject.WorldObject): def __init__(self, level, x, y, z=1, movable=True): self.__dict__.update(locals()) worldobject.WorldObject.__init__(self, level, x, y, z=z, movable=movable) self.anim_root = 'idle' self.anim = 'right' self.frame = 0 self.anim_speed = 15 self.load() def load(self): self.imgs = {} for anim, directory in ( [('idle_up', os.path.join('robot_idle', 'up')), ('idle_down', os.path.join('robot_idle', 'down')), ('idle_right', os.path.join('robot_idle', 'right')), ('idle_left', os.path.join('robot_idle', 'right')), ('carry_up', os.path.join('robot_carry', 'up')), ('carry_down', os.path.join('robot_carry', 'down')), ('carry_right', os.path.join('robot_carry', 'right')), ('carry_left', os.path.join('robot_carry', 'right'))] ): self.imgs[anim] = [] # Find all image files for the given animation anim_files = [] for root, dirs, files in os.walk(os.path.join( self.level.graphics_dir, directory)): for f in files: if re.match(r"^.*\.(png)$", '/'.join([root, f])): anim_files.append('/'.join([root, f])) # Sort and load the files for f in sorted(anim_files): img = pygame.image.load(f) # Special treatment: if anim in ['idle_left', 'carry_left']: img = pygame.transform.flip(img, 1, 0) self.imgs[anim].append(img) self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)] def touch(self, touch_x, touch_y): for obj in self.level.objects: if (obj.x == self.x + touch_x * self.tile_x and obj.y == self.y + touch_y * self.tile_y and obj != self): obj.use(self) def update(self, e, t, dt): for event in e: if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: self.touch(*self.direction) keys = pygame.key.get_pressed() if keys[pygame.K_UP]: if not self.holding: self.direction = (0, -1) self.anim = 'up' self.move(0, -1) if keys[pygame.K_DOWN]: if not self.holding: self.direction = (0, 1) self.anim = 'down' self.move(0, 1) if keys[pygame.K_RIGHT]: if not self.holding: self.direction = (1, 0) self.anim = 'right' self.move(1, 0) if keys[pygame.K_LEFT]: if not self.holding: self.direction = (-1, 0) self.anim = 'left' self.move(-1, 0) # Update the animation self.frame = ((self.frame + self.anim_speed * dt) % len(self.imgs[self.anim_root + '_' + self.anim])) worldobject.WorldObject.update(self, e, t, dt) def draw(self, window): self.anim_root = 'carry' if self.holding else 'idle' self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)] self.img.set_alpha(128) window.blit(self.img, (self.x - 32 - self.level.camera_x, self.y - self.img.get_size()[1] + 24 - self.level.camera_y))