Atom

Small complete game

Save as lane-runner.atom next to ray.atom. Run atom run lane-runner.atom -- /path/to/libraylib.so. Use the arrow keys to change lanes, collect the falling squares, and press Space to restart after five catches or misses. Escape closes the window. --verify after the library path tests the game rules and collision bindings without opening the window. --verify-window renders three hidden frames.

Handle a catch or a miss

Each update moves the square down. A collision adds one catch. A square that falls past the screen adds one miss. Either event starts a new drop.

Excerpt
func advance(ray, state):
    state.y += 4
    if hit(ray, state):
        state.score += 1
        next_drop(state)
    elif state.y > 360:
        state.missed += 1
        next_drop(state)
Show full source 86 lines
lane-runner.atom
use './ray.atom' as gfx
use ffi
use os

const lane_left = 263
const lane_right = 262
const space = 32

func reset():
    return {lane = 1, drop = 0, y = 0, score = 0, missed = 0}

func x_position(lane):
    return 70 + lane * 100

func hit(ray, state):
    player = gfx.rectangle(x_position(state.lane), 340, 44, 12)
    square = gfx.rectangle(x_position(state.drop), state.y, 24, 24)
    return ray.CheckCollisionRecs(player, square)

func next_drop(state):
    state.drop = (state.drop + 2) % 3
    state.y = 0

func advance(ray, state):
    state.y += 4
    if hit(ray, state):
        state.score += 1
        next_drop(state)
    elif state.y > 360:
        state.missed += 1
        next_drop(state)

ray = gfx.open_graphics(os.args[0])
try:
    mode = get(os.args, 1, '')
    state = reset()
    if mode == '--verify':
        state.lane = 0
        state.y = 332
        advance(ray, state)
        assert(state.score == 1 and state.drop == 2)
        state.y = 361
        advance(ray, state)
        assert(state.missed == 1)
        return
    if mode == '--verify-window':
        ray.SetConfigFlags(128)
    ray.InitWindow(384, 400, 'Lane Runner')
    assert(ray.IsWindowReady())
    try:
        ray.SetTargetFPS(60)
        background = gfx.color(23, 37, 51)
        player_color = gfx.color(155, 222, 210)
        drop_color = gfx.color(255, 192, 105)
        hud = gfx.color(244, 246, 247)
        frames = 0
        while not ray.WindowShouldClose():
            finished = state.score >= 5 or state.missed >= 5
            if finished:
                if ray.IsKeyPressed(space):
                    state = reset()
                    finished = false
            else:
                if ray.IsKeyPressed(lane_left) and state.lane > 0:
                    state.lane -= 1
                if ray.IsKeyPressed(lane_right) and state.lane < 2:
                    state.lane += 1
                advance(ray, state)

            ray.BeginDrawing()
            try:
                ray.ClearBackground(background)
                ray.DrawRectangleRec(gfx.rectangle(x_position(state.lane), 340, 44, 12), player_color)
                ray.DrawRectangleRec(gfx.rectangle(x_position(state.drop), state.y, 24, 24), drop_color)
                ray.DrawText('Caught: {state.score}  Missed: {state.missed}', 14, 372, 18, hud)
                if finished:
                    ray.DrawText('Space to play again', 82, 180, 20, hud)
            finally:
                ray.EndDrawing()
            frames += 1
            if mode == '--verify-window' and frames == 3:
                break
    finally:
        ray.CloseWindow()
finally:
    ffi.close(ray)