From 1d838ef5d19bd115938f8e4de73b4abc75d97b35 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Thu, 9 Aug 2012 16:00:27 +0200 Subject: [PATCH] Added trigger pads/holes and implemented them into level1. --- robotgame/level1.py | 64 +++++++++++++++++++++++++++++++------------- robotgame/trigger.py | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 18 deletions(-) create mode 100644 robotgame/trigger.py diff --git a/robotgame/level1.py b/robotgame/level1.py index d040128..6b9aeea 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -35,6 +35,7 @@ import tile import block import boulder import lever +import trigger import logic.rollingstone @@ -44,6 +45,8 @@ class Level1(level.Level): 48 * 100), paused=paused) + self.task_completions = [] + self.dimensions = 50, 50 for i in range(self.dimensions[0]): @@ -179,6 +182,13 @@ class Level1(level.Level): task2_pos[1] + 48 * (task2_size[1]), 'moat_end_horizontal_flip') + self.objects.append( + trigger.Trigger(self, task2_pos[0] + 64 * (task2_size[0]), + task2_pos[1] + 48 * (task2_size[1] - 1), + [self.complete_task], + self.imgs['hole'], [b], + signal=[0, 2])) + ### Task 4: Inverted bits task4_pos = (64 * 13, 48 * 13) @@ -231,6 +241,12 @@ class Level1(level.Level): risingblocks[i].set_init_pos() risingblocks[n].set_init_pos() + self.objects.append(trigger.Trigger(self, task4_pos[0] + 64 * 8, + task4_pos[1] - 48 * 3, + [self.complete_task], + self.imgs['hole'], [b], + signal=[0, 4])) + # Moat self.add_moat(task4_pos[0] - 64 * 2, task4_pos[1] - 48 * 4, 'moat_corner_north') @@ -260,27 +276,31 @@ class Level1(level.Level): def load(self): """Load all resources used in the level.""" - tile_list = ['ground1', 'ground2'] + l = ['ground1', 'ground2'] + for o in l: + self.imgs[o] = pygame.image.load(os.path.join( + self.graphics_dir, 'tiles', '%s.png' % o)) - for tile in tile_list: - self.imgs[tile] = pygame.image.load(os.path.join( - self.graphics_dir, 'tiles', '%s.png' % tile)) + l = ['block1'] + for o in l: + self.imgs[o] = pygame.image.load(os.path.join( + self.graphics_dir, 'blocks', '%s.png' % o)) - block_list = ['block1'] - for block in block_list: - self.imgs[block] = pygame.image.load(os.path.join( - self.graphics_dir, 'blocks', '%s.png' % block)) + l = ['hole'] + for o in l: + self.imgs[o] = pygame.image.load(os.path.join( + self.graphics_dir, '%s.png' % o)) - moat_list = ['moat_corner_north', - 'moat_corner_south', - 'moat_corner_north_flip', - 'moat_corner_south_flip', - 'moat_end_horizontal', - 'moat_horizontal', - 'moat_vertical'] - for moat in moat_list: - self.imgs[moat] = pygame.image.load(os.path.join( - self.graphics_dir, 'moat', '%s.png' % moat)) + l = ['moat_corner_north', + 'moat_corner_south', + 'moat_corner_north_flip', + 'moat_corner_south_flip', + 'moat_end_horizontal', + 'moat_horizontal', + 'moat_vertical'] + for o in l: + self.imgs[o] = pygame.image.load(os.path.join( + self.graphics_dir, 'moat', '%s.png' % o)) # Special treatment self.imgs['moat_end_horizontal_flip'] = pygame.transform.flip( @@ -322,6 +342,14 @@ class Level1(level.Level): self.imgs[anim].append(img) + def complete_task(self, task): + if task == 0: + return + self.task_completions.append(task) + if len(self.task_completions) > 3: + self.task_completions.remove(self.task_completions[0]) + print(self.task_completions) + def restart(self): for obj in self.objects: obj.reset_pos() diff --git a/robotgame/trigger.py b/robotgame/trigger.py new file mode 100644 index 0000000..5ae18b8 --- /dev/null +++ b/robotgame/trigger.py @@ -0,0 +1,61 @@ +# 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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# trigger.py +# -------------------- +# date created : Thu Aug 9 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +A generic trigger pad. +""" + +import pygame + +import worldobject + +class Trigger(worldobject.WorldObject): + def __init__(self, level, x, y, links, img, trigger_objs, + toggling=False, signal=[0, 1], + setting=False): + self.__dict__.update(locals()) + worldobject.WorldObject.__init__(self, level, x, y, z=-48, + blocking=False) + + self.frame = 0 + self.anim_speed = 15 + + def trigger(self, setting): + if self.setting != setting: + self.setting = setting + + for link in self.links: + link(self.signal[setting]) + + def update(self, e, t, dt): + for obj in self.trigger_objs: + if self.x == obj.x and self.y == obj.y: + self.trigger(True) + break + self.trigger(False) + + worldobject.WorldObject.update(self, e, t, dt) + + def draw(self, window): + window.blit(self.img, (self.x - 32 - self.level.camera_x, + self.y - self.img.get_size()[1] + 24 + - self.level.camera_y))