From 12b849c6686d72250efa04d8b3445f2b7e936a32 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Wed, 8 Aug 2012 16:46:26 +0200 Subject: [PATCH] Added a rolling boulder. --- robotgame/boulder.py | 64 ++++++++++++++++++++++++++++++++++++++++++++ robotgame/level1.py | 28 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 robotgame/boulder.py diff --git a/robotgame/boulder.py b/robotgame/boulder.py new file mode 100644 index 0000000..73047da --- /dev/null +++ b/robotgame/boulder.py @@ -0,0 +1,64 @@ +# 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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# boulder.py +# -------------------- +# date created : Wed Aug 8 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +A rolling boulder. +""" + +import pygame + +import worldobject + +class Boulder(worldobject.WorldObject): + def __init__(self, level, x, y, movable=True, rolling=True): + self.__dict__.update(locals()) + worldobject.WorldObject.__init__(self, level, x, y, + movable=movable, speed=1) + + self.anim = 'boulder_right' + self.frame = 0 + self.anim_speed = 30 + + def update(self, e, t, dt): + # Update the animation + self.frame = ((self.frame + self.anim_speed * dt) % + len(self.level.imgs[self.anim])) + + if self.direction == (1, 0): + self.anim = 'boulder_right' + if self.direction == (0, 1): + self.anim = 'boulder_down' + if self.direction == (-1, 0): + self.anim = 'boulder_left' + if self.direction == (0, -1): + self.anim = 'boulder_up' + + self.move(*self.direction) + + worldobject.WorldObject.update(self, e, t, dt) + + def draw(self, window): + self.img = self.level.imgs[self.anim][int(self.frame)] + window.blit(self.img, (self.x - 32 - self.level.camera_x, + self.y - self.img.get_size()[1] + 24 + - self.level.camera_y)) + diff --git a/robotgame/level1.py b/robotgame/level1.py index 583551b..2438276 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -27,11 +27,13 @@ The first level. import os import pygame import random +import re import level import player import tile import block +import boulder class Level1(level.Level): def __init__(self, game, graphics_dir, paused=False): @@ -50,6 +52,8 @@ class Level1(level.Level): self.imgs['block1'], movable=True)) + self.objects.append(boulder.Boulder(self, 64, 48)) + def load(self): """Load all resources used in the level.""" tile_list = ['ground1', 'ground2'] @@ -63,5 +67,29 @@ class Level1(level.Level): self.imgs[block] = pygame.image.load(os.path.join( self.graphics_dir, 'blocks', '%s.png' % block)) + # Load animations + for anim, directory in ( + [('boulder_up', os.path.join('boulder', 'up')), + ('boulder_down', os.path.join('boulder', 'down')), + ('boulder_right', os.path.join('boulder', 'right')), + ('boulder_left', os.path.join('boulder', '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.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) + + self.imgs[anim].append(img) + def restart(self): self.player.reset_pos()