Merge branch 'master' of hongabar.org:robotgame

This commit is contained in:
Lasse 2012-08-11 02:21:06 +02:00
commit 1912a84da9
9 changed files with 356 additions and 298 deletions

View File

@ -32,6 +32,7 @@ import jukebox
import level import level
import main_menu import main_menu
import game_menu import game_menu
import loader
class Game(object): class Game(object):
"""Create an object to handle the game.""" """Create an object to handle the game."""
@ -56,6 +57,8 @@ class Game(object):
def load(self): def load(self):
graphics_dir = os.path.join(self.directory, "resources", "graphics") graphics_dir = os.path.join(self.directory, "resources", "graphics")
self.loader = loader.Loader(graphics_dir)
self.loader.load()
self.level = None self.level = None
self.jukebox = jukebox.Jukebox( self.jukebox = jukebox.Jukebox(

View File

@ -35,9 +35,7 @@ class Level(object):
self.tiles = [] self.tiles = []
self.objects = [] self.objects = []
self.imgs = {} self.imgs = game.loader.imgs
self.load()
self.reverse_imgs = ( self.reverse_imgs = (
dict([(val, key) for (key, val) dict([(val, key) for (key, val)

View File

@ -37,6 +37,7 @@ import block
import boulder import boulder
import lever import lever
import trigger import trigger
import worldobject
import logic.rollingstone import logic.rollingstone
import logic.colourboxes import logic.colourboxes
@ -72,6 +73,60 @@ class Level1(level.Level):
self.objects.append(block.InvisBlock(self, self.dimensions[0] * 64, self.objects.append(block.InvisBlock(self, self.dimensions[0] * 64,
i * 48)) i * 48))
### Task 1: Wheels of fortune
task1_pos = (64 * 15, 48 * 13)
ws = []
for i in range(5):
w1 = Wheel(self, task1_pos[0] + 64 * i, task1_pos[1] - 48 * 2)
w2 = Wheel(self, task1_pos[0] + 64 * i, task1_pos[1],
immitate=w1)
self.objects.extend([w1, w2])
self.objects.append(lever.Lever(self,
task1_pos[0] + 64 * i,
task1_pos[1] + 48 * 1,
[w2.activate],
anim='lever_updown'))
ws.append(w2)
self.add_tile(task1_pos[0] + 64 * i,
task1_pos[1] - 48 * 3,
'moat_horizontal')
self.objects.append(
lever.Lever(self,
task1_pos[0] + 64 * 5,
task1_pos[1] + 48 * 2,
[lambda *xs: self.complete_task(1)
if len(ws) == len([w for w in ws if w.on])
else lambda: None]))
self.add_tile(task1_pos[0] - 64,
task1_pos[1] - 48 * 3,
'moat_corner_north')
self.add_tile(task1_pos[0] + 64 * 5,
task1_pos[1] - 48 * 3,
'moat_corner_north_flip')
for i in range(3):
self.add_tile(task1_pos[0] - 64,
task1_pos[1] + 48 * (i - 2),
'moat_vertical')
self.add_tile(task1_pos[0] + 64 * 5,
task1_pos[1] + 48 * (i - 2),
'moat_vertical')
self.add_tile(task1_pos[0] - 64,
task1_pos[1] + 48,
'moat_corner_south_flip')
self.add_tile(task1_pos[0] + 64 * 5,
task1_pos[1] + 48,
'moat_corner_south')
self.add_tile(task1_pos[0] - 64 * 2,
task1_pos[1] + 48,
'moat_end_horizontal')
self.add_tile(task1_pos[0] + 64 * 6,
task1_pos[1] + 48,
'moat_end_horizontal_flip')
### Task 2: Rolling stone ### Task 2: Rolling stone
task2_size = 15, 10 task2_size = 15, 10
@ -88,7 +143,13 @@ class Level1(level.Level):
task2_pos[0] + 64 * i, task2_pos[0] + 64 * i,
task2_pos[1] + 48 * j, task2_pos[1] + 48 * j,
self.imgs['block1'], self.imgs['block1'],
movable=True)) movable=True)
if (random.randint(0, 2) or (i, j) == (4, 0)) else
block.Block(self,
task2_pos[0] + 64 * i,
task2_pos[1] + 48 * j,
self.imgs['block3'],
movable=False))
arrow_blocks = [] arrow_blocks = []
n = 0 n = 0
@ -210,7 +271,7 @@ class Level1(level.Level):
# y * 48 + task3_pos[1], # y * 48 + task3_pos[1],
# self.imgs['ground1'])) # self.imgs['ground1']))
pos_colour[(x, y)] = box pos_colour[(x, y)] = box
self.add_tile(task3_pos[0] + 64 * x, task3_pos[1] + 48 * y, self.add_tile(task3_pos[0] + 64 * x, task3_pos[1] + 48 * (y + 1),
'indoor%d' % random.randint(1, 6), blocking=False) 'indoor%d' % random.randint(1, 6), blocking=False)
action_blocks = [block.ActionBlock(self, 64 * i + task3_pos[0], action_blocks = [block.ActionBlock(self, 64 * i + task3_pos[0],
@ -338,87 +399,66 @@ class Level1(level.Level):
if blocking: if blocking:
self.objects.append(block.InvisBlock(self, x, y)) self.objects.append(block.InvisBlock(self, x, y))
def load(self):
"""Load all resources used in the level."""
l = ['ground1', 'ground2']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % o))
for o in range(1, 7):
self.imgs['indoor%d' % o] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', 'indoor', 'ground%02d.png' % o))
l = ['block1', 'block1_lifted']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % o))
l = ['hole', 'well']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, '%s.png' % o))
l = ['moat_corner_north',
'moat_corner_south',
'moat_corner_north_flip',
'moat_corner_south_flip',
'moat_end_horizontal',
'moat_horizontal',
'moat_vertical']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, 'moat', '%s.png' % o))
# Special treatment
self.imgs['moat_end_horizontal_flip'] = pygame.transform.flip(
self.imgs['moat_end_horizontal'], 1, 0)
# Load animations
for anim, directory in (
[('boulder_up', os.path.join('boulder', 'up')),
('boulder_down', os.path.join('boulder', 'down')),
('boulder_right', os.path.join('boulder', 'right')),
('boulder_left', os.path.join('boulder', 'right')),
('boulder_falling', os.path.join('boulder_fall')),
('lever_updown', os.path.join('lever', 'down-up')),
('lever_leftright', os.path.join('lever', 'left-right')),
('arrow_up', os.path.join('matt', 'up')),
('arrow_right', os.path.join('matt', 'right')),
('arrow_down', os.path.join('matt', 'down')),
('arrow_left', os.path.join('matt', 'right')),]
):
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 complete_task(self, task): def complete_task(self, task):
if task == 0: if task == 0:
return return
self.task_completions.append(task) self.task_completions.append(task)
if len(self.task_completions) > 3: if len(self.task_completions) > 3:
self.task_completions.remove(self.task_completions[0]) self.task_completions = self.task_completions[-3:]
print(self.task_completions) print(self.task_completions)
def restart(self): def restart(self):
for obj in self.objects: for obj in self.objects:
obj.reset_pos() obj.reset_pos()
class Wheel(block.Block):
def __init__(self, level, x, y, immitate=None):
self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y)
self.frame = 0
self.anim_speed = 15
self.nsettings = 4
self.setting = random.randint(0, self.nsettings - 1)
if self.immitate:
self.on = self.setting == self.immitate.setting
self.init_setting = self.setting
self.anim = 'stairs'
self.anim_direction = 1
def set_init_pos(self):
worldobject.WorldObject.set_init_pos(self)
self.init_setting = self.setting
def reset_pos(self):
worldobject.WorldObject.reset_pos(self)
self.setting = self.init_setting
def set_direction(self, setting):
self.anim_direction = -1 if setting else 1
def activate(self, setting):
self.setting = (self.setting + 1 * self.anim_direction) % self.nsettings
self.on = self.setting == self.immitate.setting
def update(self, e, t, dt):
# Update the animation
l = len(self.level.imgs[self.anim])
if not (int(self.frame) == self.setting * l / self.nsettings):
self.frame = ((self.frame + self.anim_speed * dt) % l
if (self.setting > 0 and self.anim_direction > 0)
or (self.setting == self.nsettings - 1
and self.anim_direction < 0) else
(self.frame - self.anim_speed * dt) % l)
worldobject.WorldObject.update(self, e, t, dt)
def draw(self, window):
self.img = self.level.imgs[self.anim][int(self.frame)]
if self.visible:
window.blit(self.img, (self.x - 32 - self.level.camera_x,
self.y - self.img.get_size()[1] + 24
- self.level.camera_y))

