diff --git a/robotgame/block.py b/robotgame/block.py index c528165..97f56d7 100644 --- a/robotgame/block.py +++ b/robotgame/block.py @@ -168,3 +168,32 @@ class RisingBlock(Block): def draw(self, window): self.img = self.level.imgs[self.anim][int(self.frame)] Block.draw(self, window) + + +class ActionBlock(Block): + def __init__(self, level, x, y, action=None, movable=True): + self.__dict__.update(locals()) + Block.__init__( + self, level, x, y, img=level.imgs['block1'], + movable=movable) + self._last_pos = None + + def update(self, e, t, dt): + if (self.x, self.y) != self._last_pos and not self.is_moving: + self.action(self) + self._last_pos = self.x, self.y + + worldobject.WorldObject.update(self, e, t, dt) + +class ColorWell(Block): + def __init__(self, level, x, y): + self.__dict__.update(locals()) + worldobject.WorldObject.__init__( + self, level, x, y, movable=False) + self.img = pygame.Surface((128, 96)) + self.set_colour(0, 0, 0) + + def set_colour(self, r, g, b): + self.well_colour = r * 255, g * 255, b * 255 + self.img.fill(self.well_colour) + diff --git a/robotgame/level1.py b/robotgame/level1.py index 233df4c..9f7ed3c 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -28,6 +28,7 @@ import os import pygame import random import re +import itertools import level import player @@ -38,6 +39,7 @@ import lever import trigger import logic.rollingstone +import logic.colourboxes class Level1(level.Level): def __init__(self, game, graphics_dir, paused=False): @@ -191,6 +193,50 @@ class Level1(level.Level): self.imgs['hole'], [b], signal=[0, 2])) + + ### Task 3: Colour blocks + + task3_pos = (64 * 3, 48 * 16) + + # Abstract "boxes", actually colour fields + boxes = logic.colourboxes.generate_colour_boxes(1, 3) + boxes += boxes + boxes += [logic.colourboxes.generate_random_box(1) for _ in range(3)] + random.shuffle(boxes) + + pos_colour = {} + for box, (x, y) in zip(boxes, itertools.product(range(3), range(3))): + # self.tiles.append(tile.Tile(self, x * 64 + task3_pos[0], + # y * 48 + task3_pos[1], + # self.imgs['ground1'])) + pos_colour[(x, y)] = box + + action_blocks = [block.ActionBlock(self, 64 * i + task3_pos[0], + task3_pos[1], movable=True) + for i in range(3)] + self.objects.extend(action_blocks) + + wells = [block.ColorWell(self, task3_pos[0] + 64, task3_pos[1] + 48 * 5)] + self.objects.extend(wells) + + def update_wells(block): + cur_boxes = [] + for block in action_blocks: + box = pos_colour.get(((block.x - task3_pos[0]) / 64, (block.y - task3_pos[1] - 48) / 48)) + if box: + cur_boxes.append(box) + + if not cur_boxes: + well_colours = [(0, 0, 0)] * len(wells) + else: + well_colours = logic.colourboxes.get_colours(cur_boxes) + for i in range(len(wells)): + wells[i].set_colour(*well_colours[i]) + + for b in action_blocks: + b.action = update_wells + + ### Task 4: Inverted bits task4_pos = (64 * 13, 48 * 13) diff --git a/robotgame/level3.py b/robotgame/level3.py index 80394d7..bc17a74 100644 --- a/robotgame/level3.py +++ b/robotgame/level3.py @@ -44,33 +44,6 @@ import lever import logic.colourboxes -class ActionBlock(block.Block): - def __init__(self, level, x, y, action, movable=True): - self.__dict__.update(locals()) - block.Block.__init__( - self, level, x, y, img=level.imgs['block1'], - movable=movable) - self._last_pos = None - - def update(self, e, t, dt): - if (self.x, self.y) != self._last_pos and not self.is_moving: - self.action(self) - self._last_pos = self.x, self.y - - worldobject.WorldObject.update(self, e, t, dt) - -class ColorWell(block.Block): - def __init__(self, level, x, y): - self.__dict__.update(locals()) - worldobject.WorldObject.__init__( - self, level, x, y, movable=False) - self.img = pygame.Surface((128, 96)) - self.set_colour(0, 0, 0) - - def set_colour(self, r, g, b): - self.well_colour = r * 255, g * 255, b * 255 - self.img.fill(self.well_colour) - class Level3(level.Level): def __init__(self, game, graphics_dir, paused=False): level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20), @@ -83,40 +56,6 @@ class Level3(level.Level): # self.tiles.append( # tile.Tile(self, i*64, j*48, self.imgs['ground1'])) - boxes = logic.colourboxes.generate_colour_boxes(1, 3) - boxes += boxes - boxes += [logic.colourboxes.generate_random_box(1) for _ in range(3)] - random.shuffle(boxes) - - pos_colour = {} - for box, (x, y) in zip(boxes, itertools.product(range(3, 6), range(3, 6))): - self.tiles.append(tile.Tile(self, x * 64, y * 48, self.imgs['ground1'])) - pos_colour[(x, y)] = box - - blocks = [ActionBlock(self, 64 * i, 0, action=None, movable=True) - for i in range(1, 4)] - self.objects.extend(blocks) - - wells = [ColorWell(self, 64 * 5, 0)] - self.objects.extend(wells) - - def update_wells(block): - cur_boxes = [] - for block in blocks: - box = pos_colour.get((block.x / 64, block.y / 48)) - if box: - cur_boxes.append(box) - - if not cur_boxes: - well_colours = [(0, 0, 0)] * len(wells) - else: - well_colours = logic.colourboxes.get_colours(cur_boxes) - for i in range(len(wells)): - wells[i].set_colour(*well_colours[i]) - - for b in blocks: - b.action = update_wells - self.draw_background()