From 9d7bb2e946310becf43f8a14e0113da73c0f7afc Mon Sep 17 00:00:00 2001 From: Niels Serup Date: Mon, 13 Aug 2012 00:05:44 +0200 Subject: [PATCH] level 4 walls, good laser still under development (algorithm discovered, implementation next up) --- robotgame/level4.py | 75 +++++++++++++++--------------------- robotgame/loader.py | 11 ++++-- robotgame/logic/direction.py | 20 ++++++++++ 3 files changed, 58 insertions(+), 48 deletions(-) diff --git a/robotgame/level4.py b/robotgame/level4.py index a7f84d5..0aa1ad0 100644 --- a/robotgame/level4.py +++ b/robotgame/level4.py @@ -46,14 +46,14 @@ from logic.direction import * class Level4(level.Level): def __init__(self, game, graphics_dir, paused=False): level.Level.__init__(self, game, graphics_dir, - size=(64 * 16, 48 * 16), paused=paused) + size=(64 * 16, 48 * 20), paused=paused) self.dimensions = 16, 16 for i in range(self.dimensions[0]): for j in range(self.dimensions[1]): self.tiles.append( - tile.Tile(self, i*64, (j+1)*48, + tile.Tile(self, i*64, (j+4)*48, self.imgs['indoor%d' % random.randint(1, 6)])) self.draw_background() @@ -61,11 +61,12 @@ class Level4(level.Level): self.playfield = lm.generate_simple_playfield(16) for (x, y), t in self.playfield.items(): - x1, y1 = 64 * x, 48 * (y + 1) + x1, y1 = 64 * x, 48 * (y + 4) if isinstance(t, lm.Source): - self.objects.append(block.Block(self, x1, y1, - self.imgs['block3'], - movable=False)) + self.objects.append(block.Block( + self, x1, y1, + self.imgs['lasersource_' + t.direction.to_str().lower()], + movable=False)) continue def mir(b, x1, y1): def f(x, y): @@ -84,7 +85,7 @@ class Level4(level.Level): toggling=True, anim='lever_leftright' if x in (0, 15) else 'lever_updown'), - lm.Target: block.Block(self, x1, y1, self.imgs['block3'], + lm.Target: block.Block(self, x1, y1, self.imgs['lasertarget'], movable=False), lm.Blocker: block.Block(self, x1, y1, self.imgs['block1'], movable=False) @@ -100,9 +101,23 @@ class Level4(level.Level): mirrors.remove(m) l.links.insert(0, (lambda m: lambda setting: m.rotate())(m)) + for i in range(self.size[0] / 64): + if not i % 3: + self.objects.append(block.Block(self, i * 64, + 48 * 3, + self.imgs['wall'], + width=3)) + self.objects.append(block.InvisBlock(self, i * 64, + self.size[1])) + for i in range(self.size[1] / 48): + self.objects.append(block.InvisBlock(self, - 64, + i * 48)) + self.objects.append(block.InvisBlock(self, self.size[0], + i * 48)) + self.generate_lasers() - self.player.set_pos(64 * 7, 48 * 2) + self.player.set_pos(64 * 7, 48 * 5) self.player.set_init_pos() def restart(self): @@ -121,15 +136,17 @@ class Level4(level.Level): self._blit_background(window) objs = self._sorted_objs(self.objects + self.lasers) - self._after_sort(itertools.groupby(objs, lambda obj: obj.y + obj.z)) + objs = self._after_sort(itertools.groupby(objs, lambda obj: obj.y + obj.z)) for obj in objs: obj.draw(window) self.darkness.draw(window) def _after_sort(self, objss): - for objs in objss: - self._after_sort_line(objs) + n_objs = [] + for c, objs in objss: + n_objs.extend(self._after_sort_line(list(objs))) + return n_objs def _after_sort_line(self, objs): is_special = lambda obj: isinstance(obj, mirror.Mirror) or isinstance(obj, laser.Laser) @@ -137,39 +154,9 @@ class Level4(level.Level): return nonspecials + self._sort_line_specials(specials) def _sort_line_specials(self, objs): - print(objs) - return objs - -# def _compare(a, b): -# types = map(type, (a, b)) -# if a.y + a.z == b.y + b.z: -# if isinstance(a, mirror.Mirror) and isinstance(b, laser.Laser): -# return 1 -# if isinstance(b, mirror.Mirror) and isinstance(a, laser.Laser): -# return -1 -# # if mirror.Mirror in types and laser.Laser in types: -# # if not isinstance(a, laser.Laser): -# # a, b, swapped = b, a, True -# # else: -# # swapped = False -# # laser_direc = a.laser_direction -# # if a.p0[1] < a.p1[1]: -# # laser_direc = Down # instead of Down -# # if a.p0[1] > a.p1[1]: -# # laser_direc = Up # instead of Up -# # mirror_direc = Left if b.left_up else Right -# # result = 1 if _is_laser_behind_mirror(mirror_direc, laser_direc) \ -# # else -1 -# # result = -1 -# # return result if not swapped else -1 * result -# if a is laser.Laser and b is not laser.Laser: -# return 1 -# if b is laser.Laser and a is not laser.Laser: -# return -1 -# return 0 - -# return -1 if a.y + a.z < b.y + b.z \ -# else 1 if a.y + a.z > b.y + b.z else 0 + mirrors = filter(lambda obj: isinstance(obj, mirror.Mirror), objs) + lasers = filter(lambda obj: isinstance(obj, laser.Laser), objs) + return mirrors + lasers def _is_laser_behind_mirror(mirror_direc, laser_direc): return { diff --git a/robotgame/loader.py b/robotgame/loader.py index 4028f73..8d9d3c1 100644 --- a/robotgame/loader.py +++ b/robotgame/loader.py @@ -51,11 +51,14 @@ 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', 'block3'] + l = ['block1', 'block1_lifted', 'block3', '../lasertarget', + '../lasersource_up', '../lasersource_down', '../lasersource_right'] for o in l: - self.imgs[o] = pygame.image.load(os.path.join( + self.imgs[os.path.basename(o)] = pygame.image.load(os.path.join( self.directory, 'blocks', '%s.png' % o)) - + self.imgs['lasersource_left'] = pygame.transform.flip( + self.imgs['lasersource_right'], 1, 0) + l = ['hole', 'well', 'wall', 'wall_outside', 'intro-screen'] for o in l: self.imgs[o] = pygame.image.load(os.path.join( @@ -129,7 +132,7 @@ class Loader(object): img = pygame.image.load(f) # Special treatment: - if anim == 'arrow_left': + if anim in ('arrow_left',): img = pygame.transform.flip(img, 1, 0) self.imgs[anim].append(img) diff --git a/robotgame/logic/direction.py b/robotgame/logic/direction.py index 00c5d61..1107bcf 100644 --- a/robotgame/logic/direction.py +++ b/robotgame/logic/direction.py @@ -29,6 +29,10 @@ class Direction(object): def next_pos(pos): raise NotImplementedError + @staticmethod + def to_str(): + raise NotImplementedError + @staticmethod def from_sakse(p): return {(0, -1): Up, @@ -42,24 +46,40 @@ class Up(Direction): x, y = pos return x, y - 1 + @staticmethod + def to_str(): + return 'up' + class Right(Direction): @staticmethod def next_pos(pos): x, y = pos return x + 1, y + @staticmethod + def to_str(): + return 'right' + class Down(Direction): @staticmethod def next_pos(pos): x, y = pos return x, y + 1 + @staticmethod + def to_str(): + return 'down' + class Left(Direction): @staticmethod def next_pos(pos): x, y = pos return x - 1, y + @staticmethod + def to_str(): + return 'left' + all_directions = (Up, Right, Down, Left) _sp = lambda n: lambda d: all_directions[(all_directions.index(d) + n) % 4] succ, pred = _sp(1), _sp(-1)