Ready for lasers.

This commit is contained in:
Niels Serup 2012-08-11 14:30:46 +02:00
parent b61f54963d
commit e9a62a028c
3 changed files with 40 additions and 7 deletions

View File

@ -72,10 +72,16 @@ class Level(object):
self.camera_x = (self.player.x - screen_size[0] / 2 + 32) self.camera_x = (self.player.x - screen_size[0] / 2 + 32)
self.camera_y = (self.player.y - screen_size[1] / 2 - 24) self.camera_y = (self.player.y - screen_size[1] / 2 - 24)
def draw(self, window): def _blit_background(self, window):
window.blit(self.background, (0 - self.camera_x, 0 - self.camera_y)) window.blit(self.background, (0 - self.camera_x, 0 - self.camera_y))
for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)): def _sorted_objs(self):
return sorted(self.objects, key=lambda obj: (obj.y + obj.z))
def draw(self, window):
self._blit_background(window)
for obj in self._sorted_objs():
obj.draw(window) obj.draw(window)
self.darkness.draw(window) self.darkness.draw(window)

View File

@ -65,7 +65,7 @@ class Level4(level.Level):
self.objects.append({ self.objects.append({
lm.Mirror: mirror.Mirror(self, x, y, random.choice((True, False))), lm.Mirror: mirror.Mirror(self, x, y, random.choice((True, False))),
lm.Lever: lever.Lever( lm.Lever: lever.Lever(
self, x, y, [], toggling=True, self, x, y, [lambda setting: self.generate_lasers], toggling=True,
anim='lever_leftright' if x == 0 or x == 15 * 64 anim='lever_leftright' if x == 0 or x == 15 * 64
else 'lever_updown'), else 'lever_updown'),
lm.Target: block.Block(self, x, y, self.imgs['block3'], lm.Target: block.Block(self, x, y, self.imgs['block3'],
@ -82,13 +82,38 @@ class Level4(level.Level):
m = min(mirrors, key=lambda m: misc.manhattan_dist( m = min(mirrors, key=lambda m: misc.manhattan_dist(
(m.x, m.y), (l.x, l.y))) (m.x, m.y), (l.x, l.y)))
mirrors.remove(m) mirrors.remove(m)
l.links.append((lambda m: lambda setting: m.rotate())(m)) 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_pos(64 * 7, 48 * 2)
self.player.set_init_pos() self.player.set_init_pos()
def restart(self): def restart(self):
for obj in self.objects: for obj in self.objects:
obj.reset_pos() obj.reset_pos()
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)

View File

@ -34,6 +34,7 @@ class Mirror(worldobject.WorldObject):
worldobject.WorldObject.__init__(self, level, x, y) worldobject.WorldObject.__init__(self, level, x, y)
self.in_rotation = False self.in_rotation = False
self.left_up_aim = self.left_up
self.frame = 7 self.frame = 7
self.anim_speed = 12 self.anim_speed = 12
@ -43,7 +44,7 @@ class Mirror(worldobject.WorldObject):
if self.in_rotation: if self.in_rotation:
return return
self.in_rotation = True self.in_rotation = True
self.left_up = not self.left_up self.left_up_aim = not self.left_up_aim
self.frame = 0 self.frame = 0
def update(self, e, t, dt): def update(self, e, t, dt):
@ -52,11 +53,12 @@ class Mirror(worldobject.WorldObject):
self.frame = min(self.frame + self.anim_speed * dt, top) self.frame = min(self.frame + self.anim_speed * dt, top)
if self.frame == top: if self.frame == top:
self.in_rotation = False self.in_rotation = False
self.left_up = not self.left_up
worldobject.WorldObject.update(self, e, t, dt) worldobject.WorldObject.update(self, e, t, dt)
def draw(self, window): def draw(self, window):
fn = self.frame + 1 if not self.left_up else self.frame + self._half_anim_len fn = self.frame + 1 if not self.left_up_aim else self.frame + self._half_anim_len
self.img = self.level.imgs['mirror'][int(fn)] self.img = self.level.imgs['mirror'][int(fn)]
window.blit(self.img, (self.x - 32 - self.level.camera_x, window.blit(self.img, (self.x - 32 - self.level.camera_x,
self.y - self.img.get_size()[1] + 24 self.y - self.img.get_size()[1] + 24