NAME

Game::Terminal::Kit::Ticker - when the next step of a game that moves is due

VERSION

Version 0.05

SYNOPSIS

use Game::Terminal::Kit::Ticker;

my $ticker = Game::Terminal::Kit::Ticker->new;
$ticker->start(1 / $speed);

while ($playing) {
    unless ($ticker->due) {
        my $key = $keys->read_key($ticker->remaining);
        steer($key) if defined $key;
        next;
    }
    advance();
    $ticker->tick(1 / $speed);
    draw();
}

DESCRIPTION

A game that moves on its own steps at a steady rate however long each frame took to draw and however many keys were pressed in between. The ticker keeps the time of the next step. Between steps the game waits on the keyboard for exactly as long as is left, so a key is acted on at once and a step is never late for the sake of one.

Each step is scheduled from when the last one was due, not from when it happened, so the rate does not drift. When the game falls a whole step behind, the schedule starts again from now and the step is counted in "late": a burst of steps nobody saw is how a game punishes the player for a slow machine.

ATTRIBUTES

clock

A code reference returning the time in seconds. Time::HiRes::time by default; a test gives its own.

next

When the next step is due, by that clock.

ticks

How many steps have been taken.

late

How many of them were more than a whole step behind.

METHODS

start

$ticker->start($period);

The first step is due one period from now. Returns the ticker.

remaining

Seconds until the next step is due; 0 when it is due, overdue, or the ticker was never started. Never negative.

due

True when nothing remains.

tick

$ticker->tick($period);

A step has been taken; the next is due one period after this one was. The period is given each time because a game speeds up. Returns the ticker.

SEE ALSO

Game::Terminal::Kit, Game::Terminal::Kit::Keys.

AUTHOR

LNATION, <email at lnation.org>

LICENSE AND COPYRIGHT

This software is Copyright (c) 2026 by LNATION.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)