Exemplo n.º 1
0
# coding: utf-8
"""Title screen."""
import wasabi2d as w2d

scene = w2d.Scene(background="#223366")

cx, cy = scene.width // 2, scene.height // 2

title = scene.layers[0].add_label(
    "Game maths\nin 10 minutes",
    pos=(cx, -200),
    font='comfortaa',
    fontsize=68,
    align='center',
)
subtitle = scene.layers[0].add_label(
    "Daniel Pope     @lordmauve",
    pos=(cx, -10),
    font='comfortaa',
    fontsize=30,
    align='center',
    color="#dd88ff",
)


async def animate_title():
    w2d.animate(title, tween='bounce_end', y=cy - 80)
    await w2d.clock.coro.sleep(1 / 2.75)
    w2d.animate(title, tween='decelerate', duration=0.6, angle=0.1)
    await w2d.clock.coro.sleep(0.2)
    w2d.animate(subtitle, tween='bounce_end', y=cy + 200)
Exemplo n.º 2
0
import wasabi2d as w2d
import random
from pygame.math import Vector2
from wasabi2d.keyboard import keys
from wasabi2d import chain

from pygame import joystick

CYAN = (0, 1, 1)
RED = (1, 0.2, 0.2)

mode_1080p = 1920, 1080
scene = w2d.Scene(*mode_1080p,
                  #fullscreen=True,
                  )
scene.chain = [
    chain.Light(light=[
        chain.Layers([99]),
    ],
                diffuse=chain.LayerRange(stop=10),
                ambient='#333333')
]

center = Vector2(scene.width, scene.height) / 2

scene.layers[-2].add_sprite('background', pos=center)

red_score = scene.layers[-1].add_label(0,
                                       pos=(30, 100),
                                       font="bitwise",
                                       fontsize=100,
Exemplo n.º 3
0
        fill=False,
        color=GREEN,
        stroke_width=LINE_W,
    )
    ship.pos = ship.initial_pos = pos
    ship.angle = ship.initial_angle = angle
    ship.v = forward(ship, 160)
    ship.initial_v = Vector2(ship.v)
    ship.radius = 7
    ship.dead = False
    return ship


mode_720p = 1280, 720
mode_1080p = 1920, 1080
scene = w2d.Scene(*mode_720p, fullscreen=False)
scene.chain = [
    w2d.LayerRange().wrap_effect('trails', alpha=0.4,
                                 fade=0.08).wrap_effect('bloom', radius=8)
]

