Redid block loading mechanism. Added the lifted image to the block.

This commit is contained in:
Sakse Dalum 2012-08-10 12:50:32 +02:00
parent 1f552640e5
commit 502039e3f8
3 changed files with 39 additions and 12 deletions

View File

@ -27,33 +27,56 @@ A generic block. And other block derivatives.
import pygame import pygame
import worldobject import worldobject
import copy
class Block(worldobject.WorldObject): class Block(worldobject.WorldObject):
def __init__(self, level, x, y, img=None, movable=False, visible=True): def __init__(self, level, x, y, img_str='block1',
movable=False, visible=True):
self.__dict__.update(locals()) self.__dict__.update(locals())
worldobject.WorldObject.__init__(self, level, x, y, worldobject.WorldObject.__init__(self, level, x, y,
movable=movable, visible=visible) movable=movable, visible=visible)
self.orig_alpha = pygame.surfarray.array_alpha(self.img) self.load()
def update_alpha(self): # self.orig_alphas = {
be_opaque = not (self.y + self.z >= self.level.player.y # 'normal':
+ self.level.player.z) # pygame.surfarray.array_alpha(self.level.imgs[self.img_str]),
if be_opaque and not self.is_currently_opaque: # 'lifted':
self.is_currently_opaque = True # pygame.surfarray.array_alpha(
self.set_alpha(1) # self.level.imgs[self.img_str + '_lifted'])}
elif not be_opaque and self.is_currently_opaque:
self.is_currently_opaque = False # self.orig_alpha = self.orig_alphas['normal']
self.set_alpha(0.5)
def load(self):
if not isinstance(self.img_str, str):
self.img_str = self.level.reverse_imgs[self.img_str]
self.img = self.level.imgs[self.img_str]
# def update_alpha(self):
# be_opaque = not (self.y + self.z >= self.level.player.move_y
# + self.level.player.move_z)
# if be_opaque and not self.is_currently_opaque:
# self.is_currently_opaque = True
# self.set_alpha(1)
# elif not be_opaque and self.is_currently_opaque:
# self.is_currently_opaque = False
# self.set_alpha(0.5)
def use(self, obj): def use(self, obj):
if self.movable: if self.movable:
if obj == self.holder: if obj == self.holder:
obj.holding = None obj.holding = None
self.holder = None self.holder = None
if hasattr(self, 'img_str'):
self.img = self.level.imgs[self.img_str]
# self.orig_alpha = self.orig_alphas['normal']
else: else:
obj.holding = self obj.holding = self
self.holder = obj self.holder = obj
if hasattr(self, 'img_str'):
self.img = self.level.imgs[self.img_str + '_lifted']
# self.orig_alpha = self.orig_alphas['lifted']
def draw(self, window): def draw(self, window):
if self.visible: if self.visible:

View File

@ -39,6 +39,10 @@ class Level(object):
self.load() self.load()
self.reverse_imgs = (
dict([(val, key) for (key, val)
in self.imgs.iteritems() if not isinstance(val, list)]))
self.camera_x = 0 self.camera_x = 0
self.camera_y = 0 self.camera_y = 0

View File

@ -283,7 +283,7 @@ class Level1(level.Level):
self.imgs[o] = pygame.image.load(os.path.join( self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % o)) self.graphics_dir, 'tiles', '%s.png' % o))
l = ['block1'] l = ['block1', 'block1_lifted']
for o in l: for o in l:
self.imgs[o] = pygame.image.load(os.path.join( self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % o)) self.graphics_dir, 'blocks', '%s.png' % o))