From d6ec520e2155aa63f150902e66fb48895306082a Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Fri, 10 Aug 2012 18:54:48 +0200 Subject: [PATCH 1/8] Added loader object. Game now loads on startup. --- robotgame/game.py | 3 + robotgame/level.py | 4 +- robotgame/level1.py | 73 ----------------- robotgame/loader.py | 186 ++++++++++++++++++++++++++++++++++++++++++++ robotgame/player.py | 85 +------------------- 5 files changed, 192 insertions(+), 159 deletions(-) create mode 100644 robotgame/loader.py diff --git a/robotgame/game.py b/robotgame/game.py index 8aa7692..80fe3ba 100644 --- a/robotgame/game.py +++ b/robotgame/game.py @@ -32,6 +32,7 @@ import jukebox import level import main_menu import game_menu +import loader class Game(object): """Create an object to handle the game.""" @@ -56,6 +57,8 @@ class Game(object): def load(self): graphics_dir = os.path.join(self.directory, "resources", "graphics") + self.loader = loader.Loader(graphics_dir) + self.loader.load() self.level = None self.jukebox = jukebox.Jukebox( diff --git a/robotgame/level.py b/robotgame/level.py index b7f1f55..5975dbc 100644 --- a/robotgame/level.py +++ b/robotgame/level.py @@ -35,9 +35,7 @@ class Level(object): self.tiles = [] self.objects = [] - self.imgs = {} - - self.load() + self.imgs = game.loader.imgs self.reverse_imgs = ( dict([(val, key) for (key, val) diff --git a/robotgame/level1.py b/robotgame/level1.py index f2e3d26..1704342 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -338,79 +338,6 @@ class Level1(level.Level): if blocking: self.objects.append(block.InvisBlock(self, x, y)) - def load(self): - """Load all resources used in the level.""" - l = ['ground1', 'ground2'] - for o in l: - self.imgs[o] = pygame.image.load(os.path.join( - self.graphics_dir, 'tiles', '%s.png' % o)) - - for o in range(1, 7): - self.imgs['indoor%d' % o] = pygame.image.load(os.path.join( - self.graphics_dir, 'tiles', 'indoor', 'ground%02d.png' % o)) - - l = ['block1', 'block1_lifted'] - for o in l: - self.imgs[o] = pygame.image.load(os.path.join( - self.graphics_dir, 'blocks', '%s.png' % o)) - - l = ['hole', 'well'] - for o in l: - self.imgs[o] = pygame.image.load(os.path.join( - self.graphics_dir, '%s.png' % o)) - - 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( - self.imgs['moat_end_horizontal'], 1, 0) - - # 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')), - ('boulder_falling', os.path.join('boulder_fall')), - - ('lever_updown', os.path.join('lever', 'down-up')), - ('lever_leftright', os.path.join('lever', 'left-right')), - - ('arrow_up', os.path.join('matt', 'up')), - ('arrow_right', os.path.join('matt', 'right')), - ('arrow_down', os.path.join('matt', 'down')), - ('arrow_left', os.path.join('matt', '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) - - # Special treatment: - if anim == 'arrow_left': - img = pygame.transform.flip(img, 1, 0) - - self.imgs[anim].append(img) - def complete_task(self, task): if task == 0: return diff --git a/robotgame/loader.py b/robotgame/loader.py new file mode 100644 index 0000000..23a595e --- /dev/null +++ b/robotgame/loader.py @@ -0,0 +1,186 @@ +# 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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# loader.py +# -------------------- +# date created : Fri Aug 10 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +A loader object. +""" + +import pygame +import os +import re + +class Loader(object): + def __init__(self, directory): + self.__dict__.update(locals()) + self.imgs = {} + + def load(self): + """Load all game resources.""" + l = ['ground1', 'ground2'] + for o in l: + self.imgs[o] = pygame.image.load(os.path.join( + self.directory, 'tiles', '%s.png' % o)) + + for o in range(1, 7): + self.imgs['indoor%d' % o] = pygame.image.load(os.path.join( + self.directory, 'tiles', 'indoor', 'ground%02d.png' % o)) + + l = ['block1', 'block1_lifted'] + for o in l: + self.imgs[o] = pygame.image.load(os.path.join( + self.directory, 'blocks', '%s.png' % o)) + + l = ['hole', 'well'] + for o in l: + self.imgs[o] = pygame.image.load(os.path.join( + self.directory, '%s.png' % o)) + + 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.directory, 'moat', '%s.png' % o)) + + # Special treatment + self.imgs['moat_end_horizontal_flip'] = pygame.transform.flip( + self.imgs['moat_end_horizontal'], 1, 0) + + # 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')), + ('boulder_falling', os.path.join('boulder_fall')), + + ('lever_updown', os.path.join('lever', 'down-up')), + ('lever_leftright', os.path.join('lever', 'left-right')), + + ('arrow_up', os.path.join('matt', 'up')), + ('arrow_right', os.path.join('matt', 'right')), + ('arrow_down', os.path.join('matt', 'down')), + ('arrow_left', os.path.join('matt', '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.directory, 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) + + # Special treatment: + if anim == 'arrow_left': + img = pygame.transform.flip(img, 1, 0) + + self.imgs[anim].append(img) + + ####### PLAYER ####### + + for anim, directory in ( + [('idle_up', os.path.join('robot_idle', 'up')), + ('idle_down', os.path.join('robot_idle', 'down')), + ('idle_right', os.path.join('robot_idle', 'right')), + ('idle_left', os.path.join('robot_idle', 'right')), + ('carry_up', os.path.join('robot_carry', 'up')), + ('carry_down', os.path.join('robot_carry', 'down')), + ('carry_right', os.path.join('robot_carry', 'right')), + ('carry_left', os.path.join('robot_carry', 'right')), + + # Lever + + ('lever_down_right', os.path.join('robot_lever', 'horizontal', + 'down_left')), + ('lever_down_left', os.path.join('robot_lever', 'horizontal', + 'down_left')), + ('lever_down_up', os.path.join('robot_lever', 'vertical', + 'down_down')), + ('lever_down_down', os.path.join('robot_lever', 'vertical', + 'down_down')), + + ('lever_up_right', os.path.join('robot_lever', 'horizontal', + 'up_right')), + ('lever_up_left', os.path.join('robot_lever', 'horizontal', + 'up_right')), + ('lever_up_up', os.path.join('robot_lever', 'vertical', + 'up_up')), + ('lever_up_down', os.path.join('robot_lever', 'vertical', + 'up_up')), + + ('lever_right_right', os.path.join('robot_lever', 'horizontal', + 'right_right')), + ('lever_right_left', os.path.join('robot_lever', 'horizontal', + 'right_right')), + ('lever_right_up', os.path.join('robot_lever', 'vertical', + 'left_up')), + ('lever_right_down', os.path.join('robot_lever', 'vertical', + 'right_down')), + + ('lever_left_right', os.path.join('robot_lever', 'horizontal', + 'right_right')), + ('lever_left_left', os.path.join('robot_lever', 'horizontal', + 'left_left')), + ('lever_left_up', os.path.join('robot_lever', 'vertical', + 'left_up')), + ('lever_left_down', os.path.join('robot_lever', 'vertical', + 'right_down')) + ] + ): + + self.imgs['robot_' + anim] = [] + + # Find all image files for the given animation + anim_files = [] + for root, dirs, files in os.walk(os.path.join( + self.directory, 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) + + # Special treatment: + if anim in ['idle_left', 'carry_left', + + 'lever_down_right', 'lever_left_down', + 'lever_up_left', 'lever_right_up']: + img = pygame.transform.flip(img, 1, 0) + + if anim in ['lever_right_left', 'lever_left_right', + 'lever_down_up', 'lever_up_down']: + self.imgs['robot_' + anim].insert(0, img) + else: + self.imgs['robot_' + anim].append(img) diff --git a/robotgame/player.py b/robotgame/player.py index dcf118d..e0e590b 100644 --- a/robotgame/player.py +++ b/robotgame/player.py @@ -47,89 +47,8 @@ class Player(worldobject.WorldObject): self.ignore_list.append(boulder.Boulder) - self.load() - - def load(self): - self.imgs = {} - - for anim, directory in ( - [('idle_up', os.path.join('robot_idle', 'up')), - ('idle_down', os.path.join('robot_idle', 'down')), - ('idle_right', os.path.join('robot_idle', 'right')), - ('idle_left', os.path.join('robot_idle', 'right')), - ('carry_up', os.path.join('robot_carry', 'up')), - ('carry_down', os.path.join('robot_carry', 'down')), - ('carry_right', os.path.join('robot_carry', 'right')), - ('carry_left', os.path.join('robot_carry', 'right')), - - # Lever - - ('lever_down_right', os.path.join('robot_lever', 'horizontal', - 'down_left')), - ('lever_down_left', os.path.join('robot_lever', 'horizontal', - 'down_left')), - ('lever_down_up', os.path.join('robot_lever', 'vertical', - 'down_down')), - ('lever_down_down', os.path.join('robot_lever', 'vertical', - 'down_down')), - - ('lever_up_right', os.path.join('robot_lever', 'horizontal', - 'up_right')), - ('lever_up_left', os.path.join('robot_lever', 'horizontal', - 'up_right')), - ('lever_up_up', os.path.join('robot_lever', 'vertical', - 'up_up')), - ('lever_up_down', os.path.join('robot_lever', 'vertical', - 'up_up')), - - ('lever_right_right', os.path.join('robot_lever', 'horizontal', - 'right_right')), - ('lever_right_left', os.path.join('robot_lever', 'horizontal', - 'right_right')), - ('lever_right_up', os.path.join('robot_lever', 'vertical', - 'left_up')), - ('lever_right_down', os.path.join('robot_lever', 'vertical', - 'right_down')), - - ('lever_left_right', os.path.join('robot_lever', 'horizontal', - 'right_right')), - ('lever_left_left', os.path.join('robot_lever', 'horizontal', - 'left_left')), - ('lever_left_up', os.path.join('robot_lever', 'vertical', - 'left_up')), - ('lever_left_down', os.path.join('robot_lever', 'vertical', - 'right_down')) - ] - ): - - self.imgs[anim] = [] - - # Find all image files for the given animation - anim_files = [] - for root, dirs, files in os.walk(os.path.join( - self.level.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) - - # Special treatment: - if anim in ['idle_left', 'carry_left', - - 'lever_down_right', 'lever_left_down', - 'lever_up_left', 'lever_right_up']: - img = pygame.transform.flip(img, 1, 0) - - if anim in ['lever_right_left', 'lever_left_right', - 'lever_down_up', 'lever_up_down']: - self.imgs[anim].insert(0, img) - else: - self.imgs[anim].append(img) - - self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)] + self.imgs = dict([(k[6:], v) for (k, v) in self.level.imgs.iteritems() + if k.startswith('robot_')]) def direction_as_string(self): if self.direction == (1, 0): From c0f59d72c948c25e60368317c0d0387c0624d60c Mon Sep 17 00:00:00 2001 From: Niels Serup Date: Fri, 10 Aug 2012 21:54:21 +0200 Subject: [PATCH 2/8] Level 3 easier but still missing necessary clues to the player. --- robotgame/level3.py | 30 ++++++++++++++++++------------ robotgame/logic/colourboxes.py | 27 ++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/robotgame/level3.py b/robotgame/level3.py index b09a799..8c59e76 100644 --- a/robotgame/level3.py +++ b/robotgame/level3.py @@ -57,16 +57,17 @@ class Level3(level.Level): # tile.Tile(self, i*64, j*48, self.imgs['ground1'])) task_start = (2, 2) - task_size = (5, 5) # Abstract "boxes", actually colour fields - boxes = [box + [(0, 0, 0)] * 2 for box in logic.colourboxes.generate_colour_boxes(2, 3)] - boxes += [[(0, 0, 0)] * 2 + box for box in logic.colourboxes.generate_colour_boxes(2, 3)] - boxes += [logic.colourboxes.generate_random_box(4) for _ in range(9)] - boxes += [[(0, 0, 0)] * 4 for _ in range(9)] + boxes = [] + for i in range(4): + boxes.extend([(0, 0, 0)] * i + box + [(0, 0, 0)] * (3 - i) + for box in logic.colourboxes.generate_colour_boxes(1, 3)) + boxes.extend(logic.colourboxes.generate_random_box(4, 2) for _ in range(20)) + boxes.extend([(0, 0, 0)] * 4 for _ in range(10)) random.shuffle(boxes) pos_colour = {} - for box, (x, y) in zip(boxes, itertools.product(range(6), range(4))): + for box, (x, y) in zip(boxes, itertools.product(range(7), range(6))): self.tiles.append(tile.Tile(self, 64 * (x + task_start[0] + 1), 48 * (y + task_start[1] + 1), self.imgs['ground1'])) @@ -74,15 +75,20 @@ class Level3(level.Level): self.draw_background() - action_blocks = [block.ActionBlock(self, 64 * (i + 1 + task_start[0]), - 48 * task_start[1], movable=True) - for i in range(6)] + action_blocks = list(itertools.chain(* + [(block.ActionBlock(self, 64 * task_start[0], + 48 * (i + 1 + task_start[1]), + movable=True), + block.ActionBlock(self, 64 * (task_start[0] + 8), + 48 * (i + 1 + task_start[1]), + movable=True)) + for i in range(6)])) self.objects.extend(action_blocks) wells = [block.ColorWell(self, task_start[0] * 64, task_start[1] * 48), - block.ColorWell(self, (task_start[0] + 7) * 64, task_start[1] * 48), - block.ColorWell(self, task_start[0] * 64, (task_start[1] + 5) * 48), - block.ColorWell(self, (task_start[0] + 7) * 64, (task_start[1] + 5) * 48), + block.ColorWell(self, (task_start[0] + 8) * 64, task_start[1] * 48), + block.ColorWell(self, task_start[0] * 64, (task_start[1] + 7) * 48), + block.ColorWell(self, (task_start[0] + 8) * 64, (task_start[1] + 7) * 48), ] self.objects.extend(wells) diff --git a/robotgame/logic/colourboxes.py b/robotgame/logic/colourboxes.py index bf8c3fb..9ec886b 100644 --- a/robotgame/logic/colourboxes.py +++ b/robotgame/logic/colourboxes.py @@ -91,14 +91,35 @@ def _get_oxs(x): else (x - 1, x + 1) if x % 3 == 1 \ else (x - 2, x - 1) -def generate_random_box(nwells): - """Generate a box with random colors except white (111).""" +def generate_random_box(nwells, min_nonblacks=0): + """ + Generate a box that triggers nwells wells, with random colors except white + (111). + + Arguments: + min_nonblacks -- minimum number of well colours in a box required not to be + black. + """ def gen_wc(): wc = [random.choice((0, 1)) for i in range(3)] if all(b == 1 for b in wc): wc[random.randrange(3)] = 0 return wc - return [tuple(gen_wc()) for _ in range(nwells)] + def gen_wc_nonblack(): + wc = gen_wc() + if all(b == 0 for b in wc): + wc[random.randrange(3)] = 1 + return wc + colours = [tuple(gen_wc()) for _ in range(nwells)] + nonblack = lambda t: any(n == 1 for n in t) + missing_nonblacks = min_nonblacks - len(list(filter(nonblack, colours))) + i = 0 + while missing_nonblacks > 0: + if not nonblack(colours[i]): + colours[i] = gen_wc_nonblack() + missing_nonblacks -= 1 + i += 1 + return colours def get_colours(boxes): colours = [] From 6790174a76ff5f114f53b35134aacce6327d2329 Mon Sep 17 00:00:00 2001 From: Niels Serup Date: Fri, 10 Aug 2012 22:10:27 +0200 Subject: [PATCH 3/8] Erased useless load functions. --- robotgame/level2.py | 39 --------------------------------------- robotgame/level3.py | 39 --------------------------------------- robotgame/level_bonus.py | 40 ---------------------------------------- 3 files changed, 118 deletions(-) diff --git a/robotgame/level2.py b/robotgame/level2.py index c3e7e88..c429d9e 100644 --- a/robotgame/level2.py +++ b/robotgame/level2.py @@ -45,45 +45,6 @@ class Level2(level.Level): anim='lever_updown')) - def load(self): - """Load all resources used in the level.""" - tile_list = ['ground1', 'ground2'] - - for tile in tile_list: - self.imgs[tile] = pygame.image.load(os.path.join( - self.graphics_dir, 'tiles', '%s.png' % tile)) - - block_list = ['block1'] - for block in block_list: - self.imgs[block] = pygame.image.load(os.path.join( - self.graphics_dir, 'blocks', '%s.png' % block)) - - # Load animations - for anim, directory in ( - [('lever_updown', os.path.join('lever', 'down-up')), - ] - ): - - 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) - - # Special treatment: - if anim == 'arrow_left': - img = pygame.transform.flip(img, 1, 0) - - self.imgs[anim].append(img) - def restart(self): for obj in self.objects: obj.reset_pos() diff --git a/robotgame/level3.py b/robotgame/level3.py index 8c59e76..dfbfca8 100644 --- a/robotgame/level3.py +++ b/robotgame/level3.py @@ -111,45 +111,6 @@ class Level3(level.Level): b.action = update_wells - def load(self): - """Load all resources used in the level.""" - tile_list = ['ground1', 'ground2'] - - for tile in tile_list: - self.imgs[tile] = pygame.image.load(os.path.join( - self.graphics_dir, 'tiles', '%s.png' % tile)) - - block_list = ['block1', 'block1_lifted'] - for block in block_list: - self.imgs[block] = pygame.image.load(os.path.join( - self.graphics_dir, 'blocks', '%s.png' % block)) - - # Load animations - for anim, directory in ( - [('lever_updown', os.path.join('lever', 'down-up')), - ] - ): - - 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) - - # Special treatment: - if anim == 'arrow_left': - img = pygame.transform.flip(img, 1, 0) - - self.imgs[anim].append(img) - def restart(self): for obj in self.objects: obj.reset_pos() diff --git a/robotgame/level_bonus.py b/robotgame/level_bonus.py index 020ae2a..b7c82b8 100644 --- a/robotgame/level_bonus.py +++ b/robotgame/level_bonus.py @@ -73,46 +73,6 @@ class Level(level.Level): toggling=False, anim='lever_updown')) - - def load(self): - """Load all resources used in the level.""" - tile_list = ['ground1', 'ground2'] - - for tile in tile_list: - self.imgs[tile] = pygame.image.load(os.path.join( - self.graphics_dir, 'tiles', '%s.png' % tile)) - - block_list = ['block1', 'block1_lifted'] - for block in block_list: - self.imgs[block] = pygame.image.load(os.path.join( - self.graphics_dir, 'blocks', '%s.png' % block)) - - # Load animations - for anim, directory in ( - [('lever_updown', os.path.join('lever', 'down-up')), - ] - ): - - 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) - - # Special treatment: - if anim == 'arrow_left': - img = pygame.transform.flip(img, 1, 0) - - self.imgs[anim].append(img) - def enter(self, root_level): self.__dict__.update(locals()) self.game.objs.remove(root_level) From ba7edfffd49b6a310acf14cc0e211f882e6fae53 Mon Sep 17 00:00:00 2001 From: Niels Serup Date: Fri, 10 Aug 2012 22:20:31 +0200 Subject: [PATCH 4/8] Replaced ground tile with indoor tiles in some places. --- robotgame/level2.py | 2 +- robotgame/level3.py | 2 +- robotgame/level_bonus.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/robotgame/level2.py b/robotgame/level2.py index c429d9e..208c963 100644 --- a/robotgame/level2.py +++ b/robotgame/level2.py @@ -23,7 +23,7 @@ class Level2(level.Level): for i in range(self.dimensions[0]): for j in range(self.dimensions[1]): self.tiles.append( - tile.Tile(self, i*64, j*48, self.imgs['ground1'])) + tile.Tile(self, i*64, (j + 1)*48, self.imgs['indoor%d' % random.randint(1, 6)])) self.draw_background() diff --git a/robotgame/level3.py b/robotgame/level3.py index dfbfca8..fc3c136 100644 --- a/robotgame/level3.py +++ b/robotgame/level3.py @@ -70,7 +70,7 @@ class Level3(level.Level): for box, (x, y) in zip(boxes, itertools.product(range(7), range(6))): self.tiles.append(tile.Tile(self, 64 * (x + task_start[0] + 1), 48 * (y + task_start[1] + 1), - self.imgs['ground1'])) + self.imgs['indoor%d' % random.randint(1, 6)])) pos_colour[(x, y)] = box self.draw_background() diff --git a/robotgame/level_bonus.py b/robotgame/level_bonus.py index b7c82b8..31a06e8 100644 --- a/robotgame/level_bonus.py +++ b/robotgame/level_bonus.py @@ -49,7 +49,7 @@ class Level(level.Level): for i in range(self.dimensions[0]): for j in range(self.dimensions[1]): self.tiles.append( - tile.Tile(self, i*64, j*48, self.imgs['ground1'])) + tile.Tile(self, (i+1)*64, j*48, self.imgs['indoor%d' % random.randint(1, 6)])) self.draw_background() From bb17cbb4307f287c3ef0f341712239a954339b53 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Fri, 10 Aug 2012 22:31:15 +0200 Subject: [PATCH 5/8] Added task 1. --- robotgame/level1.py | 124 +++++++++++++++++++++++++++++++++++++++++++- robotgame/loader.py | 9 ++-- 2 files changed, 129 insertions(+), 4 deletions(-) diff --git a/robotgame/level1.py b/robotgame/level1.py index 1704342..d9e774e 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -37,6 +37,7 @@ import block import boulder import lever import trigger +import worldobject import logic.rollingstone import logic.colourboxes @@ -72,6 +73,69 @@ class Level1(level.Level): self.objects.append(block.InvisBlock(self, self.dimensions[0] * 64, i * 48)) + ### Task 1: Wheels of fortune + task1_pos = (64 * 10, 48 * 10) + + ws = [] + + for i in range(5): + w1 = Wheel(self, task1_pos[0] - 64, task1_pos[1] + 48 * i * 2) + w2 = Wheel(self, task1_pos[0], task1_pos[1] + 48 * i * 2, + immitate=w1) + self.objects.extend([w1, w2]) + self.objects.append(lever.Lever(self, + task1_pos[0] + 64 * 1, + task1_pos[1] + 48 * i * 2, + [w2.activate])) + if i > 0: + self.objects.append(lever.Lever(self, + task1_pos[0], + task1_pos[1] + 48 * (i * 2 - 1), + [w2.set_direction], + toggling=True)) + else: + self.objects.append(lever.Lever(self, + task1_pos[0] + 64 * 2, + task1_pos[1] - 48, + [w2.set_direction], + toggling=True, + anim='lever_updown')) + + ws.append(w2) + + self.objects.append( + lever.Lever(self, + task1_pos[0] + 64 * 2, + task1_pos[1] + 48 * 9, + [lambda *xs: self.complete_task(1) + if len(ws) == len([w for w in ws if w.on]) + else lambda: None], + anim='lever_updown')) + + for i in range(9): + self.add_tile(task1_pos[0] - 64 * 2, + task1_pos[1] + 48 * i, + 'moat_vertical') + self.add_tile(task1_pos[0] - 64 * 2, + task1_pos[1] - 48, + 'moat_corner_north') + self.add_tile(task1_pos[0] - 64 * 2, + task1_pos[1] + 48 * 9, + 'moat_corner_south') + for i in range(2): + self.add_tile(task1_pos[0] + 64 * (i - 1), + task1_pos[1] - 48, + 'moat_horizontal') + self.add_tile(task1_pos[0] + 64 * (i - 1), + task1_pos[1] + 48 * 9, + 'moat_horizontal') + self.add_tile(task1_pos[0] + 64, + task1_pos[1] - 48, + 'moat_end_horizontal_flip') + self.add_tile(task1_pos[0] + 64, + task1_pos[1] + 48 * 9, + 'moat_end_horizontal_flip') + ### Task 2: Rolling stone task2_size = 15, 10 @@ -88,7 +152,13 @@ class Level1(level.Level): task2_pos[0] + 64 * i, task2_pos[1] + 48 * j, self.imgs['block1'], - movable=True)) + movable=True) + if (random.randint(0, 2) or (i, j) == 4) else + block.Block(self, + task2_pos[0] + 64 * i, + task2_pos[1] + 48 * j, + self.imgs['block3'], + movable=False)) arrow_blocks = [] n = 0 @@ -349,3 +419,55 @@ class Level1(level.Level): def restart(self): for obj in self.objects: obj.reset_pos() + +class Wheel(block.Block): + def __init__(self, level, x, y, immitate=None): + self.__dict__.update(locals()) + worldobject.WorldObject.__init__(self, level, x, y) + + self.frame = 0 + self.anim_speed = 15 + self.nsettings = 5 + self.setting = random.randint(0, self.nsettings - 1) + if self.immitate: + self.on = self.setting == self.immitate.setting + + self.init_setting = self.setting + + self.anim = 'stairs' + + self.anim_direction = 1 + + 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 set_direction(self, setting): + self.anim_direction = -1 if setting else 1 + + def activate(self, setting): + self.setting = (self.setting + 1 * self.anim_direction) % self.nsettings + self.on = self.setting == self.immitate.setting + + def update(self, e, t, dt): + # Update the animation + l = len(self.level.imgs[self.anim]) + if not (int(self.frame) == self.setting * l / self.nsettings): + self.frame = ((self.frame + self.anim_speed * dt) % l + if (self.setting > 0 and self.anim_direction > 0) + or (self.setting == self.nsettings - 1 + and self.anim_direction < 0) else + (self.frame - self.anim_speed * dt) % l) + + 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 - self.level.camera_x, + self.y - self.img.get_size()[1] + 24 + - self.level.camera_y)) diff --git a/robotgame/loader.py b/robotgame/loader.py index 23a595e..588564e 100644 --- a/robotgame/loader.py +++ b/robotgame/loader.py @@ -44,7 +44,7 @@ class Loader(object): self.imgs['indoor%d' % o] = pygame.image.load(os.path.join( self.directory, 'tiles', 'indoor', 'ground%02d.png' % o)) - l = ['block1', 'block1_lifted'] + l = ['block1', 'block1_lifted', 'block3'] for o in l: self.imgs[o] = pygame.image.load(os.path.join( self.directory, 'blocks', '%s.png' % o)) @@ -83,7 +83,9 @@ class Loader(object): ('arrow_up', os.path.join('matt', 'up')), ('arrow_right', os.path.join('matt', 'right')), ('arrow_down', os.path.join('matt', 'down')), - ('arrow_left', os.path.join('matt', 'right')),] + ('arrow_left', os.path.join('matt', 'right')), + + ('stairs', 'stairs')] ): self.imgs[anim] = [] @@ -176,7 +178,8 @@ class Loader(object): if anim in ['idle_left', 'carry_left', 'lever_down_right', 'lever_left_down', - 'lever_up_left', 'lever_right_up']: + 'lever_up_left', 'lever_right_up', + 'lever_left_right']: img = pygame.transform.flip(img, 1, 0) if anim in ['lever_right_left', 'lever_left_right', From 814471e722570cbd5168bee1e6f17e89c574de8f Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Fri, 10 Aug 2012 22:46:22 +0200 Subject: [PATCH 6/8] Modified the layout of task 1. --- robotgame/level1.py | 79 ++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/robotgame/level1.py b/robotgame/level1.py index d9e774e..2d105f0 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -74,66 +74,57 @@ class Level1(level.Level): i * 48)) ### Task 1: Wheels of fortune - task1_pos = (64 * 10, 48 * 10) + task1_pos = (64 * 15, 48 * 13) ws = [] for i in range(5): - w1 = Wheel(self, task1_pos[0] - 64, task1_pos[1] + 48 * i * 2) - w2 = Wheel(self, task1_pos[0], task1_pos[1] + 48 * i * 2, + w1 = Wheel(self, task1_pos[0] + 64 * i, task1_pos[1] - 48 * 2) + w2 = Wheel(self, task1_pos[0] + 64 * i, task1_pos[1], immitate=w1) self.objects.extend([w1, w2]) self.objects.append(lever.Lever(self, - task1_pos[0] + 64 * 1, - task1_pos[1] + 48 * i * 2, - [w2.activate])) - if i > 0: - self.objects.append(lever.Lever(self, - task1_pos[0], - task1_pos[1] + 48 * (i * 2 - 1), - [w2.set_direction], - toggling=True)) - else: - self.objects.append(lever.Lever(self, - task1_pos[0] + 64 * 2, - task1_pos[1] - 48, - [w2.set_direction], - toggling=True, - anim='lever_updown')) - + task1_pos[0] + 64 * i, + task1_pos[1] + 48 * 1, + [w2.activate], + anim='lever_updown')) ws.append(w2) + self.add_tile(task1_pos[0] + 64 * i, + task1_pos[1] - 48 * 3, + 'moat_horizontal') self.objects.append( lever.Lever(self, - task1_pos[0] + 64 * 2, - task1_pos[1] + 48 * 9, + task1_pos[0] + 64 * 5, + task1_pos[1] + 48 * 2, [lambda *xs: self.complete_task(1) if len(ws) == len([w for w in ws if w.on]) - else lambda: None], - anim='lever_updown')) + else lambda: None])) - for i in range(9): - self.add_tile(task1_pos[0] - 64 * 2, - task1_pos[1] + 48 * i, - 'moat_vertical') - self.add_tile(task1_pos[0] - 64 * 2, - task1_pos[1] - 48, + self.add_tile(task1_pos[0] - 64, + task1_pos[1] - 48 * 3, 'moat_corner_north') - self.add_tile(task1_pos[0] - 64 * 2, - task1_pos[1] + 48 * 9, + self.add_tile(task1_pos[0] + 64 * 5, + task1_pos[1] - 48 * 3, + 'moat_corner_north_flip') + for i in range(3): + self.add_tile(task1_pos[0] - 64, + task1_pos[1] + 48 * (i - 2), + 'moat_vertical') + self.add_tile(task1_pos[0] + 64 * 5, + task1_pos[1] + 48 * (i - 2), + 'moat_vertical') + self.add_tile(task1_pos[0] - 64, + task1_pos[1] + 48, + 'moat_corner_south_flip') + self.add_tile(task1_pos[0] + 64 * 5, + task1_pos[1] + 48, 'moat_corner_south') - for i in range(2): - self.add_tile(task1_pos[0] + 64 * (i - 1), - task1_pos[1] - 48, - 'moat_horizontal') - self.add_tile(task1_pos[0] + 64 * (i - 1), - task1_pos[1] + 48 * 9, - 'moat_horizontal') - self.add_tile(task1_pos[0] + 64, - task1_pos[1] - 48, - 'moat_end_horizontal_flip') - self.add_tile(task1_pos[0] + 64, - task1_pos[1] + 48 * 9, + self.add_tile(task1_pos[0] - 64 * 2, + task1_pos[1] + 48, + 'moat_end_horizontal') + self.add_tile(task1_pos[0] + 64 * 6, + task1_pos[1] + 48, 'moat_end_horizontal_flip') ### Task 2: Rolling stone From ab1cb93527c3b1e3b42e43723569130ecbebab4d Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Fri, 10 Aug 2012 22:52:55 +0200 Subject: [PATCH 7/8] Moved tiles to their proper location in task 3. --- robotgame/level1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/robotgame/level1.py b/robotgame/level1.py index 2d105f0..a9929b2 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -271,7 +271,7 @@ class Level1(level.Level): # y * 48 + task3_pos[1], # self.imgs['ground1'])) pos_colour[(x, y)] = box - self.add_tile(task3_pos[0] + 64 * x, task3_pos[1] + 48 * y, + self.add_tile(task3_pos[0] + 64 * x, task3_pos[1] + 48 * (y + 1), 'indoor%d' % random.randint(1, 6), blocking=False) action_blocks = [block.ActionBlock(self, 64 * i + task3_pos[0], @@ -404,7 +404,7 @@ class Level1(level.Level): return self.task_completions.append(task) if len(self.task_completions) > 3: - self.task_completions.remove(self.task_completions[0]) + self.task_completions = self.task_completions[-3:] print(self.task_completions) def restart(self): @@ -418,7 +418,7 @@ class Wheel(block.Block): self.frame = 0 self.anim_speed = 15 - self.nsettings = 5 + self.nsettings = 4 self.setting = random.randint(0, self.nsettings - 1) if self.immitate: self.on = self.setting == self.immitate.setting From 5904d29aefb55840f6ef5d5ca94d8124911492c7 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Fri, 10 Aug 2012 23:11:06 +0200 Subject: [PATCH 8/8] Committed the fix. --- robotgame/level1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robotgame/level1.py b/robotgame/level1.py index a9929b2..f4acf19 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -144,7 +144,7 @@ class Level1(level.Level): task2_pos[1] + 48 * j, self.imgs['block1'], movable=True) - if (random.randint(0, 2) or (i, j) == 4) else + if (random.randint(0, 2) or (i, j) == (4, 0)) else block.Block(self, task2_pos[0] + 64 * i, task2_pos[1] + 48 * j,