Moat has been added to level 1.

This commit is contained in:
Sakse Dalum 2012-08-09 13:54:27 +02:00
parent 1ebd4a9c91
commit 9456b30c8c
4 changed files with 133 additions and 47 deletions

View File

@ -29,12 +29,12 @@ import pygame
import worldobject import worldobject
class Block(worldobject.WorldObject): class Block(worldobject.WorldObject):
def __init__(self, level, x, y, img, movable=False): def __init__(self, level, x, y, img=None, movable=False, visible=True):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, movable=movable) worldobject.WorldObject.__init__(self, level, x, y,
movable=movable, visible=visible)
self.orig_alpha = pygame.surfarray.array_alpha(self.img) self.orig_alpha = pygame.surfarray.array_alpha(self.img)
self.is_currently_opaque = True
def update_alpha(self): def update_alpha(self):
be_opaque = not (self.y + self.z >= self.level.player.y be_opaque = not (self.y + self.z >= self.level.player.y
@ -56,16 +56,22 @@ class Block(worldobject.WorldObject):
self.holder = obj self.holder = obj
def draw(self, window): def draw(self, window):
window.blit(self.img, (self.x - 32 - self.level.camera_x, if self.visible:
self.y - self.img.get_size()[1] + 24 window.blit(self.img, (self.x - 32 - self.level.camera_x,
- self.level.camera_y)) self.y - self.img.get_size()[1] + 24
- self.level.camera_y))
class InvisBlock(Block):
def __init__(self, level, x, y):
self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, visible=False)
class ArrowBlock(Block): class ArrowBlock(Block):
def __init__(self, level, x, y, direction, movable=True, is_up=True): def __init__(self, level, x, y, direction, movable=True, is_up=True):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, worldobject.WorldObject.__init__(self, level, x, y,
direction=direction, movable=movable) direction=direction, movable=movable)
self.is_currently_opaque = True
self.frame = 0 self.frame = 0
self.anim_speed = 15 self.anim_speed = 15
@ -115,8 +121,7 @@ class RisingBlock(Block):
def __init__(self, level, x, y, movable=False, is_up=True): def __init__(self, level, x, y, movable=False, is_up=True):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, movable=movable) worldobject.WorldObject.__init__(self, level, x, y, movable=movable)
self.is_currently_opaque = True
self.frame = 0 self.frame = 0
self.anim_speed = 15 self.anim_speed = 15
self.setting = [0, 0] self.setting = [0, 0]

View File

@ -56,7 +56,7 @@ class Level(object):
if not self.paused: if not self.paused:
for obj in self.objects: for obj in self.objects:
obj.update(e, t, dt) obj.update(e, t, dt)
if self.player.is_moving and hasattr(obj, 'update_alpha'): if self.player.is_moving:
obj.update_alpha() obj.update_alpha()
@ -65,7 +65,7 @@ class Level(object):
self.camera_y = (self.player.y - screen_size[1] / 2 - 24) self.camera_y = (self.player.y - screen_size[1] / 2 - 24)
def draw(self, window): def draw(self, window):
window.blit(self.background, (0 - self.camera_x, -48 - self.camera_y)) window.blit(self.background, (0 - self.camera_x, 0 - self.camera_y))
for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)): for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)):
obj.draw(window) obj.draw(window)

View File

