Added a rolling boulder.

This commit is contained in:
Sakse Dalum 2012-08-08 16:46:26 +02:00
parent 868df222bf
commit 12b849c668
2 changed files with 92 additions and 0 deletions

64
robotgame/boulder.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# boulder.py
# --------------------
# date created : Wed Aug 8 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
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))

View File

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