It's awesome!

This commit is contained in:
Sakse Dalum 2012-08-08 18:13:53 +02:00
parent 0ac3baa8f5
commit ce84def0e0
4 changed files with 80 additions and 26 deletions

View File

@ -42,12 +42,13 @@ class Block(worldobject.WorldObject):
self.set_alpha(1)
def use(self, obj):
if obj == self.holder:
obj.holding = None
self.holder = None
else:
obj.holding = self
self.holder = obj
if self.movable:
if obj == self.holder:
obj.holding = None
self.holder = None
else:
obj.holding = self
self.holder = obj
def draw(self, window):
window.blit(self.img, (self.x - 32 - self.level.camera_x,
@ -55,17 +56,27 @@ class Block(worldobject.WorldObject):
- self.level.camera_y))
class ArrowBlock(Block):
def __init__(self, level, x, y, anim='arrow_up', movable=True, is_up=True):
def __init__(self, level, x, y, direction, movable=True, is_up=True):
self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, movable=movable)
worldobject.WorldObject.__init__(self, level, x, y,
direction=direction, movable=movable)
self.frame = 0
self.anim_speed = 15
self.setting = False
if direction == (1, 0):
self.anim = 'arrow_right'
if direction == (-1, 0):
self.anim = 'arrow_left'
if direction == (0, -1):
self.anim = 'arrow_up'
if direction == (0, 1):
self.anim = 'arrow_down'
def activate(self, setting):
self.movable = True
if self.check_move(0, 0):
if not self.share_tile(worldobject.WorldObject):
self.is_up = not setting
self.movable = False

View File

@ -27,21 +27,30 @@ A rolling boulder.
import pygame
import worldobject
import block
class Boulder(worldobject.WorldObject):
def __init__(self, level, x, y, movable=True, rolling=True):
def __init__(self, level, x, y, movable=True, rolling=False):
self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y,
movable=movable, speed=1)
movable=movable, speed=4)
self.anim = 'boulder_right'
self.frame = 0
self.anim_speed = 30
def activate(self, setting):
self.rolling = True
def reset_pos(self):
worldobject.WorldObject.reset_pos(self)
self.rolling = False
def update(self, e, t, dt):
# Update the animation
self.frame = ((self.frame + self.anim_speed * dt) %
len(self.level.imgs[self.anim]))
if self.rolling:
self.frame = ((self.frame + self.anim_speed * dt) %
len(self.level.imgs[self.anim]))
if self.direction == (1, 0):
self.anim = 'boulder_right'
@ -52,7 +61,13 @@ class Boulder(worldobject.WorldObject):
if self.direction == (0, -1):
self.anim = 'boulder_up'
self.move(*self.direction)
tile_sharer = self.share_tile(block.ArrowBlock)
if tile_sharer:
self.direction = tile_sharer.direction
if self.rolling:
if not self.move(*self.direction) and not self.is_moving:
self.reset_pos()
worldobject.WorldObject.update(self, e, t, dt)

View File

@ -53,16 +53,22 @@ class Level1(level.Level):
# self.imgs['block1'],
# movable=True))
self.objects.append(boulder.Boulder(self, 64, 48))
arrow_block = block.ArrowBlock(self, 64, 0)
lever1 = lever.Lever(self, 128, 48*2, [arrow_block.activate],
toggling=True)
self.objects.extend([lever1, arrow_block])
self.objects.append(lever.Lever(self, 128, 48*3, [], toggling=False))
self.objects.append(lever.Lever(self, 128*2, 48*3, [], toggling=False,
anim='lever_updown'))
self.objects.append(lever.Lever(self, 128*2, 48*5, [], toggling=True,
anim='lever_updown'))
b = boulder.Boulder(self, 64, 48)
self.objects.append(b)
self.objects.append(lever.Lever(self, 64*5, 48*7, [b.activate]))
arrow_blocks = [block.ArrowBlock(self, 64*4, 48, (1, 0)),
block.ArrowBlock(self, 64*4, 48*2, (-1, 0)),
block.ArrowBlock(self, 64*5, 48*2, (0, 1)),
block.ArrowBlock(self, 64*5, 48, (0, -1))]
self.objects.extend(arrow_blocks)
self.objects.append(lever.Lever(self,
128,
48*2,
[arrow_block.activate
for arrow_block in arrow_blocks],
toggling=True))
def load(self):
"""Load all resources used in the level."""
@ -107,7 +113,12 @@ class Level1(level.Level):
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):
self.player.reset_pos()
for obj in self.objects:
obj.reset_pos()

View File

@ -31,11 +31,12 @@ import copy
class WorldObject(object):
def __init__(self, level, x, y, z=0, direction=(1, 0), speed=4,
tile_x=64, tile_y=48,
movable=False, blocking=True):
movable=False, blocking=True, is_moving=False):
self.__dict__.update(locals())
self.init_x = self.move_x = self.x = self.x - (self.x % self.tile_x)
self.init_y = self.move_y = self.y = self.y - (self.y % self.tile_y)
self.init_direction = self.direction
self.holding = None
self.holder = None
@ -45,9 +46,20 @@ class WorldObject(object):
def set_init_pos(self):
self.init_x, self.init_y = self.x, self.y
self.init_direction = self.direction
def reset_pos(self):
self.x, self.y = self.move_x, self.move_y = self.init_x, self.init_y
self.direction = self.init_direction
def share_tile(self, obj_type):
for obj in self.level.objects:
if (obj.x - (obj.x % self.tile_x) == self.x - (self.x % self.tile_x)
and obj.y - (obj.y % self.tile_y) == self.y - (self.y
% self.tile_y)
and obj is not self and isinstance(obj, obj_type)):
return obj
return None
def check_move(self, move_x, move_y):
if self.move_x == self.x and self.move_y == self.y and self.movable:
@ -73,6 +85,9 @@ class WorldObject(object):
if self.holding:
self.holding.move(move_x, move_y)
return True
return False
def use(self, obj):
pass
@ -104,4 +119,6 @@ class WorldObject(object):
self.y += min(self.speed * dt * self.tile_y,
abs(self.y - self.move_y))
self.is_moving = self.x != self.move_x or self.y != self.move_y
self.x, self.y = int(self.x), int(self.y)