It's awesome!

This commit is contained in:
Sakse Dalum 2012-08-08 18:13:53 +02:00
parent 0ac3baa8f5
commit ce84def0e0
4 changed files with 80 additions and 26 deletions

View File

@ -42,6 +42,7 @@ class Block(worldobject.WorldObject):
self.set_alpha(1) self.set_alpha(1)
def use(self, obj): def use(self, obj):
if self.movable:
if obj == self.holder: if obj == self.holder:
obj.holding = None obj.holding = None
self.holder = None self.holder = None
@ -55,17 +56,27 @@ class Block(worldobject.WorldObject):
- self.level.camera_y)) - self.level.camera_y))
class ArrowBlock(Block): class ArrowBlock(Block):
def __init__(self, level, x, y, anim='arrow_up', movable=True, is_up=True): def __init__(self, level, x, y, direction, movable=True, is_up=True):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, movable=movable) worldobject.WorldObject.__init__(self, level, x, y,
direction=direction, movable=movable)
self.frame = 0 self.frame = 0
self.anim_speed = 15 self.anim_speed = 15
self.setting = False self.setting = False
if direction == (1, 0):
self.anim = 'arrow_right'
if direction == (-1, 0):
self.anim = 'arrow_left'
if direction == (0, -1):
self.anim = 'arrow_up'
if direction == (0, 1):
self.anim = 'arrow_down'
def activate(self, setting): def activate(self, setting):
self.movable = True self.movable = True
if self.check_move(0, 0): if not self.share_tile(worldobject.WorldObject):
self.is_up = not setting self.is_up = not setting
self.movable = False self.movable = False

View File

@ -27,19 +27,28 @@ A rolling boulder.
import pygame import pygame
import worldobject import worldobject
import block
class Boulder(worldobject.WorldObject): class Boulder(worldobject.WorldObject):
def __init__(self, level, x, y, movable=True, rolling=True): def __init__(self, level, x, y, movable=True, rolling=False):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, worldobject.WorldObject.__init__(self, level, x, y,
movable=movable, speed=1) movable=movable, speed=4)
self.anim = 'boulder_right' self.anim = 'boulder_right'
self.frame = 0 self.frame = 0
self.anim_speed = 30 self.anim_speed = 30
def activate(self, setting):
self.rolling = True
def reset_pos(self):
worldobject.WorldObject.reset_pos(self)
self.rolling = False
def update(self, e, t, dt): def update(self, e, t, dt):
# Update the animation # Update the animation
if self.rolling:
self.frame = ((self.frame + self.anim_speed * dt) % self.frame = ((self.frame + self.anim_speed * dt) %
len(self.level.imgs[self.anim])) len(self.level.imgs[self.anim]))
@ -52,7 +61,13 @@ class Boulder(worldobject.WorldObject):
if self.direction == (0, -1): if self.direction == (0, -1):
self.anim = 'boulder_up' self.anim = 'boulder_up'
self.move(*self.direction) tile_sharer = self.share_tile(block.ArrowBlock)
if tile_sharer:
self.direction = tile_sharer.direction
if self.rolling:
if not self.move(*self.direction) and not self.is_moving:
self.reset_pos()
worldobject.WorldObject.update(self, e, t, dt) worldobject.WorldObject.update(self, e, t, dt)

View File

