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']) ppb.run( setup=setup, basic_systems=(CustomRenderer, Updater, EventPoller, SoundController, AssetLoadingSystem), systems=[ TickSystem, MenuSystem, MonsterManager, TweenSystem, Timers, ParticleSystem, ScoreBoard, ], resolution=(1280, 720), window_title='✨Seed Magic✨', target_frame_rate=60, )
import ppb ppb.run()
def run(): ppb.run(starting_scene=Sandbox, systems=[Controller, Collider], title="Survival: Prototype")
import argparse import ppb import butterfly_effect as be parser = argparse.ArgumentParser( description="Dev tooling to choose scene to start.") parser.add_argument("start_scene") args = parser.parse_args() run_first = be.scene_map.get(args.start_scene, be.Splash) ppb.run(starting_scene=run_first)
self.add( ppb.RectangleSprite( image=ppb.Text(" ".join((self.cursor[cursor_state], self._continue)), font=font, color=(255, 255, 255)))) def on_button_pressed(self, event: ppb.events.ButtonPressed, signal): signal(self.click_event) class NoCursorScene(RootScene): background_color = (100, 100, 100) show_cursor = False class DefaultScene(RootScene): click_event = ppb.events.ReplaceScene(NoCursorScene) class ExplicitVisibleCursor(RootScene): background_color = (0, 0, 0) show_cursor = True click_event = ppb.events.StartScene(DefaultScene) def on_scene_continued(self, _, __): self.click_event = ppb.events.StopScene() ppb.run(None, starting_scene=ExplicitVisibleCursor)
def main(): ppb.run( starting_scene=hellopursuedpybear, title='hellopursuedpybear', )
class Projectile(ppb.BaseSprite): velocity = ppb.Vector(0, 8) size = 0.25 def on_update(self, event, signal): self.position += self.velocity * event.time_delta for target in event.scene.get(kind=Target): distance = (target.position - self.position).length collision_distance = (target.size + self.size) / 2 if distance <= collision_distance: event.scene.remove(self) event.scene.remove(target) break if self.bottom > 6: event.scene.remove(self) class Target(ppb.BaseSprite): pass def setup(scene): scene.add(Player()) for x in range(-4, 5, 2): scene.add(Target(position=ppb.Vector(x, 4))) ppb.run(setup, starting_scene=Game)
class LoadScreen(loadingscene.BaseLoadingScene): next_scene = Game 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 update_progress(self, progress): bar = sorted(self.get(tag='progress'), key=lambda s: s.position.x) progress_index = progress * len(bar) for i, sprite in enumerate(bar): if i <= progress_index: sprite.image = sprite.ready_image else: sprite.image = sprite.waiting_image ppb.run(starting_scene=LoadScreen)
import ppb from pathunstrom_splash import Splash ppb.run(starting_scene=Splash(next_scene=ppb.BaseScene))
repeat(1.0, self.report) def on_pre_render(self, ev, signal): if self.enable_fps: cur_frame = perf_counter() f = (1.0 / (cur_frame - self.last_frame)) self.last_frame = cur_frame self.frames.append(f) self.fps = sum(self.frames) / 10.0 del self.frames[0] ppb.run( starting_scene=TitleScene, basic_systems=(CustomRenderer, Updater(0.1), EventPoller, SoundController, AssetLoadingSystem), systems=[ Tweening, Timers, ParticleSystem, CloudSystem, ui.UISystem, FloatingNumberSystem, ProgressSystem, SpriteManager, ] + ([DiagnosticSystem] if DIAGNOSE else []), resolution=(1280, 720), window_title='🍄Last Stand of the Mushrooms🍄', target_frame_rate=60, )
else: for target in scene.get(tag='target'): d = (target.position - self.position).length if d <= target.radius: scene.remove(self) scene.remove(target) break class Target(ppb.BaseSprite): radius = 0.5 class GameScene(ppb.BaseScene): def __init__(self, *p, **kw): super().__init__(*p, **kw) # Set up sprites self.add(Player(pos=Vector(0, 0)), tags=['player']) # 5 targets in x = -3.75 -> 3.75, with margin for x in (-3, -1.5, 0, 1.5, 3): self.add(Target(pos=Vector(x, 1.875)), tags=['target']) if __name__ == "__main__": ppb.run( starting_scene=GameScene, log_level=logging.DEBUG, )
duration = 1.0 def __init__(self, *p, **kw): super().__init__(*p, **kw) self.frames = 0 self.start_time = time.monotonic() def on_update(self, event, signal): """ Fires at the update rate (~60 times a second) """ t = time.monotonic() - self.start_time if t >= self.duration: signal(ppb.events.Quit()) def on_pre_render(self, event, signal): """ Fires each frame. The frame rate is variable and different from the update rate. """ t = time.monotonic() - self.start_time self.frames += 1 print(f"Frame {self.frames} rendered at {t}") if __name__ == "__main__": ppb.run(TestScene, log_level=logging.INFO)
self.add(Neko(character=self.character, pose='up', position=(0, cam.frame_top / 2))) self.add(Neko(character=self.character, pose='down', position=(0, cam.frame_bottom / 2))) self.add(Neko(character=self.character, pose='left', position=(cam.frame_left / 2, 0))) self.add(Neko(character=self.character, pose='right', position=(cam.frame_right / 2, 0))) self.add(Neko(character=self.character, pose='upright', position=(cam.frame_right / 2, cam.frame_top / 2))) self.add(Neko(character=self.character, pose='upleft', position=(cam.frame_left / 2, cam.frame_top / 2))) self.add(Neko(character=self.character, pose='downright', position=(cam.frame_right / 2, cam.frame_bottom / 2))) self.add(Neko(character=self.character, pose='downleft', position=(cam.frame_left / 2, cam.frame_bottom / 2))) self.add(Neko(character=self.character, pose='up_wall', position=Vector(0, cam.frame_top - 0.5))) self.add(Neko(character=self.character, pose='down_wall', position=Vector(0, cam.frame_bottom + 0.5))) self.add(Neko(character=self.character, pose='left_wall', position=Vector(cam.frame_left + 0.5, 0))) self.add(Neko(character=self.character, pose='right_wall', position=Vector(cam.frame_right - 0.5, 0))) def on_key_pressed(self, event, signal): if event.key is kc.B: signal(ReplaceScene(NekoScene(character='bsd'))) elif event.key is kc.D: signal(ReplaceScene(NekoScene(character='dog'))) elif event.key is kc.N and event.mods == {kc.ShiftLeft}: signal(ReplaceScene(NekoScene(character='tora'))) elif event.key is kc.N: signal(ReplaceScene(NekoScene(character='neko'))) elif event.key is kc.S: signal(ReplaceScene(NekoScene(character='sakura'))) elif event.key is kc.T: signal(ReplaceScene(NekoScene(character='tomoyo'))) ppb.run(starting_scene=NekoScene)
self.next = None return self.running, n class CustomizeScene(SelectScene): class Sprite(SelectScene.Sprite): resource_path = pathlib.Path('.') class BackSprite(Region, Sprite): emoji = 'tick' def on_button_pressed(self, mouse, signal): if self.contains( mouse.position) and mouse.button is ppb.buttons.Primary: mouse.scene.running = False def __init__(self, *p, **kw): super().__init__(*p, **kw) left = self.main_camera.frame_left self.add(self.BackSprite(pos=(left + 2.5, -1.5))) def do_update_morphtone(self): EmojiSprite.morph = self.morph EmojiSprite.tone = self.tone if __name__ == '__main__': ppb.run(starting_scene=IndexScene, # window_title='Mutant Standard Index', )
d = (target.position - self.position).length if d <= target.radius: scene.remove(self) scene.remove(target) break class Target(ppb.BaseSprite): radius = 0.5 class GameScene(ppb.BaseScene): def __init__(self, *p, **kw): super().__init__(*p, **kw) # Set up sprites self.add(Player(pos=Vector(0, 0)), tags=['player']) # 5 targets in x = -3.75 -> 3.75, with margin for x in (-3, -1.5, 0, 1.5, 3): self.add(Target(pos=Vector(x, 1.875)), tags=['target']) if __name__ == "__main__": ppb.run( GameScene, logging_level=logging.DEBUG, window_title="Targets", resolution=(600, 400), )
[11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11], [11, 1, 1, 11, 11, 1, 1, 1, 1, 1, 11], [11, 1, 1, 11, 11, 1, 1, 1, 1, 1, 11], [11, 1, 1, 11, 11, 1, 1, 1, 1, 1, 11], [11, 1, 1, 11, 11, 1, 1, 1, 1, 1, 11], [11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11], [11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11], [11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11], ] # Create sprites for tiles: y = 0 center_y = len(matrix) / 2.0 for row in matrix: center_x = len(row) / 2.0 x = 0 for column in row: tile = tiles[column] # get an instance of Tile or Wall target_vector = ppb.Vector(x - center_x, y + center_y) new_tile = tile.pos(tile=tile, position=target_vector) # set position scene.add(new_tile) # place it! print(f"Placing {new_tile.image.name} at {target_vector}") x += 1 y -= 1 ppb.run(setup=setup, resolution=(1024, 768))
#!/usr/bin/env python3 """ Show the morph/tone picker. """ import ppb from ppb_mutant import SelectScene class SelectDemoScene(SelectScene): def do_update_morphtone(self): print(self.morph, self.tone) if __name__ == '__main__': ppb.run(starting_scene=SelectDemoScene, # window_title='Mutant Morph/Tone Picker', )
import ppb def hsv2rgb(h, s, v): return list(map(int, colorsys.hsv_to_rgb(h, s, v))) class TextScene(ppb.BaseScene): elapsed = 0 def on_scene_started(self, event, signal): for i, font in enumerate( ('B', 'BI', 'C', 'L', 'LI', 'M', 'MI', 'R', 'RI', 'Th')): self.add( ppb.Sprite( image=ppb.Text( "Hello, PPB!", font=ppb.Font( f"resources/ubuntu_font/Ubuntu-{font}.ttf", size=72), color=hsv2rgb(i / 10, 1.0, 75)), position=(0, i - 4.5), )) def on_update(self, event, signal): self.elapsed += event.time_delta self.background_color = hsv2rgb(self.elapsed / 10, 1.0, 200) ppb.run(starting_scene=TextScene)
""" Tests the sound system without initializing anything else. NOTE: Does not open a window. """ import ppb class Scene(ppb.BaseScene): sound = ppb.Sound("laser1.ogg") running = 0 lifespan = 2 def on_scene_started(self, event, signal): print("Scene start") signal(ppb.events.PlaySound(sound=self.sound)) def on_update(self, event, signal): self.running += event.time_delta if self.running > self.lifespan: signal(ppb.events.Quit()) ppb.run(starting_scene=Scene, basic_systems=(ppb.systems.Updater, ppb.systems.SoundController, ppb.assetlib.AssetLoadingSystem))
duration = 1.0 def __init__(self, *p, **kw): super().__init__(*p, **kw) self.frames = 0 self.start_time = ppb.get_time() def on_update(self, event, signal): """ Fires at the update rate (~60 times a second) """ t = ppb.get_time() - self.start_time if t >= self.duration: signal(ppb.events.Quit()) def on_pre_render(self, event, signal): """ Fires each frame. The frame rate is variable and different from the update rate. """ t = ppb.get_time() - self.start_time self.frames += 1 print(f"Frame {self.frames} rendered at {t}") if __name__ == "__main__": ppb.run(starting_scene=TestScene, log_level=logging.INFO)
self.bullet = DelayedImage('bullet.png') self.player = DelayedImage('player.png') self.target = DelayedImage('target.png') def on_update(self, event, signal): self.counter += 1 assert self.bullet.is_loaded() assert self.player.is_loaded() assert self.target.is_loaded() if self.counter >= self.loop_count: signal(ppb.events.Quit()) 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)) ppb.run(starting_scene=LoadingScene)
import ppb from ppb.assetlib import AssetLoadingSystem from ppb.systems import EventPoller, Renderer, Updater from noise.playerscene import PlayerScene from noise.playersystem import NoiseSoundController ppb.run( starting_scene=PlayerScene, basic_systems=(Renderer, Updater, EventPoller, NoiseSoundController, AssetLoadingSystem), )
else: for target in scene.get(tag='target'): d = (target.position - self.position).length if d <= target.radius: scene.remove(self) scene.remove(target) break class Target(ppb.Sprite): radius = 0.5 class GameScene(ppb.Scene): def __init__(self, *p, **kw): super().__init__(*p, **kw) # Set up sprites self.add(Player(position=Vector(0, 0)), tags=['player']) # 5 targets in x = -3.75 -> 3.75, with margin for x in (-3, -1.5, 0, 1.5, 3): self.add(Target(position=Vector(x, 1.875)), tags=['target']) if __name__ == "__main__": ppb.run( starting_scene=GameScene, log_level=logging.DEBUG, )
def on_update(self, event, signal): # This assumes all planets have the same mass force = sum((delta / (len(delta)**2) for planet, delta in self.get_bodies(event.scene)), Vector(0, 0)) self.velocity += force * self.G_CONST * event.time_delta self.stage_changes(position=self.position + self.velocity * event.time_delta) def setup(scene): scene.add( Planet(position=(3, 0), velocity=Vector(0, 1), image=ppb.Circle(40, 200, 150))) scene.add( Planet(position=(-3, 3), velocity=Vector(1, -1), image=ppb.Circle(200, 150, 40))) scene.add( Planet(position=(-3, -3), velocity=Vector(-1, 0), image=ppb.Circle(150, 40, 200))) if __name__ == "__main__": ppb.run(setup, systems=[TwoPhaseSystem])
import math import ppb from ppb.features.animation import Animation import ppb.events as events class Blob(ppb.BaseSprite): image = Animation("resources/blob_{0..6}.png", 10) target = ppb.Vector(0, 0) speed = 1 def on_mouse_motion(self, event: events.MouseMotion, signal): self.target = event.position def on_update(self, event: events.Update, signal): intent_vector = self.target - self.position if intent_vector: self.position += intent_vector.scale(self.speed * event.time_delta) self.rotation = math.degrees( math.atan2(intent_vector.y, intent_vector.x)) - 90 def setup(scene): scene.add(Blob()) ppb.run(setup)
class Player(ppb.BaseSprite): left = keycodes.Left right = keycodes.Right def on_scene_started(self, ev: SceneStarted, signal): self.position = ppb.Vector(-4, 0) self.tweener = ppb_tween.Tweener() ev.scene.add(self.tweener) def on_key_pressed(self, key_event: KeyPressed, signal): if key_event.key == self.left: self.tweener.tween(self, 'position', ppb.Vector(-4, 0), 1, easing='bounce_out') elif key_event.key == self.right: self.tweener.tween(self, 'position', ppb.Vector(4, 0), 1, easing='bounce_out') def setup(scene): scene.add(Player()) ppb.run(setup, systems=(ppb_tween.Tweening, ))