a-robots-conundrum/robotgame/level1.py

330 lines
13 KiB
Python
Raw Normal View History

# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# level1.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
The first level.
"""
2012-08-07 21:23:10 +02:00
import os
import pygame
import random
2012-08-08 16:46:26 +02:00
import re
2012-08-07 21:23:10 +02:00
import level
import player
2012-08-07 21:23:10 +02:00
import tile
import block
2012-08-08 16:46:26 +02:00
import boulder
2012-08-08 17:06:10 +02:00
import lever
import logic.rollingstone
class Level1(level.Level):
2012-08-08 13:50:48 +02:00
def __init__(self, game, graphics_dir, paused=False):
2012-08-08 22:40:27 +02:00
level.Level.__init__(self, game, graphics_dir, size=(64 * 100,
48 * 100),
2012-08-08 19:53:48 +02:00
paused=paused)
self.dimensions = 50, 50
for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]):
2012-08-07 21:23:10 +02:00
self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
2012-08-09 13:54:27 +02:00
self.player.set_pos(64 * 15, 48 * 15)
2012-08-08 22:40:27 +02:00
self.player.set_init_pos()
2012-08-09 14:26:53 +02:00
for i in range(self.dimensions[0]):
self.objects.append(block.InvisBlock(self, i * 64,
0))
self.objects.append(block.InvisBlock(self, i * 64,
(self.dimensions[1]) * 48))
for i in range(self.dimensions[1]):
self.objects.append(block.InvisBlock(self, -64,
i * 48))
self.objects.append(block.InvisBlock(self, self.dimensions[0] * 64,
i * 48))
2012-08-08 22:40:27 +02:00
### Task 2: Rolling stone
2012-08-08 18:13:53 +02:00
2012-08-09 13:54:27 +02:00
task2_size = 15, 10
2012-08-08 22:40:27 +02:00
task2_pos = (64 * 20, 48 * 20)
playfield, nsteps, directions = (
2012-08-08 22:40:27 +02:00
logic.rollingstone.generate_simple_unsolved_solvable_extra(
2012-08-09 13:54:27 +02:00
task2_size[0], task2_size[1], 7, 30))
2012-08-08 22:40:27 +02:00
for i, j in playfield:
self.objects.append(
block.Block(self,
task2_pos[0] + 64 * i,
task2_pos[1] + 48 * j,
self.imgs['block1'],
movable=True))
2012-08-08 19:53:48 +02:00
arrow_blocks = []
n = 0
for i in directions:
2012-08-09 13:54:27 +02:00
arrow_blocks.append(
block.ArrowBlock(self,
task2_pos[0] + 64 * (task2_size[0] - n),
task2_pos[1] + 48 * (task2_size[1] + 2),
i.next_pos((0, 0))))
2012-08-08 19:53:48 +02:00
n += 1
self.objects.extend(arrow_blocks)
2012-08-08 22:40:27 +02:00
self.objects.append(lever.Lever(self,
task2_pos[0] + 64 * 3,
task2_pos[1] - 48 * 2,
2012-08-08 19:53:48 +02:00
[arrow_block.activate
for arrow_block in arrow_blocks],
2012-08-08 22:40:27 +02:00
toggling=True,
anim='lever_updown'))
2012-08-09 13:54:27 +02:00
b = boulder.Boulder(self, task2_pos[0], task2_pos[1] - 48,
2012-08-08 22:40:27 +02:00
direction=(0, 1))
self.objects.append(b)
self.objects.append(lever.Lever(self,
task2_pos[0] + 64 * 2,
task2_pos[1] - 48 * 2,
[b.activate],
anim='lever_updown'))
2012-08-09 13:54:27 +02:00
# Moat sides
for i in range(-1, task2_size[1]):
self.add_moat(task2_pos[0] - 64,
task2_pos[1] + 48 * i,
'moat_vertical')
for i in range(task2_size[1] - 2):
self.add_moat(task2_pos[0] + 64 * task2_size[0],
task2_pos[1] + 48 * i,
'moat_vertical')
for i in range(6, task2_size[0]):
self.add_moat(task2_pos[0] + 64 * i,
task2_pos[1] - 48,
'moat_horizontal')
for i in range(task2_size[0] - 2):
self.add_moat(task2_pos[0] + 64 * i,
task2_pos[1] + 48 * task2_size[1],
'moat_horizontal')
# Corners
self.add_moat(task2_pos[0] + 64 * task2_size[0],
task2_pos[1] - 48,
'moat_corner_north_flip')
self.add_moat(task2_pos[0] - 64,
task2_pos[1] + 48 * task2_size[1],
'moat_corner_south')
# Start
self.add_moat(task2_pos[0] + 64 * 2,
task2_pos[1] - 48,
'moat_horizontal')
self.add_moat(task2_pos[0] + 64 * 3,
task2_pos[1] - 48,
'moat_end_horizontal_flip')
self.add_moat(task2_pos[0] + 64 * 5,
task2_pos[1] - 48,
'moat_end_horizontal')
self.add_moat(task2_pos[0] - 64,
task2_pos[1] - 48 * 2,
'moat_corner_north')
self.add_moat(task2_pos[0],
task2_pos[1] - 48 * 2,
'moat_horizontal')
self.add_moat(task2_pos[0] + 64,
task2_pos[1] - 48 * 2,
'moat_corner_north_flip')
self.add_moat(task2_pos[0] + 64,
task2_pos[1] - 48,
'moat_corner_south')
# End
self.add_moat(task2_pos[0] + 64 * task2_size[0],
task2_pos[1] + 48 * (task2_size[1] - 2),
'moat_corner_south')
self.add_moat(task2_pos[0] + 64 * (task2_size[0] + 1),
task2_pos[1] + 48 * (task2_size[1] - 2),
'moat_corner_north_flip')
self.add_moat(task2_pos[0] + 64 * (task2_size[0] + 1),
task2_pos[1] + 48 * (task2_size[1] - 1),
'moat_vertical')
self.add_moat(task2_pos[0] + 64 * (task2_size[0] + 1),
task2_pos[1] + 48 * (task2_size[1]),
'moat_corner_south_flip')
self.add_moat(task2_pos[0] + 64 * (task2_size[0]),
task2_pos[1] + 48 * (task2_size[1]),
'moat_end_horizontal')
self.add_moat(task2_pos[0] + 64 * (task2_size[0] - 2),
task2_pos[1] + 48 * (task2_size[1]),
'moat_end_horizontal_flip')
2012-08-08 22:40:27 +02:00
### Task 4: Inverted bits
task4_pos = (64 * 13, 48 * 13)
b = boulder.Boulder(self, task4_pos[0] - 64, task4_pos[1] - 48 * 3,
direction=(1, 0))
self.objects.append(b)
2012-08-08 19:53:48 +02:00
2012-08-08 22:40:27 +02:00
self.objects.append(lever.Lever(self,
2012-08-09 13:54:27 +02:00
task4_pos[0] - 64 * 3,
2012-08-08 22:40:27 +02:00
task4_pos[1] - 48 * 3,
[b.activate]))
risingblocks = [block.RisingBlock(self, task4_pos[0] + 64 * i,
task4_pos[1] - 48 * 3,
is_up = False)
for i in range(8)]
for i in range(8):
r = range(8)
r.remove(i)
2012-08-08 22:40:27 +02:00
self.objects.append(risingblocks[i])
n = random.choice(r)
2012-08-08 22:40:27 +02:00
self.objects.append(lever.Lever(self,
task4_pos[0] + 64 * i,
task4_pos[1] - 48 * 2,
[risingblocks[i].activate,
risingblocks[n].activate],
anim='lever_updown',
toggling=True,
signal=[0, 1]))
for k in range(random.randint(0, 1)):
self.objects[-1].use(self)
self.objects[-1].set_init_pos()
risingblocks[i].set_init_pos()
risingblocks[n].set_init_pos()
n = random.choice(r)
2012-08-08 22:40:27 +02:00
self.objects.append(lever.Lever(self,
task4_pos[0] + 64 * i,
task4_pos[1],
[risingblocks[i].activate,
risingblocks[n].activate],
anim='lever_updown',
toggling=True,
signal=[2, 3]))
2012-08-08 23:08:53 +02:00
for k in range(random.randint(0, 1)):
2012-08-08 22:40:27 +02:00
self.objects[-1].use(self)
self.objects[-1].set_init_pos()
risingblocks[i].set_init_pos()
risingblocks[n].set_init_pos()
2012-08-09 13:54:27 +02:00
# Moat
self.add_moat(task4_pos[0] - 64 * 2, task4_pos[1] - 48 * 4,
'moat_corner_north')
self.add_moat(task4_pos[0] - 64 * 2, task4_pos[1] - 48 * 3,
'moat_vertical')
self.add_moat(task4_pos[0] - 64 * 2, task4_pos[1] - 48 * 2,
'moat_corner_south')
self.add_moat(task4_pos[0] - 64, task4_pos[1] - 48 * 2,
'moat_end_horizontal_flip')
for i in range(10):
self.add_moat(task4_pos[0] + 64 * (i - 1), task4_pos[1] - 48 * 4,
'moat_horizontal')
self.add_moat(task4_pos[0] + 64 * 9, task4_pos[1] - 48 * 4,
'moat_corner_north_flip')
self.add_moat(task4_pos[0] + 64 * 9, task4_pos[1] - 48 * 3,
'moat_vertical')
self.add_moat(task4_pos[0] + 64 * 9, task4_pos[1] - 48 * 2,
'moat_corner_south_flip')
self.add_moat(task4_pos[0] + 64 * 8, task4_pos[1] - 48 * 2,
'moat_end_horizontal')
self.draw_background()
def add_moat(self, x, y, img):
self.tiles.append(tile.Tile(self, x, y, self.imgs[img]))
self.objects.append(block.InvisBlock(self, x, y))
2012-08-08 23:08:53 +02:00
2012-08-07 21:23:10 +02:00
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))
2012-08-09 12:43:44 +02:00
moat_list = ['moat_corner_north',
'moat_corner_south',
'moat_end_horizontal',
'moat_horizontal',
'moat_vertical']
for moat in moat_list:
self.imgs[moat] = pygame.image.load(os.path.join(
2012-08-09 13:54:27 +02:00
self.graphics_dir, 'moat', '%s.png' % moat))
# Special treatment
self.imgs['moat_corner_north_flip'] = pygame.transform.flip(
self.imgs['moat_corner_north'], 1, 0)
self.imgs['moat_corner_south_flip'] = pygame.transform.flip(
self.imgs['moat_corner_south'], 1, 0)
self.imgs['moat_end_horizontal_flip'] = pygame.transform.flip(
self.imgs['moat_end_horizontal'], 1, 0)
2012-08-09 12:43:44 +02:00
2012-08-08 16:46:26 +02:00
# 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')),
2012-08-08 17:06:10 +02:00
('boulder_left', os.path.join('boulder', 'right')),
('lever_updown', os.path.join('lever', 'down-up')),
2012-08-08 17:41:19 +02:00
('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')),]
2012-08-08 16:46:26 +02:00
):
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)
2012-08-08 18:13:53 +02:00
# Special treatment:
if anim == 'arrow_left':
img = pygame.transform.flip(img, 1, 0)
2012-08-08 16:46:26 +02:00
self.imgs[anim].append(img)
def restart(self):
2012-08-08 18:13:53 +02:00
for obj in self.objects:
obj.reset_pos()