beetlebug▾
/

Games: the loop and actions

ztk::game gives a 2D game the parts that each game writes by hand: a loop at a fixed step, the input as named actions, random numbers from a seed, a fit to the screen, and saves. The game draws with the widgets of Tk, such as a canvas. The same script runs on a desktop, on the web, and on a phone.

Skate Pond runs on ztk::game.

A complete game

A ball falls, and Space, the up arrow, or a tap makes it jump. A miss of the floor ends the run. The save keeps the best count of jumps.

interp alias {} game {} ztk::game
canvas .c -width 320 -height 180 -background #101820 -highlightthickness 0
pack .c
.c create rectangle 0 170 320 180 -fill #3b4456 -width 0
.c create oval 150 60 170 80 -fill #ffe27a -width 0 -tags ball
.c create text 8 8 -anchor nw -fill white -tags score
game action jump -keys {space Up} -touch tap
game view . -fit letterbox
set best [dict get [game load jumper {best 0}] best]
set vy 0; set jumps 0
proc step {dt} {
    global vy jumps best
    if {[game pressed jump]} { set vy -260; incr jumps }
    set vy [expr {$vy + 700 * $dt}]
    .c move ball [expr {40 * $dt}] [expr {$vy * $dt}]
    lassign [.c coords ball] x0 y0 x1 y1
    if {$y1 > 170} { .c move ball 0 [expr {170 - $y1}]; set vy 0 }
    if {$x0 > 320} {
        .c move ball -340 0
        if {$jumps > $best} { set best $jumps; game save jumper [dict create best $best] }
        set jumps 0
    }
    .c itemconfigure score -text "jumps $jumps  best $best"
}
game loop -update step -rate 60

The loop

game loop -update SCRIPT -rate 60 runs SCRIPT 60 times in each second of the game, with the step in seconds appended. A fixed step makes the game run at the same speed on a slow phone and on a fast desktop, and gives the same result for the same input.

The loop runs from the frames of the display, and it uses no after timer. Each frame adds the time since the last frame, and runs a step for each whole step of that time. When a frame comes late, the loop runs at most -maxsteps steps (5 by default) and drops the rest, so a long pause does not start a run of catch-up steps. game info counts the dropped steps.

game loop -scale 0.5 runs the game at half speed, and -scale 2 at twice the speed. The step that the update script gets stays the same, so the game gives the same result for the same input at each speed: the scale changes how many steps run in a second. -scale 0 runs no step and keeps the draw script, for a frozen frame that still draws.

game time counts the steps of the game. It goes on over game stop and a new game loop, so a page that runs the loop only while something moves keeps one clock; game loop -time 0 starts it again. dict get [game info] seconds gives the time of the game in seconds, with the part of a step.

-draw SCRIPT runs once for each frame, after the steps, with the fraction of a step that is left. A game that moves its items in -update needs no -draw.

game pause and game resume stop and start the steps. The app in the background on a phone, or a hidden page on the web, pauses the loop and sends <<GamePause>> to the window. The return sends <<GameResume>>, and the game shows its own pause screen until the script calls game resume. With -pauseonblur 1, a window that loses the focus pauses the loop too.

-budget UNITS gives each step a count of compute units. A step that uses more stops the loop with the error code ZTK GAME BUDGET, and the app stays live.

Actions

An action is a name for an input: jump, left, or fire. game action NAME -keys {space Up} -touch tap maps keys and zones of the screen to it. A step reads it:

A key and a finger on the same action give one press. A letter key matches with and without Shift, and a key that repeats gives no new press. A zone is the whole view (tap), a half or a third of it, a box of logical pixels, or a swipe (swipe-up, swipe-down, swipe-left, swipe-right).

game axis NAME -keys {Left Right} -drag x gives a number from -1 to 1. game pointers gives each finger and the mouse, for a game that reads its own taps.

Controllers come in a later stage. -pad names are kept now, so a script can name them, and game pad list gives an empty list.

Random numbers

game random gives a float from 0 to 1, game random 1 6 an integer from 1 to 6, and game random -seed 42 starts the generator again. The generator gives the same numbers on each platform, so a seed and the same input give the same game. Use it for the game and for its effects, in place of rand().

The fit

game view . -fit letterbox zooms the window so that the game fills the most room inside the insets of the screen, with the game in the middle. -fit integer zooms by whole numbers only, for pixel art, and -fit fill covers the window. -size {320 180} gives the logical size of the game, and with no -size the game has the requested size of its widgets. The fit runs again at each resize and each change of the insets. -orientation landscape keeps a phone in landscape.

Saves

game save NAME DICT writes a dict, and game load NAME DEFAULT reads it back with the keys of DEFAULT that the save does not have. A name has 1 to 64 characters of a-z, 0-9, _, and -, and a save has at most 1 MiB. The saves go to the folder game-saves:

Tests

A headless run moves its clock one step at a time, so a test gets the same steps each time:

ztk --headless game.tcl

In a test, game step N runs N steps at once, also while the loop is paused, and ztk::clock set moves the clock to test a late frame.