Merge branch 'master' of hongabar.org:robotgame

This commit is contained in:
Sakse Dalum 2012-08-12 20:42:24 +02:00
commit 2e72488824
3 changed files with 63 additions and 27 deletions

View File

@ -86,7 +86,8 @@ class Game(object):
def goto_level(self, level):
self.objs.remove(self.level)
if self.level in self.objs:
self.objs.remove(self.level)
self.level_num = level
graphics_dir = os.path.join(self.directory, "resources", "graphics")

View File

@ -29,9 +29,9 @@ import pygame
import worldobject
class Laser(worldobject.WorldObject):
def __init__(self, level, p0, p1):
def __init__(self, level, p0, p1, laser_direction):
self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, p0[0], p0[1])
worldobject.WorldObject.__init__(self, level, p0[0], max(p0[1], p1[1]) + 48)
def update(self, e, t, dt):

View File

@ -115,35 +115,70 @@ class Level4(level.Level):
for ((x0, y0), (x1, y1)), direc in self.lasers_orig:
self.lasers.append(laser.Laser(
self, (x0 * 64, y0 * 48),
(x1 * 64, y1 * 48)))
(x1 * 64, y1 * 48), direc))
def draw(self, window):
self._blit_background(window)
objs = self._sorted_objs()
objs = self._sorted_objs(self.objects + self.lasers)
self._after_sort(itertools.groupby(objs, lambda obj: obj.y + obj.z))
for obj in objs:
obj.draw(window)
for obj in self.lasers:
obj.draw(window)
for obj in objs:
x0, y0 = obj.x / 64, obj.y / 48 - 1
if isinstance(obj, mirror.Mirror):
for (p0, p1), laser_direc in self.lasers_orig:
if p0 == (x0, y0):
mirror_direc = Left if obj.left_up else Right
do_redraw = {
(Left, Up): True,
(Left, Left): True,
(Left, Down): False,
(Left, Right): False,
(Right, Up): True,
(Right, Right): True,
(Right, Down): False,
(Right, Left): False
}[(mirror_direc, laser_direc)]
if do_redraw:
obj.draw(window)
self.darkness.draw(window)
def _after_sort(self, objss):
for objs in objss:
self._after_sort_line(objs)
def _after_sort_line(self, objs):
is_special = lambda obj: isinstance(obj, mirror.Mirror) or isinstance(obj, laser.Laser)
specials, nonspecials = filter(is_special, objs), filter(lambda obj: not is_special(obj), objs)
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
def _is_laser_behind_mirror(mirror_direc, laser_direc):
return {
(Left, Up): True,
(Left, Left): True,
(Left, Down): False,
(Left, Right): False,
(Right, Up): True,
(Right, Right): True,
(Right, Down): False,
(Right, Left): False
}[(mirror_direc, laser_direc)]