Worked on task 4.

This commit is contained in:
Sakse Dalum 2012-08-08 22:40:27 +02:00
parent 67611d3ad1
commit 48a4a2ee3b
6 changed files with 157 additions and 31 deletions

View File

@ -104,3 +104,55 @@ class ArrowBlock(Block):
def draw(self, window): def draw(self, window):
self.img = self.level.imgs[self.anim][int(self.frame)] self.img = self.level.imgs[self.anim][int(self.frame)]
Block.draw(self, window) Block.draw(self, window)
class RisingBlock(Block):
def __init__(self, level, x, y, movable=False, is_up=True):
self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, movable=movable)
self.frame = 0
self.anim_speed = 15
self.setting = [0, 0]
self.init_setting = self.setting
self.anim = 'arrow_right'
def set_init_pos(self):
worldobject.WorldObject.set_init_pos(self)
self.init_setting = [self.setting[0], self.setting[1]]
def reset_pos(self):
worldobject.WorldObject.reset_pos(self)
self.setting = [self.init_setting[0], self.init_setting[1]]
self.is_up = self.setting[1] == self.setting[0]
def activate(self, setting):
if setting in [2, 3]:
self.setting[1] = setting - 2
else:
self.setting[0] = setting
if not self.share_tile(worldobject.WorldObject):
self.is_up = self.setting[1] == self.setting[0]
def update(self, e, t, dt):
# Update the animation
if not self.is_up:
self.frame = min(self.frame + self.anim_speed * dt,
len(self.level.imgs[self.anim]) - 1)
else:
self.frame = max(self.frame - self.anim_speed * dt,
0)
if self.frame == len(self.level.imgs[self.anim]) - 1:
self.blocking = False
self.z = -48
else:
self.blocking = True
self.z = 0
worldobject.WorldObject.update(self, e, t, dt)
def draw(self, window):
self.img = self.level.imgs[self.anim][int(self.frame)]
Block.draw(self, window)

View File

@ -38,7 +38,7 @@ class Boulder(worldobject.WorldObject):
self.anim = 'boulder_right' self.anim = 'boulder_right'
self.frame = 0 self.frame = 0
self.anim_speed = 30 self.anim_speed = 60
def activate(self, setting): def activate(self, setting):
self.rolling = True self.rolling = True
@ -77,4 +77,3 @@ class Boulder(worldobject.WorldObject):
window.blit(self.img, (self.x - 32 - self.level.camera_x, window.blit(self.img, (self.x - 32 - self.level.camera_x,
self.y - self.img.get_size()[1] + 24 self.y - self.img.get_size()[1] + 24
- self.level.camera_y)) - self.level.camera_y))

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 hasattr(obj, 'update_alpha'): if self.player.is_moving and hasattr(obj, 'update_alpha'):
obj.update_alpha() obj.update_alpha()

View File

