async def enemy(): color = colorsys.hsv_to_rgb(random.random(), 1, 1) pos = vec2(random.uniform(50, scene.width - 50), random.uniform(50, scene.height - 50)) e = scene.layers[0].add_circle( radius=10, color=color, pos=pos, ) e.scale = 0.1 await animate( e, duration=0.3, scale=1, ) async for dt in clock.coro.frames_dt(): to_target = target - pos if to_target.length() < e.radius: break pos += to_target.scaled_to(100 * dt) e.pos = pos scene.camera.screen_shake() await animate(e, duration=0.1, scale=10, tween='accelerate', color=(*color, 0)) e.delete()
async def drive_ship(): while True: x, y = ship.pos target = vec2( random.uniform(50, scene.width - 50), random.uniform(50, scene.height - 50), ) dist, angle = (target - ship.pos).to_polar() duration = (dist / 500) ** 0.5 # Rotate to face await animate(ship, duration=0.5, angle=angle) # Begin emitting particles clock.coro.run(thrust(duration * 0.8)) # Move scene.layers[0].set_effect('trails', fade=1e-2) await animate( ship, duration=duration, tween='accel_decel', pos=target, ) scene.layers[0].clear_effect()
def __init__(self, x, y, radius=15): self.sprite = scene.layers[0].add_sprite( 'steel', scale=radius / 32, ) self.refl = scene.layers[1].add_sprite( 'lens_fresnel', scale=radius / 100, color=(1, 1, 1, 0.5) ) self.velocity = vec2(0, 0) self.radius = radius self.mass = radius * radius dr = vec2(radius, radius) self.rect = Rect(0, 0, *(dr * 2)) self.pos = vec2(x, y)
def separate(a, b, frac=0.5): """Move a and b apart. Return True if they are now separate. """ ab = a.pos - b.pos sep = ab.length() overlap = a.radius + b.radius - sep if overlap <= 0: return if sep == 0.0: ab = vec2(1, 0) else: ab /= sep masses = a.mass + b.mass if overlap > 1: overlap *= frac a.pos += ab * (overlap * b.mass / masses) b.pos -= ab * (overlap * a.mass / masses)
def on_key_down(key, mod): if key == key.F11: import pdb pdb.set_trace() elif key == key.K_1: lbl.align = 'left' elif key == key.K_2: lbl.align = 'center' elif key == key.K_3: lbl.align = 'right' elif key == key.SPACE: bullet = scene.layers[0].add_sprite('tiny_bullet', pos=ship.pos) bullet.color = (1, 0, 0, 1) bullet.vel = vec2(600, 0).rotated(ship.angle) bullet.power = 1.0 bullets.append(bullet) w2d.sounds.laser.play() w2d.animate(ship[0], 'accel_decel', 0.5, pos=next(orbiter_positions)) elif key == key.Z: pos = ship.local_to_world((-10, 0)) w2d.clock.coro.run(bomb(pos))
def on_mouse_move(pos): global target cursor.pos = pos target = vec2(pos)
import random import wasabi2d as w2d import colorsys from wasabi2d import clock, vec2, animate scene = w2d.Scene(title="Run!") center = vec2(scene.width, scene.height) / 2 scene.background = 'white' target = vec2(scene.width, scene.height) / 2 scene.layers[0].set_effect('dropshadow', opacity=1, radius=1) cursor = scene.layers[0].add_polygon( [(0, 0), (-15, 5), (-13, 0), (-15, -5)], fill=False, color='black', stroke_width=3, ) cursor.angle = 4 async def enemy(): color = colorsys.hsv_to_rgb(random.random(), 1, 1) pos = vec2(random.uniform(50, scene.width - 50), random.uniform(50, scene.height - 50)) e = scene.layers[0].add_circle( radius=10, color=color, pos=pos, ) e.scale = 0.1 await animate(
vel_spread=20, size=8, spin_spread=3, ) ship = Group( [ scene.layers[2].add_sprite('orbiter', pos=(-10, 50)), scene.layers[2].add_sprite( 'ship', anchor_x=0, ), plume ], pos=(scene.width / 2, scene.height / 2), ) ship.vel = vec2(0, 0) bullets = [] SHIFT = w2d.keymods.LSHIFT | w2d.keymods.RSHIFT @w2d.event def on_key_down(key, mod): if key == key.F11: import pdb pdb.set_trace() elif key == key.K_1: lbl.align = 'left' elif key == key.K_2:
def pos(self): return vec2(self._pos)
paint=w2d.chain.LayerRange(stop=0), scale=-100 ) ] scene.layers[0].set_effect('dropshadow', offset=(3, 3)) cursor = scene.layers[0].add_sprite( 'cursor', anchor_x='left', anchor_y='top' ) scene.layers[-1].add_sprite('wood', anchor_x='left', anchor_y='top') GRAVITY = vec2(0, 1) BALL_RADIUS = 15 BALL_COLOR = (34, 128, 75) ELASTICITY = 0.3 SEPARATION_STEPS = [1.0] * 10 BALL_COUNT = 20 class Ball: def __init__(self, x, y, radius=15): self.sprite = scene.layers[0].add_sprite( 'steel', scale=radius / 32, ) self.refl = scene.layers[1].add_sprite(
def forward(ship, length=1) -> vec2: """Get a vector in the direction of the ship.""" return vec2(length, 0).rotated(ship.angle)
ship.initial_v = ship.v ship.radius = 7 ship.dead = False return ship mode_720p = 1280, 720 mode_1080p = 1920, 1080 scene = w2d.Scene(*mode_720p, fullscreen=True) scene.chain = [ w2d.LayerRange() .wrap_effect('trails', alpha=0.4, fade=0.08) .wrap_effect('bloom', radius=8) ] center = vec2(scene.width, scene.height) * 0.5 score1 = scene.layers[0].add_label('0', pos=(10, 40), fontsize=30, color=GREEN) score2 = scene.layers[0].add_label( '0', pos=(scene.width - 10, 40), align='right', fontsize=30, color=GREEN ) score1.value = score2.value = 0 fps = scene.layers[0].add_label( 'FPS: 60', pos=(10, scene.height - 10), fontsize=20,
def pos(touch_event): return w2d.vec2(touch_event.x * scene.width, touch_event.y * scene.height)
import wasabi2d as w2d import pygame._sdl2.touch import random import colorsys from wasabi2d import vec2 scene = w2d.Scene(1280, 720, background='white') scene.chain = [w2d.chain.LayerRange().wrap_effect('outline')] particles = scene.layers[0].add_particle_group( max_age=2, grow=0.2, gravity=vec2(0, -200), drag=0.5, ) def pos(touch_event): return w2d.vec2(touch_event.x * scene.width, touch_event.y * scene.height) async def next_touch(): touch = w2d.events.subscribe( pygame.FINGERDOWN, pygame.FINGERMOTION, pygame.FINGERUP, ) async for ev in touch: if ev.type == pygame.FINGERDOWN: finger_id = ev.finger_id touch_start = pos(ev) break
import math import random import wasabi2d as w2d from wasabi2d import clock, vec2, animate scene = w2d.Scene() trail = scene.layers[-1].add_particle_group(max_age=2, grow=0.1) bottomright = vec2(scene.width, scene.height) center = bottomright / 2 ship = scene.layers[0].add_sprite( 'ship', pos=center ) async def drive_ship(): while True: x, y = ship.pos target = vec2( random.uniform(50, scene.width - 50), random.uniform(50, scene.height - 50), ) dist, angle = (target - ship.pos).to_polar() duration = (dist / 500) ** 0.5 # Rotate to face await animate(ship, duration=0.5, angle=angle)