a-robots-conundrum/robotgame/level4.py

120 lines
3.9 KiB
Python
Raw Normal View History

# 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 <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# level1.py
# --------------------
# date created : Fri Aug 10 2012
# copyright : (C) 2012 Niels G. W. Serup
# maintained by : Niels G. W. Serup <ns@metanohi.name>
"""
The fourth level.
"""
import os
import pygame
import random
import re
import itertools
import level
import player
import tile
import block
import boulder
import lever
import mirror
import trigger
import misc
import worldobject
import logic.lasermirror as lm
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)
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,
self.imgs['indoor%d' % random.randint(1, 6)]))
self.draw_background()
playfield = lm.generate_simple_playfield(16)
for (x, y), t in playfield.items():
x, y = 64 * x, 48 * (y + 1)
self.objects.append({
lm.Mirror: mirror.Mirror(self, x, y, random.choice((True, False))),
lm.Lever: lever.Lever(
2012-08-11 14:30:46 +02:00
self, x, y, [lambda setting: self.generate_lasers], toggling=True,
anim='lever_leftright' if x == 0 or x == 15 * 64
else 'lever_updown'),
lm.Target: block.Block(self, x, y, self.imgs['block3'],
movable=False),
lm.Blocker: block.Block(self, x, y, self.imgs['block1'],
movable=False)
}[t])
mirrors = list(filter(lambda obj: isinstance(obj, mirror.Mirror),
self.objects))
levers = list(filter(lambda obj: isinstance(obj, lever.Lever),
self.objects))
random.shuffle(levers)
for l in levers:
m = min(mirrors, key=lambda m: misc.manhattan_dist(
(m.x, m.y), (l.x, l.y)))
mirrors.remove(m)
2012-08-11 14:30:46 +02:00
l.links.insert(0, (lambda m: lambda setting: m.rotate())(m))
self.generate_lasers()
self.player.set_pos(64 * 7, 48 * 2)
self.player.set_init_pos()
def restart(self):
for obj in self.objects:
obj.reset_pos()
2012-08-11 14:30:46 +02:00
def generate_lasers(self):
self.lasers = []
def draw_lasers(self, window):
for p0, p1 in self.lasers:
pygame.draw.line(p0, p1, (255, 0, 0))
def draw(self, window):
self._blit_background(window)
objs = self._sorted_objs()
nonmirrors = filter(lambda obj: not isinstance(obj, mirror.Mirror),
objs)
mirrors = filter(lambda obj: isinstance(obj, mirror.Mirror), objs)
for obj in nonmirrors:
obj.draw(window)
self.draw_lasers(window)
for obj in mirrors:
obj.draw(window)
self.darkness.draw(window)