Merge branch 'master' of hongabar.org:robotgame

This commit is contained in:
Sakse Dalum 2012-08-08 22:40:33 +02:00
commit 43b3cd791c
5 changed files with 166 additions and 16 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
*~
*.py[co]
*.py[co]
/.old

View File

@ -69,7 +69,7 @@ class Game(object):
self.objs.append(main_menu.MainMenu(self, graphics_dir))
else:
exec 'from level%d import Level%d as level' % (self.initial_goto_level,
self.initial_goto_level)
self.initial_goto_level)
self.goto_level(level(self, graphics_dir))
def run(self):

View File

@ -10,9 +10,11 @@ import block
import boulder
import lever
class Level2(level.Level):
def __init__(self, game, graphics_dir, paused=False):
level.Level.__init__(self, game, graphics_dir, paused)
level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20),
paused=paused)
self.dimensions = 20, 20
@ -21,6 +23,8 @@ class Level2(level.Level):
self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
self.draw_background()
blocks = [block.Block(self, 64*4, 48, self.imgs['block1'])]
self.objects.extend(blocks)

145
robotgame/level3.py Normal file
View File

@ -0,0 +1,145 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# level3.py
# --------------------
# date created : Wed Aug 8 2012
# copyright : (C) 2012 Niels G. W. Serup
# maintained by : Niels G. W. Serup <ns@metanohi.name>
"""
The third level.
"""
import os
import pygame
import random
import re
import itertools
import worldobject
import level
import player
import tile
import block
import boulder
import lever
import logic.colourboxes
class ActionBlock(block.Block):
def __init__(self, level, x, y, action, movable=True):
self.__dict__.update(locals())
block.Block.__init__(
self, level, x, y, img=level.imgs['block1'],
movable=movable)
self._last_pos = None
def update(self, e, t, dt):
if (self.x, self.y) != self._last_pos and not self.is_moving:
self.action(self)
self._last_pos = self.x, self.y
worldobject.WorldObject.update(self, e, t, dt)
class ColorWell(block.Block):
def __init__(self, level, x, y):
self.__dict__.update(locals())
worldobject.WorldObject.__init__(
self, level, x, y, movable=False)
self.img = pygame.Surface((128, 96))
self.set_colour(0, 0, 0)
def set_colour(self, r, g, b):
self.well_colour = r * 255, g * 255, b * 255
self.img.fill(self.well_colour)
class Level3(level.Level):
def __init__(self, game, graphics_dir, paused=False):
level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20),
paused=paused)
self.dimensions = 10, 10
# 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']))
boxes = logic.colourboxes.generate_colour_boxes(1, 3)
boxes += boxes
boxes += [logic.colourboxes.generate_random_box(1) for _ in range(3)]
random.shuffle(boxes)
pos_colour = {}
for box, (x, y) in zip(boxes, itertools.product(range(3, 6), range(3, 6))):
self.tiles.append(tile.Tile(self, x * 64, (y + 1) * 48, self.imgs['ground1']))
pos_colour[(x, y)] = box
blocks = [ActionBlock(self, 64 * i, 0, action=None, movable=True)
for i in range(1, 4)]
self.objects.extend(blocks)
wells = [ColorWell(self, 64 * 5, 0)]
self.objects.extend(wells)
def update_wells(block):
cur_boxes = []
for block in blocks:
box = pos_colour.get((block.x / 64, block.y / 48))
if box:
cur_boxes.append(box)
if not cur_boxes:
well_colours = [(0, 0, 0)] * len(wells)
else:
well_colours = logic.colourboxes.get_colours(cur_boxes)
for i in range(len(wells)):
wells[i].set_colour(*well_colours[i])
for b in blocks:
b.action = update_wells
self.draw_background()
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))
def restart(self):
for obj in self.objects:
obj.reset_pos()
def infrange():
n = 0
while True:
yield n
n += 1

View File

@ -72,20 +72,20 @@ def generate_random_box(nwells):
r = lambda: random.choice((0, 1))
return [(r(), r(), r()) for _ in range(nwells)]
def get_colours(boxes):
colours = []
for i in range(len(boxes[0])):
r, g, b = boxes[0][i]
for j in range(1, len(boxes)):
r1, g1, b1 = boxes[j][i]
r ^= r1
g ^= g1
b ^= b1
colours.append((r, g, b))
return colours
def makes_all_wells_white(boxes):
"""
Determine if the boxes make all wells white when XOR'ed together.
"""
total = 0
for box in boxes:
n = 0
for r, g, b in box:
n <<= 1
n |= r
n <<= 1
n |= g
n <<= 1
n |= b
total ^= n
return total == 2**(len(boxes[0]) * 3) - 1
return all(c == 1 for c in itertools.chain(*get_colours(boxes)))