beetlebug▾
/

ztk::game

Runs the loop of a 2D game at a fixed step, reads the input as named actions, gives random numbers from a seed, fits the game to the window, and keeps saves. Games: the loop and actions has a guide. The reference entry has each subcommand with its examples and errors.

This is stage 1 of the design in docs/dev/game-apis.md. The sprite layer, controllers, sound, physics, and record and replay come in later stages.

Synopsis

ztk::game loop ?-update SCRIPT? ?-draw SCRIPT? ?-rate N? ?-budget UNITS? ?-maxsteps N? ?-pauseonblur BOOLEAN? ?-scale F? ?-time N?
ztk::game stop
ztk::game pause
ztk::game resume
ztk::game step ?COUNT?
ztk::game time
ztk::game info
ztk::game random ?MIN MAX?
ztk::game random -seed N
ztk::game action ?NAME? ?-keys KEYS? ?-touch ZONES? ?-pad BUTTONS?
ztk::game axis NAME ?-keys {NEGATIVE POSITIVE}? ?-drag x|y? ?-pad AXIS? ?-tilt x|y?
ztk::game pressed NAME
ztk::game held NAME
ztk::game released NAME
ztk::game pointers
ztk::game pad list
ztk::game view ?PATH? ?-size {W H}? ?-fit letterbox|fill|integer|none? ?-orientation landscape|portrait|any? ?-fullscreen BOOLEAN?
ztk::game save NAME DICT
ztk::game load NAME ?DEFAULT?

Arguments

Name Type Default Description
loop subcommand Starts the loop, or changes its options. With no option, gives the options as a dict. -update runs at each step with the step in seconds appended. -draw runs once for each frame of the display with the fraction of a step that is left (0 to 1) appended.
-rate N integer 60 The steps in each second, from 1 to 1000.
-budget UNITS integer 0 The compute units of one run of -update. A step over its budget stops the loop with the error code ZTK GAME BUDGET through bgerror. 0 is no budget.
-maxsteps N integer 5 The most steps in one frame, from 1 to 100. A late frame drops the other steps, and info counts them.
-pauseonblur BOOLEAN boolean 0 1 pauses the loop when the view loses the focus, as for a hidden window.
stop subcommand Stops the loop. A new loop starts it again at the time 0.
pause, resume subcommand Pauses and resumes the loop. The time of a pause does not count.
step ?COUNT? subcommand Runs COUNT steps now (1 by default, at most 100,000), and then -draw once. It works while the loop is paused, for a test.
time subcommand Gives the count of steps since the start of the loop.
info subcommand Gives a dict: running, paused, rate, time, dropped, frames, seed, store (files or memory), and pads, sound, tilt, and haptics (0 in this stage).
random ?MIN MAX? subcommand Gives a number of the generator of the game: a float from 0 to 1 with no argument, an integer from MIN to MAX for two integers, or a float from MIN to MAX for two numbers.
random -seed N subcommand Starts the generator again from the integer N. The seed at the start is 0.
action NAME ?OPTION VALUE ...? subcommand Names an action and its sources: -keys is a list of key names as bind names them, -touch is a list of zones, and -pad is kept for a later stage. With NAME alone, gives the options. With no argument, gives the names.
axis NAME ?OPTION VALUE ...? subcommand Names an axis: -keys {NEGATIVE POSITIVE} and -drag x or -drag y. With NAME alone, gives its value from -1 to 1 in this step.
pressed NAME, held NAME, released NAME subcommand Gives 1 when the action was pressed in this step, is held, or was released in this step.
pointers subcommand Gives a list of dicts {id ID x X y Y state STATE} in logical pixels of the view. The id 0 is the mouse, and a finger has the id 1 or more. STATE is pressed, held, or released.
pad list subcommand Gives the controllers: an empty list in this stage.
view ?PATH? ?OPTION VALUE ...? subcommand Fits the game into the toplevel PATH (. by default) with its zoom. -size is the logical size of the game, or the requested size of the content when it is empty. With no option, gives the options.
save NAME DICT subcommand Writes DICT as the save NAME. NAME has 1 to 64 characters of a-z, 0-9, _, and -, and a save has at most 1,048,576 bytes. The file goes to game-saves of tk appdata. With no folder of tk appdata, it goes to game-saves of the current folder only with the capability file. Else the saves stay in memory for the run.
load NAME ?DEFAULT? subcommand Gives the dict DEFAULT with the keys of the save NAME over it, or DEFAULT when the save does not exist.

A zone of -touch is tap (the whole view), left-half, right-half, left-third, middle-third, right-third, a box {X0 Y0 X1 Y1} in logical pixels of the view, or swipe-up, swipe-down, swipe-left, or swipe-right.

Return value

time gives an integer, random a number, pressed, held, and released 0 or 1, axis NAME a number, and info, loop, action NAME, view, and load a dict. The other forms give an empty string.

Errors

Error When
no action "NAME" pressed, held, released, or action NAME of an action that does not exist. The error code is ZTK GAME ACTION NAME.
bad key "NAME" A key name that bind does not know.
bad zone "ZONE": must be ... An unknown zone of -touch.
bad save name "NAME": must be 1 to 64 characters of a-z, 0-9, _, and - A name of a save with another character, such as .. or /, an empty name, or a longer name. The error code is ZTK GAME SAVE NAME.
the save "NAME" has more than 1048576 bytes A save over the cap.
too many saves: at most 64 A new save when 64 saves exist.
missing value to go with key DICT or DEFAULT is not a dict.
expected a rate from 1 to 1000 but got "N" A bad -rate.
unknown or ambiguous subcommand "X": must be ... Another subcommand.

Platforms

Platform Works
Windows Yes
macOS Yes
Linux, Wayland Yes
Linux, X11 Yes
Web Yes. The saves stay in memory for the run.
iOS, Android Yes. The app in the background pauses the loop.
Headless Yes. The clock moves one step at a time, so a run gives the same steps each time.

Example

interp alias {} game {} ztk::game
canvas .c -width 200 -height 100 -background #101820 -highlightthickness 0
pack .c
.c create oval 90 40 110 60 -fill #ffe27a -tags ball
game action jump -keys {space Up} -touch tap
set vy 0
proc step {dt} {
    global vy
    if {[game pressed jump]} { set vy -200 }
    set vy [expr {$vy + 600 * $dt}]
    .c move ball 0 [expr {$vy * $dt}]
    lassign [.c coords ball] x0 y0 x1 y1
    if {$y1 > 100} { .c move ball 0 [expr {100 - $y1}]; set vy 0 }
}
game loop -update step -rate 60
event generate . <KeyPress-space>
event generate . <KeyRelease-space>
game step 10
lindex [.c coords ball] 1

See also