View File

@ -23,7 +23,7 @@ class Level2(level.Level):
for i in range(self.dimensions[0]): for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]): for j in range(self.dimensions[1]):
self.tiles.append( 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() self.draw_background()
@ -45,45 +45,6 @@ class Level2(level.Level):
anim='lever_updown')) 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): def restart(self):
for obj in self.objects: for obj in self.objects:
obj.reset_pos() obj.reset_pos()

View File

@ -57,32 +57,38 @@ class Level3(level.Level):
# tile.Tile(self, i*64, j*48, self.imgs['ground1'])) # tile.Tile(self, i*64, j*48, self.imgs['ground1']))
task_start = (2, 2) task_start = (2, 2)
task_size = (5, 5)
# Abstract "boxes", actually colour fields # Abstract "boxes", actually colour fields
boxes = [box + [(0, 0, 0)] * 2 for box in logic.colourboxes.generate_colour_boxes(2, 3)] boxes = []
boxes += [[(0, 0, 0)] * 2 + box for box in logic.colourboxes.generate_colour_boxes(2, 3)] for i in range(4):
boxes += [logic.colourboxes.generate_random_box(4) for _ in range(9)] boxes.extend([(0, 0, 0)] * i + box + [(0, 0, 0)] * (3 - i)
boxes += [[(0, 0, 0)] * 4 for _ in range(9)] 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) random.shuffle(boxes)
pos_colour = {} 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), self.tiles.append(tile.Tile(self, 64 * (x + task_start[0] + 1),
48 * (y + task_start[1] + 1), 48 * (y + task_start[1] + 1),
self.imgs['ground1'])) self.imgs['indoor%d' % random.randint(1, 6)]))
pos_colour[(x, y)] = box pos_colour[(x, y)] = box
self.draw_background() self.draw_background()
action_blocks = [block.ActionBlock(self, 64 * (i + 1 + task_start[0]), action_blocks = list(itertools.chain(*
48 * task_start[1], movable=True) [(block.ActionBlock(self, 64 * task_start[0],
for i in range(6)] 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) self.objects.extend(action_blocks)
wells = [block.ColorWell(self, task_start[0] * 64, task_start[1] * 48), 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] + 8) * 64, task_start[1] * 48),
block.ColorWell(self, task_start[0] * 64, (task_start[1] + 5) * 48), block.ColorWell(self, task_start[0] * 64, (task_start[1] + 7) * 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] + 7) * 48),
] ]
self.objects.extend(wells) self.objects.extend(wells)
@ -105,45 +111,6 @@ class Level3(level.Level):
b.action = update_wells 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): def restart(self):
for obj in self.objects: for obj in self.objects:
obj.reset_pos() obj.reset_pos()

