class SilentAudioDriver(AbstractAudioDriver): def __init__(self): self._players = WeakSet() def create_audio_player(self, source_group, player): if source_group.audio_format: p = SilentAudioPlayerPacketConsumer(source_group, player) self._players.add(p) return p else: return SilentTimeAudioPlayer(source_group, player) def get_listener(self): raise NotImplementedError('Silent audio driver does not support positional audio') def delete(self): while len(self._players) > 0: self._players.pop().delete()
class OpenALDriver(AbstractAudioDriver): def __init__(self, device_name=None): super(OpenALDriver, self).__init__() # TODO devices must be enumerated on Windows, otherwise 1.0 context is # returned. self.device = interface.OpenALDevice(device_name) self.context = self.device.create_context() self.context.make_current() self.lock = threading.Lock() self._listener = OpenALListener(self) self._players = WeakSet() # Start worker thread self.worker = PlayerWorker() self.worker.start() def create_audio_player(self, source_group, player): assert self.device is not None, "Device was closed" if self.have_version(1, 1): player = OpenALAudioPlayer11(self, source_group, player) else: player = OpenALAudioPlayer10(self, source_group, player) self._players.add(player) return player def delete(self): self.worker.stop() for player in self._players: player.delete() with self.lock: if self.context is not None: self.context.delete() self.context = None if self.device is not None: self.device.delete() self.device = None def have_version(self, major, minor): return (major, minor) <= self.get_version() def get_version(self): assert self.device is not None, "Device was closed" return self.device.get_version() def get_extensions(self): assert self.device is not None, "Device was closed" return self.device.get_extensions() def have_extension(self, extension): return extension in self.get_extensions() def get_listener(self): return self._listener def __enter__(self): self.lock.acquire() def __exit__(self, exc_type, exc_value, traceback): self.lock.release()
class Tile(entity.Entity): MASS = 20 FRICTION = 0.5 SIZE = 32 WHITE_RATIO = 0.5 BRIGHTNESS_MIN = 0.7 BRIGHTNESS_RANGE = 1.0 - BRIGHTNESS_MIN DROP_IMPULSES = [(100, 0), (-100, 0)] DROP_OFFSET = (0, SIZE // 2) COLLISION_TYPE = 1 def __init__(self, x, y, space, *args, **kwargs): self.space = space super(Tile, self).__init__(space, x=x * Tile.SIZE, y=y * Tile.SIZE, angle=0, width=Tile.SIZE, height=Tile.SIZE, friction=Tile.FRICTION, collision_type=Tile.COLLISION_TYPE, mass=Tile.MASS, static=True, texture=resources.box) # random colour if random.random() < Tile.WHITE_RATIO: brightness = random.random( ) % Tile.BRIGHTNESS_RANGE + Tile.BRIGHTNESS_MIN self.rgb = brightness, brightness, brightness self.orig_color = self.rgb self.tints = WeakSet() def drop(self): self.static = False self.untint_all() self.pymunk_body.apply_impulse(random.choice(Tile.DROP_IMPULSES), Tile.DROP_OFFSET) def set_last_tile_for(self, bro): self.tint(bro.color) def unset_last_tile_for(self, bro): self.untint(bro.color) def tint(self, color): self.tints.add(color) self._color() def untint(self, color): if color in self.tints: self.tints.remove(color) self._color() def untint_all(self): for tint in self.tints: self.tints.remove(tint) self._color() def _color(self): # start with orig colour self.red = self.orig_color[0] self.green = self.orig_color[1] self.blue = self.orig_color[2] # overlay each tint for tint in self.tints: t = tint.fade(Tile.BRIGHTNESS_RANGE).invert() self.red -= t.red self.green -= t.green self.blue -= t.blue
class Tile(entity.Entity): MASS = 20 FRICTION = 0.5 SIZE = 32 WHITE_RATIO = 0.5 BRIGHTNESS_MIN = 0.7 BRIGHTNESS_RANGE = 1.0 - BRIGHTNESS_MIN DROP_IMPULSES = [(100, 0), (-100, 0)] DROP_OFFSET = (0, SIZE // 2) COLLISION_TYPE = 1 def __init__(self, x, y, space, *args, **kwargs): self.space = space super(Tile, self).__init__( space, x=x * Tile.SIZE, y=y * Tile.SIZE, angle=0, width=Tile.SIZE, height=Tile.SIZE, friction=Tile.FRICTION, collision_type=Tile.COLLISION_TYPE, mass=Tile.MASS, static=True, texture=resources.box, ) # random colour if random.random() < Tile.WHITE_RATIO: brightness = random.random() % Tile.BRIGHTNESS_RANGE + Tile.BRIGHTNESS_MIN self.rgb = brightness, brightness, brightness self.orig_color = self.rgb self.tints = WeakSet() def drop(self): self.static = False self.untint_all() self.pymunk_body.apply_impulse(random.choice(Tile.DROP_IMPULSES), Tile.DROP_OFFSET) def set_last_tile_for(self, bro): self.tint(bro.color) def unset_last_tile_for(self, bro): self.untint(bro.color) def tint(self, color): self.tints.add(color) self._color() def untint(self, color): if color in self.tints: self.tints.remove(color) self._color() def untint_all(self): for tint in self.tints: self.tints.remove(tint) self._color() def _color(self): # start with orig colour self.red = self.orig_color[0] self.green = self.orig_color[1] self.blue = self.orig_color[2] # overlay each tint for tint in self.tints: t = tint.fade(Tile.BRIGHTNESS_RANGE).invert() self.red -= t.red self.green -= t.green self.blue -= t.blue