@ -40,7 +40,8 @@ import logic.rollingstone
class Level1(level.Level): class Level1(level.Level):
def __init__(self, game, graphics_dir, paused=False): def __init__(self, game, graphics_dir, paused=False):
level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20), level.Level.__init__(self, game, graphics_dir, size=(64 * 100,
48 * 100),
paused=paused) paused=paused)
self.dimensions = 50, 50 self.dimensions = 50, 50
@ -58,41 +59,102 @@ class Level1(level.Level):
# self.imgs['block1'], # self.imgs['block1'],
# movable=True)) # movable=True))
b = boulder.Boulder(self, 64, 0, direction=(0, 1)) self.player.set_pos(64 * 10, 48 * 10)
self.objects.append(b) self.player.set_init_pos()
self.objects.append(lever.Lever(self, 64*2, 0, [b.activate]))
playfield_pos = (64*2, 48*2) ### Task 2: Rolling stone
task2_pos = (64 * 20, 48 * 20)
playfield, nsteps, directions = ( playfield, nsteps, directions = (
logic.rollingstone.generate_simple_unsolved_solvable_extra(10, 10, 7, 20)) logic.rollingstone.generate_simple_unsolved_solvable_extra(
10, 10, 7, 20))
for i in range(10): for i, j in playfield:
for j in range(10): self.objects.append(
if (i, j) in playfield: block.Block(self,
if playfield[(i, j)] is logic.rollingstone.Blocker: task2_pos[0] + 64 * i,
self.objects.append( task2_pos[1] + 48 * j,
block.Block(self, self.imgs['block1'],
playfield_pos[0] + 64 * i, movable=True))
playfield_pos[1] + 48 * j,
self.imgs['block1'],
movable=True))
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(block.ArrowBlock(self,
playfield_pos[0] - 64, task2_pos[0] - 64,
playfield_pos[1] + 48 * n, task2_pos[1] + 48 * n,
i.next_pos((0, 0)))) i.next_pos((0, 0))))
n += 1 n += 1
self.objects.extend(arrow_blocks) self.objects.extend(arrow_blocks)
self.objects.append(lever.Lever(self, 64*3, 0, self.objects.append(lever.Lever(self,
task2_pos[0] + 64 * 3,
task2_pos[1] - 48 * 2,
[arrow_block.activate [arrow_block.activate
for arrow_block in arrow_blocks], for arrow_block in arrow_blocks],
toggling=True)) 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): def load(self):
"""Load all resources used in the level.""" """Load all resources used in the level."""

View File

@ -30,31 +30,40 @@ import worldobject
class Lever(worldobject.WorldObject): class Lever(worldobject.WorldObject):
def __init__(self, level, x, y, links, def __init__(self, level, x, y, links,
anim='lever_leftright', toggling=False): anim='lever_leftright', toggling=False, signal=[0, 1],
setting=False):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y) worldobject.WorldObject.__init__(self, level, x, y)
self.init_setting = setting
self.frame = 0 self.frame = 0
self.anim_speed = 15 self.anim_speed = 15
self.setting = False
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 use(self, obj): def use(self, obj):
self.setting = not self.setting self.setting = not self.setting
for link in self.links: for link in self.links:
link(self.setting) link(self.signal[self.setting])
def update(self, e, t, dt): def update(self, e, t, dt):
# Update the animation # Update the animation
if self.setting: if not self.setting:
self.frame = min(self.frame + self.anim_speed * dt, self.frame = min(self.frame + self.anim_speed * dt,
len(self.level.imgs[self.anim]) - 1) len(self.level.imgs[self.anim]) - 1)
else: else:
self.frame = max(self.frame - self.anim_speed * dt, self.frame = max(self.frame - self.anim_speed * dt,
0) 0)
if not self.toggling: if not self.toggling:
if (self.frame + self.anim_speed * dt if (self.frame - self.anim_speed * dt <= 0):
>= len(self.level.imgs[self.anim]) - 1):
self.setting = 0 self.setting = 0
worldobject.WorldObject.update(self, e, t, dt) worldobject.WorldObject.update(self, e, t, dt)

View File

@ -34,8 +34,8 @@ class WorldObject(object):
movable=False, blocking=True, is_moving=False): movable=False, blocking=True, is_moving=False):
self.__dict__.update(locals()) self.__dict__.update(locals())
self.init_x = self.move_x = self.x = self.x - (self.x % self.tile_x) self.init_x = self.move_x = self.x = x - (x % self.tile_x)
self.init_y = self.move_y = self.y = self.y - (self.y % self.tile_y) self.init_y = self.move_y = self.y = y - (y % self.tile_y)
self.init_direction = self.direction self.init_direction = self.direction
self.holding = None self.holding = None
@ -44,6 +44,10 @@ class WorldObject(object):
if hasattr(self, 'img'): if hasattr(self, 'img'):
self.img = copy.copy(self.img) self.img = copy.copy(self.img)
def set_pos(self, x, y):
self.move_x = self.x = x - (x % self.tile_x)
self.move_y = self.y = y - (y % self.tile_y)
def set_init_pos(self): def set_init_pos(self):
self.init_x, self.init_y = self.x, self.y self.init_x, self.init_y = self.x, self.y
self.init_direction = self.direction self.init_direction = self.direction