View File

@ -49,7 +49,7 @@ class Level(level.Level):
for i in range(self.dimensions[0]): for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]): for j in range(self.dimensions[1]):
self.tiles.append( 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() self.draw_background()
@ -73,46 +73,6 @@ class Level(level.Level):
toggling=False, toggling=False,
anim='lever_updown')) 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): def enter(self, root_level):
self.__dict__.update(locals()) self.__dict__.update(locals())
self.game.objs.remove(root_level) self.game.objs.remove(root_level)

189
robotgame/loader.py Normal file
View File

@ -0,0 +1,189 @@
# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# loader.py
# --------------------
# date created : Fri Aug 10 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
A loader object.
"""
import pygame
import os
import re
class Loader(object):
def __init__(self, directory):
self.__dict__.update(locals())
self.imgs = {}
def load(self):
"""Load all game resources."""
l = ['ground1', 'ground2']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, 'tiles', '%s.png' % o))
for o in range(1, 7):
self.imgs['indoor%d' % o] = pygame.image.load(os.path.join(
self.directory, 'tiles', 'indoor', 'ground%02d.png' % o))
l = ['block1', 'block1_lifted', 'block3']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, 'blocks', '%s.png' % o))
l = ['hole', 'well']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, '%s.png' % o))
l = ['moat_corner_north',
'moat_corner_south',
'moat_corner_north_flip',
'moat_corner_south_flip',
'moat_end_horizontal',
'moat_horizontal',
'moat_vertical']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, 'moat', '%s.png' % o))
# Special treatment
self.imgs['moat_end_horizontal_flip'] = pygame.transform.flip(
self.imgs['moat_end_horizontal'], 1, 0)
# Load animations
for anim, directory in (
[('boulder_up', os.path.join('boulder', 'up')),
('boulder_down', os.path.join('boulder', 'down')),
('boulder_right', os.path.join('boulder', 'right')),
('boulder_left', os.path.join('boulder', 'right')),
('boulder_falling', os.path.join('boulder_fall')),
('lever_updown', os.path.join('lever', 'down-up')),
('lever_leftright', os.path.join('lever', 'left-right')),
('arrow_up', os.path.join('matt', 'up')),
('arrow_right', os.path.join('matt', 'right')),
('arrow_down', os.path.join('matt', 'down')),
('arrow_left', os.path.join('matt', 'right')),
('stairs', 'stairs')]
):
self.imgs[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.directory, 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)
####### PLAYER #######
for anim, directory in (
[('idle_up', os.path.join('robot_idle', 'up')),
('idle_down', os.path.join('robot_idle', 'down')),
('idle_right', os.path.join('robot_idle', 'right')),
('idle_left', os.path.join('robot_idle', 'right')),
('carry_up', os.path.join('robot_carry', 'up')),
('carry_down', os.path.join('robot_carry', 'down')),
('carry_right', os.path.join('robot_carry', 'right')),
('carry_left', os.path.join('robot_carry', 'right')),
# Lever
('lever_down_right', os.path.join('robot_lever', 'horizontal',
'down_left')),
('lever_down_left', os.path.join('robot_lever', 'horizontal',
'down_left')),
('lever_down_up', os.path.join('robot_lever', 'vertical',
'down_down')),
('lever_down_down', os.path.join('robot_lever', 'vertical',
'down_down')),
('lever_up_right', os.path.join('robot_lever', 'horizontal',
'up_right')),
('lever_up_left', os.path.join('robot_lever', 'horizontal',
'up_right')),
('lever_up_up', os.path.join('robot_lever', 'vertical',
'up_up')),
('lever_up_down', os.path.join('robot_lever', 'vertical',
'up_up')),
('lever_right_right', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_right_left', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_right_up', os.path.join('robot_lever', 'vertical',
'left_up')),
('lever_right_down', os.path.join('robot_lever', 'vertical',
'right_down')),
('lever_left_right', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_left_left', os.path.join('robot_lever', 'horizontal',
'left_left')),
('lever_left_up', os.path.join('robot_lever', 'vertical',
'left_up')),
('lever_left_down', os.path.join('robot_lever', 'vertical',
'right_down'))
]
):
self.imgs['robot_' + anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.directory, 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 in ['idle_left', 'carry_left',
'lever_down_right', 'lever_left_down',
'lever_up_left', 'lever_right_up',
'lever_left_right']:
img = pygame.transform.flip(img, 1, 0)
if anim in ['lever_right_left', 'lever_left_right',
'lever_down_up', 'lever_up_down']:
self.imgs['robot_' + anim].insert(0, img)
else:
self.imgs['robot_' + anim].append(img)

View File

@ -91,14 +91,35 @@ def _get_oxs(x):
else (x - 1, x + 1) if x % 3 == 1 \ else (x - 1, x + 1) if x % 3 == 1 \
else (x - 2, x - 1) else (x - 2, x - 1)
def generate_random_box(nwells): def generate_random_box(nwells, min_nonblacks=0):
"""Generate a box with random colors except white (111).""" """
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(): def gen_wc():
wc = [random.choice((0, 1)) for i in range(3)] wc = [random.choice((0, 1)) for i in range(3)]
if all(b == 1 for b in wc): if all(b == 1 for b in wc):
wc[random.randrange(3)] = 0 wc[random.randrange(3)] = 0
return wc 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): def get_colours(boxes):
colours = [] colours = []

