Merge branch 'master' of hongabar.org:robotgame

This commit is contained in:
Sakse Dalum 2012-08-10 22:31:21 +02:00
commit 3b50af291c
4 changed files with 45 additions and 136 deletions

View File

@ -23,7 +23,7 @@ class Level2(level.Level):
for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]):
self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
tile.Tile(self, i*64, (j + 1)*48, self.imgs['indoor%d' % random.randint(1, 6)]))
self.draw_background()
@ -45,45 +45,6 @@ class Level2(level.Level):
anim='lever_updown'))
def load(self):
"""Load all resources used in the level."""
tile_list = ['ground1', 'ground2']
for tile in tile_list:
self.imgs[tile] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % tile))
block_list = ['block1']
for block in block_list:
self.imgs[block] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % block))
# Load animations
for anim, directory in (
[('lever_updown', os.path.join('lever', 'down-up')),
]
):
self.imgs[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.graphics_dir, directory)):
for f in files:
if re.match(r"^.*\.(png)$", '/'.join([root, f])):
anim_files.append('/'.join([root, f]))
# Sort and load the files
for f in sorted(anim_files):
img = pygame.image.load(f)
# Special treatment:
if anim == 'arrow_left':
img = pygame.transform.flip(img, 1, 0)
self.imgs[anim].append(img)
def restart(self):
for obj in self.objects:
obj.reset_pos()

View File

@ -57,32 +57,38 @@ class Level3(level.Level):
# tile.Tile(self, i*64, j*48, self.imgs['ground1']))
task_start = (2, 2)
task_size = (5, 5)
# Abstract "boxes", actually colour fields
boxes = [box + [(0, 0, 0)] * 2 for box in logic.colourboxes.generate_colour_boxes(2, 3)]
boxes += [[(0, 0, 0)] * 2 + box for box in logic.colourboxes.generate_colour_boxes(2, 3)]
boxes += [logic.colourboxes.generate_random_box(4) for _ in range(9)]
boxes += [[(0, 0, 0)] * 4 for _ in range(9)]
boxes = []
for i in range(4):
boxes.extend([(0, 0, 0)] * i + box + [(0, 0, 0)] * (3 - i)
for box in logic.colourboxes.generate_colour_boxes(1, 3))
boxes.extend(logic.colourboxes.generate_random_box(4, 2) for _ in range(20))
boxes.extend([(0, 0, 0)] * 4 for _ in range(10))
random.shuffle(boxes)
pos_colour = {}
for box, (x, y) in zip(boxes, itertools.product(range(6), range(4))):
for box, (x, y) in zip(boxes, itertools.product(range(7), range(6))):
self.tiles.append(tile.Tile(self, 64 * (x + task_start[0] + 1),
48 * (y + task_start[1] + 1),
self.imgs['ground1']))
self.imgs['indoor%d' % random.randint(1, 6)]))
pos_colour[(x, y)] = box
self.draw_background()
action_blocks = [block.ActionBlock(self, 64 * (i + 1 + task_start[0]),
48 * task_start[1], movable=True)
for i in range(6)]
action_blocks = list(itertools.chain(*
[(block.ActionBlock(self, 64 * task_start[0],
48 * (i + 1 + task_start[1]),
movable=True),
block.ActionBlock(self, 64 * (task_start[0] + 8),
48 * (i + 1 + task_start[1]),
movable=True))
for i in range(6)]))
self.objects.extend(action_blocks)
wells = [block.ColorWell(self, task_start[0] * 64, task_start[1] * 48),
block.ColorWell(self, (task_start[0] + 7) * 64, task_start[1] * 48),
block.ColorWell(self, task_start[0] * 64, (task_start[1] + 5) * 48),
block.ColorWell(self, (task_start[0] + 7) * 64, (task_start[1] + 5) * 48),
block.ColorWell(self, (task_start[0] + 8) * 64, task_start[1] * 48),
block.ColorWell(self, task_start[0] * 64, (task_start[1] + 7) * 48),
block.ColorWell(self, (task_start[0] + 8) * 64, (task_start[1] + 7) * 48),
]
self.objects.extend(wells)
@ -105,45 +111,6 @@ class Level3(level.Level):
b.action = update_wells
def load(self):
"""Load all resources used in the level."""
tile_list = ['ground1', 'ground2']
for tile in tile_list:
self.imgs[tile] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % tile))
block_list = ['block1', 'block1_lifted']
for block in block_list:
self.imgs[block] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % block))
# Load animations
for anim, directory in (
[('lever_updown', os.path.join('lever', 'down-up')),
]
):
self.imgs[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.graphics_dir, directory)):
for f in files:
if re.match(r"^.*\.(png)$", '/'.join([root, f])):
anim_files.append('/'.join([root, f]))
# Sort and load the files
for f in sorted(anim_files):
img = pygame.image.load(f)
# Special treatment:
if anim == 'arrow_left':
img = pygame.transform.flip(img, 1, 0)
self.imgs[anim].append(img)
def restart(self):
for obj in self.objects:
obj.reset_pos()

View File

@ -49,7 +49,7 @@ class Level(level.Level):
for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]):
self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
tile.Tile(self, (i+1)*64, j*48, self.imgs['indoor%d' % random.randint(1, 6)]))
self.draw_background()
@ -73,46 +73,6 @@ class Level(level.Level):
toggling=False,
anim='lever_updown'))
def load(self):
"""Load all resources used in the level."""
tile_list = ['ground1', 'ground2']
for tile in tile_list:
self.imgs[tile] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % tile))
block_list = ['block1', 'block1_lifted']
for block in block_list:
self.imgs[block] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % block))
# Load animations
for anim, directory in (
[('lever_updown', os.path.join('lever', 'down-up')),
]
):
self.imgs[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.graphics_dir, directory)):
for f in files:
if re.match(r"^.*\.(png)$", '/'.join([root, f])):
anim_files.append('/'.join([root, f]))
# Sort and load the files
for f in sorted(anim_files):
img = pygame.image.load(f)
# Special treatment:
if anim == 'arrow_left':
img = pygame.transform.flip(img, 1, 0)
self.imgs[anim].append(img)
def enter(self, root_level):
self.__dict__.update(locals())
self.game.objs.remove(root_level)

View File

@ -91,14 +91,35 @@ def _get_oxs(x):
else (x - 1, x + 1) if x % 3 == 1 \
else (x - 2, x - 1)
def generate_random_box(nwells):
"""Generate a box with random colors except white (111)."""
def generate_random_box(nwells, min_nonblacks=0):
"""
Generate a box that triggers nwells wells, with random colors except white
(111).
Arguments:
min_nonblacks -- minimum number of well colours in a box required not to be
black.
"""
def gen_wc():
wc = [random.choice((0, 1)) for i in range(3)]
if all(b == 1 for b in wc):
wc[random.randrange(3)] = 0
return wc
return [tuple(gen_wc()) for _ in range(nwells)]
def gen_wc_nonblack():
wc = gen_wc()
if all(b == 0 for b in wc):
wc[random.randrange(3)] = 1
return wc
colours = [tuple(gen_wc()) for _ in range(nwells)]
nonblack = lambda t: any(n == 1 for n in t)
missing_nonblacks = min_nonblacks - len(list(filter(nonblack, colours)))
i = 0
while missing_nonblacks > 0:
if not nonblack(colours[i]):
colours[i] = gen_wc_nonblack()
missing_nonblacks -= 1
i += 1
return colours
def get_colours(boxes):
colours = []