Added translucency.

This commit is contained in:
Sakse Dalum 2012-08-08 14:52:08 +02:00
parent 82388afde3
commit f27c8eb2e7
6 changed files with 39 additions and 6 deletions

View File

@ -24,6 +24,8 @@
A generic block. A generic block.
""" """
import pygame
import worldobject import worldobject
class Block(worldobject.WorldObject): class Block(worldobject.WorldObject):
@ -31,6 +33,14 @@ class Block(worldobject.WorldObject):
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.orig_alpha = pygame.surfarray.array_alpha(self.img)
def update_alpha(self):
if (self.y + self.z >= self.level.player.y + self.level.player.z):
self.set_alpha(0.5)
else:
self.set_alpha(1)
def use(self, obj): def use(self, obj):
if obj == self.holder: if obj == self.holder:
obj.holding = None obj.holding = None

View File

@ -106,7 +106,7 @@ class Game(object):
""" """
Draw all game objects. Draw all game objects.
""" """
self.window.fill((0, 0, 0)) self.window.fill((0, 255, 255))
for obj in self.objs: for obj in self.objs:
if hasattr(obj, 'draw'): if hasattr(obj, 'draw'):
obj.draw(self.window) obj.draw(self.window)

View File

@ -49,10 +49,13 @@ 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'):
obj.update_alpha()
screen_size = self.game.window.get_size() screen_size = self.game.window.get_size()
self.camera_x = self.player.x - screen_size[0] / 2 self.camera_x = (self.player.x - screen_size[0] / 2 + 32)
self.camera_y = self.player.y - screen_size[1] / 2 self.camera_y = (self.player.y - screen_size[1] / 2 - 24)
def draw(self, window): def draw(self, window):
for tile in self.tiles: for tile in self.tiles:

View File

@ -36,12 +36,12 @@ class Player(worldobject.WorldObject):
worldobject.WorldObject.__init__(self, level, x, y, worldobject.WorldObject.__init__(self, level, x, y,
z=z, movable=movable) z=z, movable=movable)
self.load()
self.anim = 'idle_right' self.anim = 'idle_right'
self.frame = 0 self.frame = 0
self.anim_speed = 15 self.anim_speed = 15
self.load()
def load(self): def load(self):
self.imgs = {} self.imgs = {}
@ -72,6 +72,7 @@ class Player(worldobject.WorldObject):
self.imgs[anim].append(img) self.imgs[anim].append(img)
self.img = self.imgs[self.anim][int(self.frame)]
def touch(self, touch_x, touch_y): def touch(self, touch_x, touch_y):
for obj in self.level.objects: for obj in self.level.objects:
@ -115,6 +116,7 @@ class Player(worldobject.WorldObject):
def draw(self, window): def draw(self, window):
self.img = self.imgs[self.anim][int(self.frame)] self.img = self.imgs[self.anim][int(self.frame)]
self.img.set_alpha(128)
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

@ -33,5 +33,5 @@ class Tile(worldobject.WorldObject):
def draw(self, window): def draw(self, window):
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] self.y - self.img.get_size()[1] + 24
- self.level.camera_y)) - self.level.camera_y))

View File

@ -24,6 +24,10 @@
A generic world object. A generic world object.
""" """
import pygame
import numpy
import copy
class WorldObject(object): class WorldObject(object):
def __init__(self, level, x, y, z=0, direction=(1, 0), speed=5, def __init__(self, level, x, y, z=0, direction=(1, 0), speed=5,
tile_x=64, tile_y=48, tile_x=64, tile_y=48,
@ -36,6 +40,9 @@ class WorldObject(object):
self.holding = None self.holding = None
self.holder = None self.holder = None
if hasattr(self, 'img'):
self.img = copy.copy(self.img)
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
@ -69,6 +76,17 @@ class WorldObject(object):
def use(self): def use(self):
pass pass
def set_alpha(self, value):
"""
Set the relative translucency of the per-pixel-alpha image.
Value between 0 - 1, where 0 is completely transparent and 1 is
completely opaque.
"""
if hasattr(self, 'img') and hasattr(self, 'orig_alpha'):
alpha = pygame.surfarray.pixels_alpha(self.img)
alpha[:] = (self.orig_alpha * value).astype(numpy.uint8)
def update(self, e, t, dt): def update(self, e, t, dt):
if self.x > self.move_x: if self.x > self.move_x:
self.x -= min(self.speed * dt * self.tile_x, self.x -= min(self.speed * dt * self.tile_x,