center = Vector2(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
Exemplo n.º 4
0
import math
import numpy as np
from dataclasses import dataclass
from typing import Any

import wasabi2d as w2d
from wasabi2d.keyboard import keyboard, keys


scene = w2d.Scene(
    width=1280,
    height=720,
    background="#ccaa88",
)

tilemap = scene.layers[0].add_tile_map()
tilemap.fill_rect(
    'sand_base_1',
    left=0,
    right=scene.width // 64 + 1,
    top=0,
    bottom=scene.height // 64 + 1,
)
tilemap.line(
    'sand_road_lr',
    (2, 1),
    (20, 1)
)
tilemap.line(
    'sand_road_ud',
    (1, 2),
Exemplo n.º 5
0
"""Example of the greyscale effect."""
import wasabi2d as w2d

scene = w2d.Scene()

photo = scene.layers[0].add_sprite(
    'positano',
    pos=(scene.width / 2, scene.height / 2),
)
photo.scale = max(scene.width / photo.width, scene.height / photo.height)
grey = scene.layers[0].set_effect('greyscale')


@w2d.event
def on_mouse_move(pos):
    x, y = pos
    frac = x / scene.width
    grey.amount = frac


w2d.run()
Exemplo n.º 6
0
import wasabi2d as w2d
from wasabi2d.rect import ZRect
from wasabi2d.keyboard import keys
import random
import math

scene = w2d.Scene(1280, 720, title="Jumpatron", background="#2288cc", fullscreen=True)
main_layer = scene.layers[0]

floor = scene.height - 70
GRAVITY = 3000
SPEED = 301


class Char:
    def __init__(self, base, slot):
        self.base = base
        self.slot = slot
        pos = (slots[slot], floor)
        self.sprite = main_layer.add_sprite(
            f'{self.base}_walk1',
            pos=pos,
            anchor_x='center',
            anchor_y='bottom',
        )
        self.n = 0
        self.vy = 0
        self.can_jump = True
        self.jump_sound = w2d.sounds.load(f'{self.base}_jump')

        self.create_badge()
Exemplo n.º 7
0
from functools import reduce  # for bounds union
from bisect import insort  # for cdtf_die into lower_dice

# (3) define some _pretty_ distinguishable colors
colors = ["#fed402", "#0477bb", "#02a039", "#ef7c02", "#925ea4", "#ffffff"]
# (52) define color names for easier debugging
names = ["yellow", "blue", "green", "orange", "purple", "white"]


# (6) always add tuples
def add(s, t):
    return tuple(map(sum, zip(s, t)))


# (0) basic scene
scene = w.Scene(width=400, height=400, background='#111111')
scene.title = "Minimal Viable Product"


# (1) field objects are white rects attached to a layer, grouped
class Fields(w.Group):
    def __init__(self, layer, ypos=25, color=None):
        super().__init__(self)
        self.layer = layer
        self.color = color
        self.whites = [self.square(i, ypos) for i in range(11)]
        # (23) the field object also keeps its own scores # TODO: move
        self.scores = []
# (27) proto square function to separate some logic

    def _square(self, pos, color='white', size=30):
Exemplo n.º 8
0
"""Pinball collision demo.

"""
import wasabi2d as w2d
import random
from pygame.math import Vector2 as v2
from pygame import Rect

from spatial_hash import SpatialHash

scene = w2d.Scene(1280, 720)
scene.chain = [
    w2d.chain.LayerRange(stop=0),
    w2d.chain.DisplacementMap(displacement=w2d.chain.Layers([1]),
                              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 = v2(0, 1)
BALL_RADIUS = 15
BALL_COLOR = (34, 128, 75)
ELASTICITY = 0.3
SEPARATION_STEPS = [1.0] * 10

BALL_COUNT = 20
Exemplo n.º 9
0
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(
Exemplo n.º 10
0
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
Exemplo n.º 11
0
        fill=False,
        color=GREEN,
        stroke_width=LINE_W,
    )
    ship.pos = ship.initial_pos = pos
    ship.angle = ship.initial_angle = angle
    ship.v = forward(ship, 160)
    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
Exemplo n.º 12
0
import wasabi2d as w2d
import math
from pygame import Rect
from wasabi2d import Vector2
from wasabi2d.actor import Actor

TILE = 21
TILES_W = 15
TILES_H = 10

grid = set()
grid.update((x, TILES_H) for x in range(TILES_W))
grid.update((-1, y) for y in range(-TILES_H, TILES_H))
grid.update((TILES_W, y) for y in range(-TILES_H, TILES_H))

scene = w2d.Scene(width=TILE * TILES_W, height=TILE * TILES_H, scaler=True)
scene.background = '#5e81a2'
scene.layers[1].set_effect('dropshadow', radius=2, offset=(0, 1))

alien = scene.layers[1].add_sprite(
    'pc_standing',
    anchor_x=10,
    anchor_y=21,
    pos=(210, TILE * 9)
)
alien.fpos = Vector2(*alien.pos)
alien.v = Vector2(0, 0)
alien.stood = True
alien.crouch = False

Exemplo n.º 13
0
from pygame import Rect
from wasabi2d import Vector2
from wasabi2d.actor import Actor

TILE = 21
TILES_W = 15
TILES_H = 10

grid = set()
grid.update((x, TILES_H) for x in range(TILES_W))
grid.update((-1, y) for y in range(-TILES_H, TILES_H))
grid.update((TILES_W, y) for y in range(-TILES_H, TILES_H))

scene = w2d.Scene(
    width=TILE * TILES_W,
    height=TILE * TILES_H,
    scaler=True,
    pixel_art=True,
)
scene.background = '#5e81a2'
scene.layers[1].set_effect('dropshadow', radius=2, offset=(0, 1))

alien = scene.layers[1].add_sprite('pc_standing',
                                   anchor_x=10,
                                   anchor_y=21,
                                   pos=(210, TILE * 9))
alien.fpos = Vector2(*alien.pos)
alien.v = Vector2(0, 0)
alien.stood = True
alien.crouch = False

Exemplo n.º 14
0
import wasabi2d as w2d
from wasabi2d.color import darker
from pygame.joystick import Joystick

scene = w2d.Scene(1280, 720, fullscreen=False)

step = scene.width / 5
mid = scene.height / 2
center = scene.width / 2, mid

colors = ['cyan', 'green', 'red', 'yellow', 'silver']

particles = scene.layers[0].add_particle_group(
    max_age=2,
    drag=0.5,
)
particles.add_color_stop(0, (1, 1, 1, 1))
particles.add_color_stop(2, (1, 1, 1, 0))

sounds = [
    w2d.sounds.cymbal,
    w2d.sounds.snare1,
    w2d.sounds.snare2,
    w2d.sounds.hihat,
    w2d.sounds.kick,
]

SMALL = scene.height / 10
BIG = step / 2

params = dict(radius=SMALL,
Exemplo n.º 15
0
    ship = scene.layers[0].add_polygon(
        SHIP_PTS,
        fill=False,
        color=GREEN,
        stroke_width=LINE_W,
    )
    ship.pos = ship.initial_pos = pos
    ship.angle = ship.initial_angle = angle
    ship.v = forward(ship, 160)
    ship.initial_v = Vector2(ship.v)
    ship.radius = 7
    ship.dead = False
    return ship


scene = w2d.Scene(1200, 800)
scene.chain = [
    w2d.LayerRange().wrap_effect('trails', alpha=0.4,
                                 fade=0.08).wrap_effect('bloom', radius=8)
]

center = Vector2(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
Exemplo n.º 16
0
import math
import wasabi2d as w2d
from wasabi2d import Vector2

scene = w2d.Scene(antialias=8)
scene.background = (0, 0.03, 0.1)

ship = scene.layers[2].add_sprite('ship',
                                  pos=(scene.width / 2, scene.height / 2))
circ = scene.layers[0].add_circle(
    radius=30,
    pos=(100, 100),
    color='cyan',
    fill=False,
    stroke_width=3.0,
)
star = scene.layers[0].add_star(points=6,
                                inner_radius=30,
                                outer_radius=60,
                                pos=(400, 100),
                                color='yellow')
scene.layers[0].add_circle(
    radius=60,
    pos=(480, 120),
    color='#ff000088',
)
lbl = scene.layers[0].add_label("Time: 0s",
                                font='bubblegum_sans',
                                pos=(scene.width * 0.5, 560),
                                align='right')
lbl.color = 'yellow'
Exemplo n.º 17
0
    controllers = {}
    for stick in joysticks:
        stick.init()
        print((stick.get_name(), stick.get_init()))
        controllers[stick.get_id()] = stick
    return controllers


pygame.init()
controllers = init_gamepads()
print(controllers)

mode_1080p = 1920, 1080
scene = w2d.Scene(*mode_1080p,
                  title='Wasabi2d Alien Demo',
                  fullscreen=True,
                  background='black',
                  rootdir='.')

alien = scene.layers[0].add_sprite('p3_stand',
                                   pos=(scene.width / 2, scene.height / 2))
alien.speed = [0, 0]
alien.move_distance = 0
alien.jumping = False
alien.jump_speed = [0, 0]
alien.start_jump_y = 0

lasers = []


@w2d.event
Exemplo n.º 18
0
import wasabi2d as w2d
from pygame import joystick
from pygame.math import Vector2

scene = w2d.Scene(width=400, height=600, background='white')

midline = 200

controllers = {}

LSTICK_CENTER = Vector2(-31.5, -2)
RSTICK_CENTER = Vector2(31.5, -2)

from wasabi2d.color import convert_color_rgb, darker
GREEN = convert_color_rgb('#88aa00')
RED = convert_color_rgb('#aa0000')
BLUE = convert_color_rgb('#0088aa')
YELLOW = convert_color_rgb('#d4aa00')
colors = [GREEN, RED, BLUE, YELLOW]


def create_gamepad(device_index):
    stick = joystick.Joystick(device_index)
    instance = stick.get_instance_id()
    if instance in controllers:
        print("Duplicate", instance)
        return

    group = w2d.Group([
        scene.layers[0].add_sprite('gamepad_base'), scene.layers[0].add_sprite(
            'stick', pos=LSTICK_CENTER), scene.layers[0].add_sprite(