@ -51,18 +51,18 @@ class Level1(level.Level):
self.tiles.append( self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1'])) tile.Tile(self, i*64, j*48, self.imgs['ground1']))
self.draw_background() self.player.set_pos(64 * 15, 48 * 15)
self.player.set_pos(64 * 10, 48 * 10)
self.player.set_init_pos() self.player.set_init_pos()
### Task 2: Rolling stone ### Task 2: Rolling stone
task2_size = 15, 10
task2_pos = (64 * 20, 48 * 20) task2_pos = (64 * 20, 48 * 20)
playfield, nsteps, directions = ( playfield, nsteps, directions = (
logic.rollingstone.generate_simple_unsolved_solvable_extra( logic.rollingstone.generate_simple_unsolved_solvable_extra(
10, 10, 7, 20)) task2_size[0], task2_size[1], 7, 30))
for i, j in playfield: for i, j in playfield:
self.objects.append( self.objects.append(
@ -75,10 +75,11 @@ class Level1(level.Level):
arrow_blocks = [] arrow_blocks = []
n = 0 n = 0
for i in directions: for i in directions:
arrow_blocks.append(block.ArrowBlock(self, arrow_blocks.append(
task2_pos[0] - 64, block.ArrowBlock(self,
task2_pos[1] + 48 * n, task2_pos[0] + 64 * (task2_size[0] - n),
i.next_pos((0, 0)))) task2_pos[1] + 48 * (task2_size[1] + 2),
i.next_pos((0, 0))))
n += 1 n += 1
self.objects.extend(arrow_blocks) self.objects.extend(arrow_blocks)
@ -90,7 +91,7 @@ class Level1(level.Level):
toggling=True, toggling=True,
anim='lever_updown')) anim='lever_updown'))
b = boulder.Boulder(self, task2_pos[0], task2_pos[1], b = boulder.Boulder(self, task2_pos[0], task2_pos[1] - 48,
direction=(0, 1)) direction=(0, 1))
self.objects.append(b) self.objects.append(b)
@ -99,6 +100,73 @@ class Level1(level.Level):
task2_pos[1] - 48 * 2, task2_pos[1] - 48 * 2,
[b.activate], [b.activate],
anim='lever_updown')) anim='lever_updown'))
# 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')
### Task 4: Inverted bits ### Task 4: Inverted bits
@ -109,7 +177,7 @@ class Level1(level.Level):
self.objects.append(b) self.objects.append(b)
self.objects.append(lever.Lever(self, self.objects.append(lever.Lever(self,
task4_pos[0] - 64 * 2, task4_pos[0] - 64 * 3,
task4_pos[1] - 48 * 3, task4_pos[1] - 48 * 3,
[b.activate])) [b.activate]))
@ -152,32 +220,32 @@ class Level1(level.Level):
risingblocks[i].set_init_pos() risingblocks[i].set_init_pos()
risingblocks[n].set_init_pos() risingblocks[n].set_init_pos()
self.objects.append(block.Block(self, task4_pos[0] + 64 * i, # Moat
task4_pos[1] - 48 * 4, self.add_moat(task4_pos[0] - 64 * 2, task4_pos[1] - 48 * 4,
self.imgs['block1'])) '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()
self.objects.append(block.Block(self, task4_pos[0] - 64, def add_moat(self, x, y, img):
task4_pos[1] - 48 * 2, self.tiles.append(tile.Tile(self, x, y, self.imgs[img]))
self.imgs['block1'])) self.objects.append(block.InvisBlock(self, x, y))
self.objects.append(block.Block(self, task4_pos[0] - 64,
task4_pos[1] - 48 * 4,
self.imgs['block1']))
self.objects.append(block.Block(self, task4_pos[0] + 64 * 8,
task4_pos[1] - 48 * 4,
self.imgs['block1']))
self.objects.append(block.Block(self, task4_pos[0] + 64 * 8,
task4_pos[1] - 48 * 2,
self.imgs['block1']))
self.objects.append(block.Block(self, task4_pos[0] + 64 * 9,
task4_pos[1] - 48 * 4,
self.imgs['block1']))
self.objects.append(block.Block(self, task4_pos[0] + 64 * 9,
task4_pos[1] - 48 * 3,
self.imgs['block1']))
self.objects.append(block.Block(self, task4_pos[0] + 64 * 9,
task4_pos[1] - 48 * 2,
self.imgs['block1']))
def load(self): def load(self):
"""Load all resources used in the level.""" """Load all resources used in the level."""
@ -199,7 +267,15 @@ class Level1(level.Level):
'moat_vertical'] 'moat_vertical']
for moat in moat_list: for moat in moat_list:
self.imgs[moat] = pygame.image.load(os.path.join( self.imgs[moat] = pygame.image.load(os.path.join(
self.graphics_dir, 'moat', '%s.png' % block)) 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)
# Load animations # Load animations
for anim, directory in ( for anim, directory in (

View File

@ -31,7 +31,7 @@ import copy
class WorldObject(object): class WorldObject(object):
def __init__(self, level, x, y, z=0, direction=(1, 0), speed=4, def __init__(self, level, x, y, z=0, direction=(1, 0), speed=4,
tile_x=64, tile_y=48, tile_x=64, tile_y=48,
movable=False, blocking=True, is_moving=False): movable=False, blocking=True, is_moving=False, visible=True):
self.__dict__.update(locals()) self.__dict__.update(locals())
self.init_x = self.move_x = self.x = x - (x % self.tile_x) self.init_x = self.move_x = self.x = x - (x % self.tile_x)
@ -41,6 +41,8 @@ class WorldObject(object):
self.holding = None self.holding = None
self.holder = None self.holder = None
self.is_currently_opaque = True
if hasattr(self, 'img'): if hasattr(self, 'img'):
self.img = copy.copy(self.img) self.img = copy.copy(self.img)
@ -98,6 +100,9 @@ class WorldObject(object):
def activate(self, setting): def activate(self, setting):
pass pass
def update_alpha(self):
pass
def set_alpha(self, value): def set_alpha(self, value):
""" """
Set the relative translucency of the per-pixel-alpha image. Set the relative translucency of the per-pixel-alpha image.