View File

@ -47,89 +47,8 @@ class Player(worldobject.WorldObject):
self.ignore_list.append(boulder.Boulder) self.ignore_list.append(boulder.Boulder)
self.load() self.imgs = dict([(k[6:], v) for (k, v) in self.level.imgs.iteritems()
if k.startswith('robot_')])
def load(self):
self.imgs = {}
for anim, directory in (
[('idle_up', os.path.join('robot_idle', 'up')),
('idle_down', os.path.join('robot_idle', 'down')),
('idle_right', os.path.join('robot_idle', 'right')),
('idle_left', os.path.join('robot_idle', 'right')),
('carry_up', os.path.join('robot_carry', 'up')),
('carry_down', os.path.join('robot_carry', 'down')),
('carry_right', os.path.join('robot_carry', 'right')),
('carry_left', os.path.join('robot_carry', 'right')),
# Lever
('lever_down_right', os.path.join('robot_lever', 'horizontal',
'down_left')),
('lever_down_left', os.path.join('robot_lever', 'horizontal',
'down_left')),
('lever_down_up', os.path.join('robot_lever', 'vertical',
'down_down')),
('lever_down_down', os.path.join('robot_lever', 'vertical',
'down_down')),
('lever_up_right', os.path.join('robot_lever', 'horizontal',
'up_right')),
('lever_up_left', os.path.join('robot_lever', 'horizontal',
'up_right')),
('lever_up_up', os.path.join('robot_lever', 'vertical',
'up_up')),
('lever_up_down', os.path.join('robot_lever', 'vertical',
'up_up')),
('lever_right_right', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_right_left', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_right_up', os.path.join('robot_lever', 'vertical',
'left_up')),
('lever_right_down', os.path.join('robot_lever', 'vertical',
'right_down')),
('lever_left_right', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_left_left', os.path.join('robot_lever', 'horizontal',
'left_left')),
('lever_left_up', os.path.join('robot_lever', 'vertical',
'left_up')),
('lever_left_down', os.path.join('robot_lever', 'vertical',
'right_down'))
]
):
self.imgs[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.level.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 in ['idle_left', 'carry_left',
'lever_down_right', 'lever_left_down',
'lever_up_left', 'lever_right_up']:
img = pygame.transform.flip(img, 1, 0)
if anim in ['lever_right_left', 'lever_left_right',
'lever_down_up', 'lever_up_down']:
self.imgs[anim].insert(0, img)
else:
self.imgs[anim].append(img)
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
def direction_as_string(self): def direction_as_string(self):
if self.direction == (1, 0): if self.direction == (1, 0):