# 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 . # # ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # # level1.py # -------------------- # date created : Tue Aug 7 2012 # copyright : (C) 2012 Sakse Dalum # maintained by : Sakse Dalum """ The first level. """ import os import pygame import random import re import level import player import tile import block import boulder import lever import logic.rollingstone class Level1(level.Level): def __init__(self, game, graphics_dir, paused=False): level.Level.__init__(self, game, graphics_dir, size=(64 * 100, 48 * 100), paused=paused) self.dimensions = 50, 50 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'])) self.draw_background() # for i in range(10): # self.objects.append(block.Block(self, random.randint(0, 10)*64, # random.randint(0, 10)*48, # self.imgs['block1'], # movable=True)) self.player.set_pos(64 * 10, 48 * 10) self.player.set_init_pos() ### Task 2: Rolling stone task2_pos = (64 * 20, 48 * 20) playfield, nsteps, directions = ( logic.rollingstone.generate_simple_unsolved_solvable_extra( 10, 10, 7, 20)) 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)) arrow_blocks = [] n = 0 for i in directions: arrow_blocks.append(block.ArrowBlock(self, task2_pos[0] - 64, task2_pos[1] + 48 * n, i.next_pos((0, 0)))) n += 1 self.objects.extend(arrow_blocks) self.objects.append(lever.Lever(self, task2_pos[0] + 64 * 3, task2_pos[1] - 48 * 2, [arrow_block.activate for arrow_block in arrow_blocks], toggling=True, anim='lever_updown')) b = boulder.Boulder(self, task2_pos[0], task2_pos[1], 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')) ### 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) self.objects.append(lever.Lever(self, task4_pos[0] - 64 * 2, 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): self.objects.append(risingblocks[i]) n = random.randint(0, 7) 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.randint(0, 7) 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])) for i 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() 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 ( [('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')), ('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 restart(self): for obj in self.objects: obj.reset_pos()