class LoadingSprite(ppb.Sprite): ready_image = ppb.Image("resources/load_bar/center_filled.png") waiting_image = ppb.Image("resources/load_bar/center_empty.png") def __init__(self, **kwargs): super().__init__(**kwargs) self.image = self.waiting_image
class Bar: color: Tuple[int] position: ppb.Vector value: int = 10 max: int = 10 size: int = 0 bg: ppb.Sprite = None segments: Tuple[ppb.Sprite] = () BAR_BG = ppb.Image("resources/BAR_BG.png") BAR_SEGMENT = ppb.Image("resources/BAR_SEGMENT.png") def __hash__(self): return hash(id(self)) def __init__(self, scene, **kwargs): super().__init__() self.__dict__.update(kwargs) self.bg = ppb.Sprite( position=self.position, image=self.BAR_BG, size=1 / 4, layer=50, ) scene.add(self.bg) segments = [] for i in range(16): segment = ppb.Sprite( position=self.position + V(i / 4 - 2, 0), image=self.BAR_SEGMENT, color=self.color, size=1 / 4, layer=51, ) segments.append(segment) scene.add(segment) self.segments = tuple(segments) self.set_value(10) def set_max(self, new_max): self.max = new_max self.set_value(self.value) def set_value(self, value): self.value = value if value == 0: p = 0 else: p = int(value / self.max * 16) for i, segment in enumerate(self.segments): if i >= p: segment.size = 0 else: segment.size = 1 / 4
def get_progress_sprites(self): left = LoadingSprite( position=ppb.Vector(-4, 0), ready_image=ppb.Image("resources/load_bar/left_filled.png"), waiting_image=ppb.Image("resources/load_bar/left_empty.png")) center = [ LoadingSprite(position=ppb.Vector(x, 0)) for x in range(-3, 4) ] right = LoadingSprite( position=ppb.Vector(4, 0), ready_image=ppb.Image("resources/load_bar/right_filled.png"), waiting_image=ppb.Image("resources/load_bar/right_empty.png")) return [left, *center, right]
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.wall_images = [ ppb.Image(f"resources/terrain/wall_{x}.png") for x in range(1, 5) ] self.floor_images = [ ppb.Image(f"resources/terrain/floor_{x}.png") for x in range(1, 5) ] self.blob_animation = animation.Animation( "resources/blob/blob_{0..6}.png", 12) self.squares = [ ppb.Image(f"resources/squares/sprite_{str(x).zfill(2)}.png") for x in range(60) ]
def __image__(self): if self.image is None: klass = type(self) prefix = Path(klass.__module__.replace('.', '/')) try: klassfile = getfile(klass) except TypeError: prefix = Path('.') else: if Path(klassfile).name != '__init__.py': prefix = prefix.parent if prefix == Path('.'): self.image = ppb.Image(f"{klass.__name__.lower()}.png") else: self.image = ppb.Image(f"{prefix!s}/{klass.__name__.lower()}.png") return self.image
def create_mushroom(self, cls, position, signal): # TODO: create different kinds mushroom = cls(position=position, layer=LAYER_GAMEPLAY_LOW) self.scene.add(mushroom, tags=['mushroom']) # TODO: Create meters through events CtrlMeter.create(self.scene, FRAMES_HEALTH, target=mushroom, attr='health', track=mushroom, color=COLOR['RED']) CtrlMeter.create(self.scene, FRAMES_HEALTH, target=mushroom, attr='toxins', track=mushroom, color=COLOR['DARKGREEN'], flip=SDL_FLIP_VERTICAL) signal(MeterUpdate(mushroom, 'health', 1.0)) mushroom.root = ppb.Sprite( image=ppb.Image("resources/root_1.png"), position=position - ppb.Vector(0, 0.5), size=6, layer=LAYER_GROUND_HIGHLIGHT, opacity=0, ) self.scene.add(mushroom.root) signal(MushroomPlaced(position, mushroom))
def on_pre_render(self, ev, signal): layer = pos_to_layer(self.position) # Initialize child sprites on the first frame if not self.parts: self.parts = {} clothes_i = min(len(VIKING_CLOTHES), max(1, self.strength // 2)) - 1 hat_i = min(len(VIKING_HAT), max(1, self.strength - clothes_i)) - 1 self.parts['base'] = create_sprite( image=VIKING_WALK, anchor=self, size=2, ) self.parts['clothes'] = create_sprite( image=VIKING_CLOTHES[clothes_i], anchor=self, layer=0.1, size=2, ) if self.shield: self.parts['hair'] = create_sprite( image=choice(VIKING_HAIR), anchor=self, layer=0.2, size=2, ) else: self.parts['hat'] = create_sprite( image=VIKING_HAT[hat_i], anchor=self, layer=0.2, size=2, ) self.parts['corpse'] = create_sprite( image=ppb.Image("resources/viking/dead_fg.png"), anchor=self, layer=0.1, size=2, opacity=0, ) if self.shield: self.parts['shield'] = create_sprite( image=IMAGE_SHIELD, anchor=self, position=ppb.Vector(0.5, 0.0), size=2, layer=0.9, ) self.state.on_pre_render(self, ev, signal) self.layer = layer
class Mover(ppb.BaseSprite): image = ppb.Image("resources/mover.png") position = ppb.Vector(0, -4) velocity = ppb.Vector(0, 3) def on_update(self, update: ppb.events.Update, signal): self.position += self.velocity * update.time_delta if self.position.y > 4 or self.position.y < -4: self.velocity *= -1
def enter_state(self, scene, signal): self.parts['base'].image = ppb.Image("resources/viking/dead_bg.png") for s in self.sprites: tweening.tween(s, 'rotation', 90, 0.5) tweening.tween(s, 'opacity', 0, 5.0) tweening.tween(self, 'position', self.position + ppb.Vector(-0.5, -0.25), 0.5) self.parts['corpse'].opacity = 255 signal(VikingDeath(self))
def on_scene_started(self, ev, signal): logo = ppb.Sprite( image=ppb.Image("resources/worldmap.png"), size=10, layer=1, ) ev.scene.add(logo) self.pins = [] for c in PIN_COORDS: pin = ppb.Sprite( image=ppb.Image("resources/worldmap_pin.png"), size=2, position=ppb.Vector(*c), tint=COLOR['RED'], layer=2, ) ev.scene.add(pin) self.pins.append(pin)
def __image__(self): """ Returns the sprite's image attribute if provided, or sets a default one. """ if self.image is ...: klass = type(self) prefix = Path(klass.__module__.replace('.', '/')) try: klassfile = getfile(klass) except TypeError: prefix = Path('.') else: if Path(klassfile).name != '__init__.py': prefix = prefix.parent if prefix == Path('.'): self.image = ppb.Image(f"{klass.__name__.lower()}.png") else: self.image = ppb.Image(f"{prefix!s}/{klass.__name__.lower()}.png") return self.image
class Particle(ppb.sprites.Sprite): image = ppb.Image("resources/sparkle1.png") opacity = 128 opacity_mode = 'add' tint = COLOR['WHITE'] size = 2 rotation = 0 def __init__(self, *args, **kwargs): self.tint = kwargs.pop('color', COLOR['WHITE']) super().__init__(*args, **kwargs)
class Particle(ppb.sprites.Sprite): base_image = ppb.Image("resources/sparkle1.png") opacity = 128 opacity_mode = 'add' color = COLOR_WHITE size = 2 rotation = 0 def __init__(self, *args, **kwargs): self.color = kwargs.pop('color', COLOR_WHITE) super().__init__(*args, **kwargs) self.image = EffectedImage(self.base_image, opacity=128)
def _compile_filename(self): match = FILE_PATTERN.search(self._filename) start, end = match.groups() numdigits = min(len(start), len(end)) start = int(start) end = int(end) template = FILE_PATTERN.sub( '{:0%dd}' % numdigits, self._filename, ) self._frames = [ ppb.Image(template.format(n)) for n in range(start, end + 1) ]
class LoadingScene(ProgressBarLoadingScene): loaded_image = ppb.Image('target.png') next_scene = Quitter def on_asset_loaded(self, event, signal): print(event) assert event.total_queued >= 0 assert event.total_loaded >= 0 super().on_asset_loaded(event, signal) def get_progress_sprites(self): for x in range(-2, 3): yield ppb.BaseSprite(pos=ppb.Vector(x, 0))
def create(self, sprite_class=ppb.Sprite, anchor=None, **kwargs): kwargs.setdefault('layer', 0.0) kwargs.setdefault('position', ppb.Vector(0, 0)) kwargs.setdefault('opacity', 255) tags = kwargs.pop('tags', []) if 'image' in kwargs: if isinstance(kwargs['image'], str): kwargs['image'] = ppb.Image(kwargs['image']) sprite = sprite_class(**kwargs) if anchor: sprite.anchor = anchor sprite.local_position = kwargs['position'] sprite.local_layer = kwargs['layer'] tags.append('anchored') self.current_scene.add(sprite, tags=tags) self.signal(SpriteAdded(sprite)) return sprite
class UIButton(ppb.Sprite): image = ppb.Image("resources/uibutton.png") size = 2.0 label: str enabled: bool = True def __init__(self, *args, **kwargs): self.label = kwargs.pop('label') super().__init__(*args, **kwargs) if not self.enabled: self.enabled = 128 def on_disable_button(self, ev, signal): if ev.label == self.label: self.enabled = False self.opacity = 128 def on_enable_button(self, ev, signal): if ev.label == self.label: self.enabled = True self.opacity = 255
def setup(scene): for x in range(-2, 3): for y in range(-2, 3): seed_class = choice(SEEDS) seed = seed_class(position=V(x, y)) scene.add(seed, tags=['seed']) GRID[x, y] = seed player = Player(position=POS_PLAYER) scene.add(player, tags=['player', 'character']) snake = Monster(position=POS_ENEMY) scene.add(snake, tags=['enemy', 'character']) scene.add(Grid(), tags=['grid', 'manager']) scene.add(ppb.Sprite( image=ppb.Image("resources/BACKGROUND.png"), size=12, layer=-1, ), tags=['bg'])
class Cloud(ppb.sprites.Sprite): heading: ppb.Vector image = ppb.Image("resources/cloud.png") speed: float = 5.0 lifetime: float = 1.0 size: float = 2.0 last_hit_time: float = 0.0 opacity: int = 255 def __init__(self, *args, **kwargs): self.cloud_id = kwargs.get('cloud_id') super().__init__(*args, **kwargs) def on_update(self, ev, signal): self.lifetime -= ev.time_delta * 0.5 t = 1.0 - self.lifetime if self.lifetime <= 0.0: ev.scene.remove(self) def on_pre_render(self, ev, signal): self.layer = pos_to_layer(self.position) assert self.layer > 0, self.layer
class Player(ppb.sprites.Sprite): image = ppb.Image("resources/ANGELA.png") size = 4.0 @property def hp(self): return self._hp @hp.setter def hp(self, value): value = min(10, value) self._hp = value self.hp_text.text = str(value) self.hp_bar.set_value(value) @property def shield(self): return self._shield @shield.setter def shield(self, value): value = min(10, value) self._shield = value self.shield_bar.set_value(value) def on_start_game(self, ev, signal): self.hp = 10 def on_scene_started(self, ev, signal): self.hp_text = Text('', self.position + V(0, -3)) self.hp_text.scene = ev.scene self.hp_text.setup() ev.scene.add(self.hp_text) self.hp_bar = Bar( color=COLOR_DARKRED, scene=ev.scene, position=V(self.position + V(0, -3.5)), ) ev.scene.add(self.hp_bar) self.shield_bar = Bar( color=COLOR_YELLOW, scene=ev.scene, position=V(self.position + V(0, -4.0)), value=0, ) ev.scene.add(self.shield_bar) self.hp = 10 self.shield = 0 self.sparkler = Sparkler(self.position) def on_player_death(self, ev, signal): pass def on_damage_dealt(self, ev, signal): if ev.target == 'player': self.shield -= ev.dmg if self.shield < 0: self.hp += self.shield self.shield = 0 if self.hp < 0: self.hp = 0 if self.hp <= 0: signal(PlayerDeath(self)) else: tween(self, 'position', self.position - V(1, 0), 0.1, easing='in_quad') tween(self, 'position', self.position, 0.2, delay=0.1, easing='out_quad') signal(PlaySound(choice(SOUND_HURT_SET)))
def kill(self, event, knight): knight.dead = True knight.image = ppb.Image('splat.png') self.target = self.pick_target(event)
class PoddacimPod(ppb.Sprite): image = ppb.Image("resources/mushroom/pod.png") layer = 1000
import ppb from ppb.events import ButtonPressed, ButtonReleased, Update from ppb.systemslib import System import ppb_tween as tweening import constants as C from events import ScorePoints from controllers.meters import MeterUpdate, MeterRemove from systems.cloud import MushroomAttack from .base import Mushroom from ppb_timing import delay MUSHROOM_SPRITES = [ ppb.Image("resources/mushroom/poddacim_0.png"), ppb.Image("resources/mushroom/poddacim_1.png"), ppb.Image("resources/mushroom/poddacim_2.png"), ppb.Image("resources/mushroom/poddacim_3.png"), ] class PoddacimPod(ppb.Sprite): image = ppb.Image("resources/mushroom/pod.png") layer = 1000 class Poddacim(Mushroom): smoosh_sprites = MUSHROOM_SPRITES image: ppb.Image = smoosh_sprites[0]
class TravelUnder(ppb.BaseSprite): image = ppb.Image("resources/travel_under.png") layer = 1
class TravelOver(ppb.BaseSprite): image = ppb.Image("resources/travel_over.png") layer = -1
class OrbitSprite(ppb.BaseSprite): position = ppb.Vector(0, -2) image = ppb.Image('target.png') def on_update(self, event, signal): self.position = self.position.rotate(ROTATION_RATE * event.time_delta)
class CenterSprite(ppb.BaseSprite): image = ppb.Image('player.png') def on_update(self, event, signal): self.rotation += ROTATION_RATE * event.time_delta
def on_scene_started(self, ev, signal): logo = ppb.Sprite( image=ppb.Image("resources/logo.png"), size=10, ) ev.scene.add(logo)
def __init__(self, **props): super().__init__(**props) self.add(ppb.Sprite( image=ppb.Image('hellopursuedpybear/resources/hellopursuedpybear.png'), ))
rect=rect, layer=LAYER_BACKGROUND + 1, size=2.0, mapitemtype=mapitemtype, ) t.tag = "decor" return t return _ SOLID = 1 DECOR = 2 GROUND = 3 GROUND_IMAGES = [ ppb.Image(f"resources/ground/ground_{r}.png") for r in range(6) ] TREE_H = ppb.Image("resources/ground/tree_horizontal.png") TREE_V = ppb.Image("resources/ground/tree_vertical.png") ROCKS = ppb.Image("resources/ground/rocks.png") BRIDGE = ppb.Image("resources/ground/bridge.png") RIVER = ppb.Image("resources/ground/river.png") EXIT = ppb.Image("resources/ground/exit.png") ITEMS = { 't1': make_mapitem(TREE_V, (0, 0, 32, 32), SOLID), 't2': make_mapitem(TREE_V, (0, 32, 32, 32), SOLID), 't3': make_mapitem(TREE_V, (0, 64, 32, 32), SOLID), 't4': make_mapitem(TREE_H, (0, 0, 32, 32), SOLID), 't5': make_mapitem(TREE_H, (32, 0, 32, 32), SOLID),