Added translucency.
This commit is contained in:
		
							parent
							
								
									82388afde3
								
							
						
					
					
						commit
						f27c8eb2e7
					
				| @ -24,6 +24,8 @@ | ||||
| A generic block. | ||||
| """ | ||||
| 
 | ||||
| import pygame | ||||
| 
 | ||||
| import worldobject | ||||
| 
 | ||||
| class Block(worldobject.WorldObject): | ||||
| @ -31,6 +33,14 @@ class Block(worldobject.WorldObject): | ||||
|         self.__dict__.update(locals()) | ||||
|         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): | ||||
|         if obj == self.holder: | ||||
|             obj.holding = None | ||||
|  | ||||
| @ -106,7 +106,7 @@ class Game(object): | ||||
|         """ | ||||
|         Draw all game objects. | ||||
|         """ | ||||
|         self.window.fill((0, 0, 0)) | ||||
|         self.window.fill((0, 255, 255)) | ||||
|         for obj in self.objs: | ||||
|             if hasattr(obj, 'draw'): | ||||
|                 obj.draw(self.window) | ||||
|  | ||||
| @ -49,10 +49,13 @@ class Level(object): | ||||
|         if not self.paused: | ||||
|             for obj in self.objects: | ||||
|                 obj.update(e, t, dt) | ||||
|                 if hasattr(obj, 'update_alpha'): | ||||
|                     obj.update_alpha() | ||||
| 
 | ||||
| 
 | ||||
|         screen_size = self.game.window.get_size() | ||||
|         self.camera_x = self.player.x - screen_size[0] / 2 | ||||
|         self.camera_y = self.player.y - screen_size[1] / 2 | ||||
|         self.camera_x = (self.player.x - screen_size[0] / 2 + 32) | ||||
|         self.camera_y = (self.player.y - screen_size[1] / 2 - 24) | ||||
| 
 | ||||
|     def draw(self, window): | ||||
|         for tile in self.tiles: | ||||
|  | ||||
| @ -36,12 +36,12 @@ class Player(worldobject.WorldObject): | ||||
|         worldobject.WorldObject.__init__(self, level, x, y, | ||||
|                                          z=z, movable=movable) | ||||
| 
 | ||||
|         self.load() | ||||
| 
 | ||||
|         self.anim = 'idle_right' | ||||
|         self.frame = 0 | ||||
|         self.anim_speed = 15 | ||||
| 
 | ||||
|         self.load() | ||||
| 
 | ||||
|     def load(self): | ||||
|         self.imgs = {} | ||||
| 
 | ||||
| @ -72,6 +72,7 @@ class Player(worldobject.WorldObject): | ||||
| 
 | ||||
|                 self.imgs[anim].append(img) | ||||
| 
 | ||||
|         self.img = self.imgs[self.anim][int(self.frame)] | ||||
| 
 | ||||
|     def touch(self, touch_x, touch_y): | ||||
|         for obj in self.level.objects: | ||||
| @ -115,6 +116,7 @@ class Player(worldobject.WorldObject): | ||||
| 
 | ||||
|     def draw(self, window): | ||||
|         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, | ||||
|                                self.y - self.img.get_size()[1] + 24 | ||||
|                                - self.level.camera_y)) | ||||
|  | ||||
| @ -33,5 +33,5 @@ class Tile(worldobject.WorldObject): | ||||
| 
 | ||||
|     def draw(self, window): | ||||
|         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)) | ||||
|  | ||||
| @ -24,6 +24,10 @@ | ||||
| A generic world object. | ||||
| """ | ||||
| 
 | ||||
| import pygame | ||||
| import numpy | ||||
| import copy | ||||
| 
 | ||||
| class WorldObject(object): | ||||
|     def __init__(self, level, x, y, z=0, direction=(1, 0), speed=5, | ||||
|                  tile_x=64, tile_y=48, | ||||
| @ -36,6 +40,9 @@ class WorldObject(object): | ||||
|         self.holding = None | ||||
|         self.holder = None | ||||
| 
 | ||||
|         if hasattr(self, 'img'): | ||||
|             self.img = copy.copy(self.img) | ||||
| 
 | ||||
|     def set_init_pos(self): | ||||
|         self.init_x, self.init_y = self.x, self.y | ||||
| 
 | ||||
| @ -69,6 +76,17 @@ class WorldObject(object): | ||||
|     def use(self): | ||||
|         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): | ||||
|         if self.x > self.move_x: | ||||
|             self.x -= min(self.speed * dt * self.tile_x, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Sakse Dalum
						Sakse Dalum