class Player(ppb.Sprite): image = ppb.Square() position = ppb.Vector() target = ppb.Vector() def on_update(self, update_event: ppb.events.Update, signal): self.position += (self.target - self.position)
class Player(ppb.Sprite): direction = ppb.Vector(0, 0) speed = 4 position = ppb.Vector(0, -3) left = keycodes.Left right = keycodes.Right shoot = keycodes.Space def on_update(self, update_event, signal): if self.direction.x and self.direction.y: direction = self.direction.normalize() else: direction = self.direction self.position += direction * self.speed * update_event.time_delta def on_key_pressed(self, key_event: KeyPressed, signal): if key_event.key == self.left: self.direction += ppb.Vector(-1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(1, 0) elif key_event.key == self.shoot: key_event.scene.add( Projectile(position=self.position + ppb.Vector(0, 0.5))) def on_key_released(self, key_event: KeyReleased, signal): if key_event.key == self.left: self.direction += ppb.Vector(1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(-1, 0)
class Player(ppb.Sprite): position = ppb.Vector(0, -3) direction = ppb.Vector(0, 0) speed = 4 left = keycodes.Left right = keycodes.Right projector = keycodes.Space def on_update(self, update_event, signal): self.position += self.direction * self.speed * update_event.time_delta def on_key_pressed(self, key_event: KeyPressed, signal): if key_event.key == self.left: self.direction += ppb.Vector(-1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(1, 0) elif key_event.key == self.projector: key_event.scene.add( Skeleton(position=self.position + ppb.Vector(0, 0.5))) def on_key_released(self, key_event: KeyPressed, signal): if key_event.key == self.left: self.direction += ppb.Vector(1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(-1, 0)
class Player(ppb.BaseSprite): position = ppb.Vector(0, 0) velocity = ppb.Vector(0, 0) speed = 4 controls = { "left": keycodes.A, "right": keycodes.D, "project": keycodes.Space, } def on_update(self, event: events.Update, signal): velocity = self.velocity if velocity: velocity = self.velocity.scale(self.speed) self.position += velocity * event.time_delta def on_key_pressed(self, event: events.KeyPressed, signal): target_key = event.key if target_key == self.controls["left"]: self.velocity += ppb.Vector(-self.speed, 0) elif target_key == self.controls["right"]: self.velocity += ppb.Vector(self.speed, 0) elif target_key == self.controls["project"]: signal(Project(self.top.center)) def on_key_released(self, event: events.KeyReleased, signal): target_key = event.key if target_key == self.controls["left"]: self.velocity += ppb.Vector(self.speed, 0) elif target_key == self.controls["right"]: self.velocity += ppb.Vector(-self.speed, 0)
def on_key_pressed(self, key_event: KeyPressed, signal): if key_event.key == self.left: self.direction += ppb.Vector(-1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(1, 0) elif key_event.key == self.projector: key_event.scene.add(Projectile(position=self.position + ppb.Vector(0, 0.5)))
def on_key_pressed(self, event: events.KeyPressed, signal): target_key = event.key if target_key == self.controls["left"]: self.velocity += ppb.Vector(-self.speed, 0) elif target_key == self.controls["right"]: self.velocity += ppb.Vector(self.speed, 0) elif target_key == self.controls["project"]: signal(Project(self.top.center))
def on_key_released(self, key_event: KeyReleased, signal): if key_event.key == self.left: self.direction += ppb.Vector(1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(-1, 0) if key_event.key == self.up: self.direction += ppb.Vector(0, -1) elif key_event.key == self.down: self.direction += ppb.Vector(0, 1)
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 on_key_pressed(self, key_event: KeyPressed, signal): if key_event.key == self.left: self.direction += ppb.Vector(-1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(1, 0) if key_event.key == self.up: self.direction += ppb.Vector(0, 1) elif key_event.key == self.down: self.direction += ppb.Vector(0, -1) if key_event.key == self.space: print(self.position)
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 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 on_viking_death(self, ev, signal): v = ev.viking.position d = (self.position - v).length if d < self.absorb_radius and not ev.claimed: self.absorbing = get_time() + 5.0 signal(ParticleSpawnerCreate( source=(v + ppb.Vector(-1, -0.4), v), dest=(v + ppb.Vector(-0.9, 1.25), v + ppb.Vector(-0.1, 1)), color=COLOR['YELLOW'], lifespan=5.0, )) delay(5.0, lambda: signal(ScorePoints(1))) delay(5.0, lambda: signal(CreateFloatingNumber(1, ev.viking.position + ppb.Vector(0, 1), COLOR['YELLOW']))) ev.claimed = True
class Player(ppb.Sprite): layer = 1 image = Image('player.png') height = 1 width = 1 position = ppb.Vector(0, 0) direction = ppb.Vector(0, 0) max_speed = 4 speed = 4 left = keycodes.Left right = keycodes.Right up = keycodes.Up down = keycodes.Down space = keycodes.Space def on_update(self, update_event, signal): if self.speed == self.max_speed: self.position += self.direction * self.speed * update_event.time_delta else: self.position -= self.direction * self.max_speed * update_event.time_delta self.speed = self.max_speed # print(self.position) def on_button_released(self, key_event: ButtonReleased, signal): if key_event.button: print(key_event.position) def on_key_pressed(self, key_event: KeyPressed, signal): if key_event.key == self.left: self.direction += ppb.Vector(-1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(1, 0) if key_event.key == self.up: self.direction += ppb.Vector(0, 1) elif key_event.key == self.down: self.direction += ppb.Vector(0, -1) if key_event.key == self.space: print(self.position) def on_key_released(self, key_event: KeyReleased, signal): if key_event.key == self.left: self.direction += ppb.Vector(1, 0) elif key_event.key == self.right: self.direction += ppb.Vector(-1, 0) if key_event.key == self.up: self.direction += ppb.Vector(0, -1) elif key_event.key == self.down: self.direction += ppb.Vector(0, 1)
def spawn_one(): source_x = uniform(source_bl.x, source_tr.x) source_y = uniform(source_bl.y, source_tr.y) dest_x = uniform(dest_bl.x, dest_tr.x) dest_y = uniform(dest_bl.y, dest_tr.y) source = ppb.Vector(source_x, source_y) dest = ppb.Vector(dest_x, dest_y) ParticleSystem.spawn( source, ev.color, dest, opacity=ev.opacity, opacity_mode=ev.opacity_mode, )
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_mouse_motion(self, mouse, signal): if not self.frame_happened: return self.frame_happened = False x, y = mouse.position cam = self.main_camera if not (cam.frame_width and cam.frame_height): return frame_left, frame_right, frame_top, frame_bottom = \ cam.frame_left, cam.frame_right, cam.frame_top, cam.frame_bottom frame_width, frame_height = cam.frame_width, cam.frame_height # if not (self.xmin <= x <= self.xmax and self.ymin <= y <= self.ymax): # return xpercent = (x - frame_left) / frame_width ypercent = (y - frame_top) / frame_height if xpercent < 0: xpercent = 0 if xpercent > 1: xpercent = 1 if ypercent < 0: ypercent = 0 if ypercent > 1: ypercent = 1 cam.position = ppb.Vector( (self.xmax - self.xmin) * xpercent + self.xmin, (self.ymax - self.ymin) * ypercent + self.ymin, )
def create(cls, scene, signal): ctrl = cls() ctrl.tweener = Tweener() ctrl.scene = scene ctrl.mode = "waiting" ctrl.marker = MushroomPlacementMarker() ctrl.radius = [RadiusMarker() for i in range(24)] scene.add(ctrl.marker) for r in ctrl.radius: scene.add(r) signal(ui.CreateButton("Smooshroom", enabled=False)) signal(ui.DisableButton("Smooshroom")) signal(ui.CreateButton("Poddacim", enabled=False)) signal(ui.DisableButton("Poddacim")) # TODO: Move to scene? ctrl.create_mushroom(Smooshroom, ppb.Vector(0, 0), signal) scene.add(ctrl) return ctrl
def on_scene_started(self, scene_started: ppb.events.SceneStarted, signal): images = [ *self.wall_images, *self.floor_images, self.blob_animation, *self.squares ] for (x, y), image in zip(product(range(-5, 6), range(-3, 4)), images): self.add(ppb.Sprite(image=image, position=ppb.Vector(x, y)))
class CreateSprite: image: typing.Union[str, ppb.Image] position: ppb.Vector = ppb.Vector(0, 0) anchor: ppb.Sprite = None layer: float = 0.0 size: float = 1.0 opacity: int = 255 sprite_class: type = ppb.Sprite
class TargetSprite(ppb.BaseSprite): """Sprite that moves to a given target""" target = ppb.Vector(0, 0) speed = 1.0 def on_update(self, update_event, signal): direction = (self.position - self.target).normalize() self.position += direction * self.speed * update_event.time_delta
def next_pos(self): if not self.target_path: return None else: try: return ppb.Vector(self.target_path[self.target_path_step]) except IndexError: return None
def on_create_floating_number(self, ev: CreateFloatingNumber, signal): T = Text(str(ev.number), ev.position, color=ev.color, layer=500) ev.scene.add(T) T.scene = ev.scene T.setup() tween(T, 'position', ev.position + ppb.Vector(0, -1), 0.5) tween(T, 'opacity', 0, 0.5) delay(0.5, lambda: T.remove(ev.scene))
class FoodUIButton(ppb.BaseSprite): image = "./resources/food-can.png" position = ppb.Vector(-5, 4) def on_button_pressed(self, button_event: ppb.events.ButtonPressed, signal): if (self.left < button_event.position.x < self.right and self.top > button_event.position.y > self.bottom): button_event.scene.add(Food())
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
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 size(self, value): self._size = value p = self.position align = self._get_alignment_offset() for i, l in enumerate(self.letters): l.size = value x = p.x + i * KERNING * value + align y = p.y l.position = ppb.Vector(x, y)
def create(cls, scene, signal): ctrl = cls() ctrl.score = 0 ctrl.text = Text(str(ctrl.score), ppb.Vector(0, 4), color=COLOR['YELLOW']) scene.add(ctrl.text) scene.add(ctrl) return ctrl
class Food(ppb.BaseSprite): size = 0.5 image = "./resources/food.png" position = ppb.Vector(0, 5) def on_update(self, event: ppb.events.Update, signal): self.position += ppb.Vector(0, -3) * event.time_delta pet = next(event.scene.get(kind=Pet)) # There's only one pet. if (pet.position - self.position).length < ((self.size + pet.size) / 2): event.scene.remove(self)
class FlyUpEffect(AnimationSprite): line = ppb.Vector(0, 1) duration = 3 def do_start(self, signal): self.position = self.aposition def do_frame(self, dt, t, signal): self.position = self.line * (t / self.duration) + self.aposition return t < self.duration
class Projectile(ppb.Sprite): size = 0.25 direction = ppb.Vector(0, 1) speed = 6 def on_update(self, update_event, signal): if self.direction: direction = self.direction.normalize() else: direction = self.direction self.position += direction * self.speed * update_event.time_delta