From 4babce9f3ceaa9038607560106eeedc2f58299c8 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Sun, 12 Aug 2012 19:05:54 +0200 Subject: [PATCH] Holy smokes, level 1 almost finished! --- robotgame/block.py | 51 +++++++++++++++++++++++++++++++++++++++++++- robotgame/level1.py | 34 +++++++++++++++++++++++++++++ robotgame/loader.py | 4 +++- robotgame/trigger.py | 3 ++- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/robotgame/block.py b/robotgame/block.py index a34815d..6867348 100644 --- a/robotgame/block.py +++ b/robotgame/block.py @@ -39,7 +39,7 @@ class Block(worldobject.WorldObject): for i in range(width): for j in range(height): - if not i or not j : + if width != 1 or height != 1: self.level.objects.append( InvisBlock(self.level, self.x + 64 * i, @@ -236,3 +236,52 @@ class ColorWell(Block): self.y - self.img.get_size()[1] + 52 - self.level.camera_y)) Block.draw(self, window) + +class Door(Block): + def __init__(self, level, x, y, movable=False, setting=False): + self.__dict__.update(locals()) + worldobject.WorldObject.__init__(self, level, x, y, movable=movable) + + self.frame = 0 + self.anim_speed = 15 + + self.init_setting = self.setting + + self.anim = 'door' + + self.level.objects.append(InvisBlock(self.level, self.x + 64, self.y)) + self.level.objects.append(InvisBlock(self.level, self.x - 64, self.y)) + + def set_init_pos(self): + worldobject.WorldObject.set_init_pos(self) + self.init_setting = self.setting + + def reset_pos(self): + worldobject.WorldObject.reset_pos(self) + self.setting = self.init_setting + + def activate(self, setting): + self.setting = setting + + def update(self, e, t, dt): + # Update the animation + if self.setting: + self.frame = min(self.frame + self.anim_speed * dt, + len(self.level.imgs[self.anim]) - 1) + else: + self.frame = max(self.frame - self.anim_speed * dt, + 0) + + if self.frame == len(self.level.imgs[self.anim]) - 1: + self.blocking = False + else: + self.blocking = True + + worldobject.WorldObject.update(self, e, t, dt) + + def draw(self, window): + self.img = self.level.imgs[self.anim][int(self.frame)] + if self.visible: + window.blit(self.img, (self.x - (32 + 64) - 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 348915f..896e0ca 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -48,6 +48,11 @@ class Level1(level.Level): 48 * 100), paused=paused) + self.solution = range(1, 6) + random.shuffle(self.solution) + self.solution = self.solution[:3] + print self.solution + self.task_completions = [] self.dimensions = 50, 50 @@ -469,6 +474,35 @@ class Level1(level.Level): task5_pos[0] - 64 * (task5_size[1]), task5_pos[1] - (task5_size[0] / 2) * 48, [lambda *x: self.complete_task(5)])) + + ### Final: The door + + door_x = 22 + + for i in range(12): + self.add_tile(door_x * 64, + 48 * (5 + i), + 'indoor%d' % random.randint(1, 6), blocking=False) + for i in range(7): + self.add_tile((door_x - i - 1) * 64, + 48 * (5 + 11), + 'indoor%d' % random.randint(1, 6), blocking=False) + + + door = block.Door(self, 64 * door_x, 48 * 4) + self.objects.append(door) + self.objects.append( + lever.Lever(self, 64 * (door_x + 3), 48 * 4, + [lambda x: door.activate(x) + if self.task_completions == self.solution + else lambda *v: None], + anim='lever_updown', toggling=False)) + self.objects.append( + trigger.Trigger(self, 64 * door_x, 48 * 4, + [self.game.goto_level], + self.imgs['indoor1'], + [self.player])) + # DRAW THE BACKGROUND self.draw_background() diff --git a/robotgame/loader.py b/robotgame/loader.py index 1d861fe..7340b67 100644 --- a/robotgame/loader.py +++ b/robotgame/loader.py @@ -88,7 +88,9 @@ class Loader(object): ('stairs', 'stairs'), ('elevating_column', 'elevating_column'), - ('mirror', 'mirror')] + ('mirror', 'mirror'), + + ('door', 'door')] ): self.imgs[anim] = [] diff --git a/robotgame/trigger.py b/robotgame/trigger.py index 0448693..a2e39d1 100644 --- a/robotgame/trigger.py +++ b/robotgame/trigger.py @@ -52,7 +52,8 @@ class Trigger(worldobject.WorldObject): if self.x == obj.x and self.y == obj.y: self.trigger(True, obj) break - self.trigger(False, None) + if self.toggling: + self.trigger(False, obj) worldobject.WorldObject.update(self, e, t, dt)