@ -53,16 +53,22 @@ class Level1(level.Level):
# self.imgs['block1'], # self.imgs['block1'],
# movable=True)) # movable=True))
self.objects.append(boulder.Boulder(self, 64, 48)) b = boulder.Boulder(self, 64, 48)
arrow_block = block.ArrowBlock(self, 64, 0) self.objects.append(b)
lever1 = lever.Lever(self, 128, 48*2, [arrow_block.activate], self.objects.append(lever.Lever(self, 64*5, 48*7, [b.activate]))
toggling=True)
self.objects.extend([lever1, arrow_block]) arrow_blocks = [block.ArrowBlock(self, 64*4, 48, (1, 0)),
self.objects.append(lever.Lever(self, 128, 48*3, [], toggling=False)) block.ArrowBlock(self, 64*4, 48*2, (-1, 0)),
self.objects.append(lever.Lever(self, 128*2, 48*3, [], toggling=False, block.ArrowBlock(self, 64*5, 48*2, (0, 1)),
anim='lever_updown')) block.ArrowBlock(self, 64*5, 48, (0, -1))]
self.objects.append(lever.Lever(self, 128*2, 48*5, [], toggling=True,
anim='lever_updown')) self.objects.extend(arrow_blocks)
self.objects.append(lever.Lever(self,
128,
48*2,
[arrow_block.activate
for arrow_block in arrow_blocks],
toggling=True))
def load(self): def load(self):
"""Load all resources used in the level.""" """Load all resources used in the level."""
@ -107,7 +113,12 @@ class Level1(level.Level):
for f in sorted(anim_files): for f in sorted(anim_files):
img = pygame.image.load(f) img = pygame.image.load(f)
# Special treatment:
if anim == 'arrow_left':
img = pygame.transform.flip(img, 1, 0)
self.imgs[anim].append(img) self.imgs[anim].append(img)
def restart(self): def restart(self):
self.player.reset_pos() for obj in self.objects:
obj.reset_pos()

View File

@ -31,11 +31,12 @@ import copy
class WorldObject(object): class WorldObject(object):
def __init__(self, level, x, y, z=0, direction=(1, 0), speed=4, def __init__(self, level, x, y, z=0, direction=(1, 0), speed=4,
tile_x=64, tile_y=48, tile_x=64, tile_y=48,
movable=False, blocking=True): movable=False, blocking=True, is_moving=False):
self.__dict__.update(locals()) self.__dict__.update(locals())
self.init_x = self.move_x = self.x = self.x - (self.x % self.tile_x) self.init_x = self.move_x = self.x = self.x - (self.x % self.tile_x)
self.init_y = self.move_y = self.y = self.y - (self.y % self.tile_y) self.init_y = self.move_y = self.y = self.y - (self.y % self.tile_y)
self.init_direction = self.direction
self.holding = None self.holding = None
self.holder = None self.holder = None
@ -45,9 +46,20 @@ class WorldObject(object):
def set_init_pos(self): def set_init_pos(self):
self.init_x, self.init_y = self.x, self.y self.init_x, self.init_y = self.x, self.y
self.init_direction = self.direction
def reset_pos(self): def reset_pos(self):
self.x, self.y = self.move_x, self.move_y = self.init_x, self.init_y self.x, self.y = self.move_x, self.move_y = self.init_x, self.init_y
self.direction = self.init_direction
def share_tile(self, obj_type):
for obj in self.level.objects:
if (obj.x - (obj.x % self.tile_x) == self.x - (self.x % self.tile_x)
and obj.y - (obj.y % self.tile_y) == self.y - (self.y
% self.tile_y)
and obj is not self and isinstance(obj, obj_type)):
return obj
return None
def check_move(self, move_x, move_y): def check_move(self, move_x, move_y):
if self.move_x == self.x and self.move_y == self.y and self.movable: if self.move_x == self.x and self.move_y == self.y and self.movable:
@ -73,6 +85,9 @@ class WorldObject(object):
if self.holding: if self.holding:
self.holding.move(move_x, move_y) self.holding.move(move_x, move_y)
return True
return False
def use(self, obj): def use(self, obj):
pass pass
@ -104,4 +119,6 @@ class WorldObject(object):
self.y += min(self.speed * dt * self.tile_y, self.y += min(self.speed * dt * self.tile_y,
abs(self.y - self.move_y)) abs(self.y - self.move_y))
self.is_moving = self.x != self.move_x or self.y != self.move_y
self.x, self.y = int(self.x), int(self.y) self.x, self.y = int(self.x), int(self.y)