beetlebug

Skate Pond 2: Slide on the ice

In this step the skater slides over the pond when you press an arrow key. You learn the game loop with after, the move of items with coords, a slide with easing, and key bindings.

The game loop

A game draws a new frame about 60 times a second. In Tk, after 16 tick runs the proc tick again after 16 milliseconds. The first thing that tick does is to schedule the next call, so the loop goes on (the code):

proc tick {} {
    global g sk T
    after 16 tick
    incr g(frames)

Each frame moves the game one fixed step forward: the slide goes on by one frame, and draw puts the items where the state says. Most times of the game are counts of frames: a slide lasts from 7 to 26 frames, and after a fall into the water the game waits 50 frames. A few timers use after with milliseconds, such as the card with the name of the level (900 ms).

A push finds the end of the slide first

A push does not move anything. It finds the square where the slide ends, and it keeps the start and the end of the slide in the array sk (the code). Then the frames move the skater from the start to the end. The length of a slide in frames comes from its length in squares:

set sk(t) 0; set sk(n) [expr {max(7, min(26, 5 + $dist * 3))}]; set sk(end) $end

sk(t) goes from 0 to 1 over sk(n) frames. A slide of one square lasts 8 frames, and a long slide lasts at most 26.

Easing: fast first, slow at the end

A position that goes up by the same amount in each frame looks stiff. The game puts sk(t) through an ease-out curve: the skater starts fast and settles at the end of the slide, as a skater on ice does (the code).

proc easeOut {t} { expr {1 - pow(1 - $t, 3)} }

Move the items with coords

The skater is a group of canvas items with the tag sk. The game makes them once, at a size of one pixel, and each frame gives them new coordinates with .c coords. So the count of items stays the same from frame to frame. The snippet below has the loop, the push, the easing, and the keys. Click the window, then press the arrow keys.

# Click the window, then push with the arrow keys.
set rows {
    ############
    #S....=....#
    #..........#
    #......#...#
    #..........#
    #.#........#
    #.....=....#
    ############
}
set T 28
canvas .c -width [expr {12 * $T}] -height [expr {8 * $T}] -background #0f1b2d -highlightthickness 0
pack .c
. configure -background #0f1b2d
set grid {}
set y 0
foreach row $rows {
    set r [split $row ""]
    set x [lsearch $r S]
    if {$x >= 0} { array set sk [list x $x y $y fx $x fy $y t 1 n 1 queue {}]; lset r $x . }
    lappend grid $r
    set x 0
    foreach c $r {
        set x0 [expr {$x * $T}]; set y0 [expr {$y * $T}]
        set fill [expr {$c eq "#" ? "#f6fafd" : "#d4ebf7"}]
        .c create rectangle $x0 $y0 [expr {$x0 + $T}] [expr {$y0 + $T}] -width 0 -fill $fill
        if {$c eq "="} {
            .c create rectangle [expr {$x0 + 3}] [expr {$y0 + 3}] [expr {$x0 + $T - 3}] [expr {$y0 + $T - 3}] \
                -width 0 -fill #f6fafd
        }
        incr x
    }
    incr y
}
# The skater: two items that each frame moves.
.c create rectangle 0 0 1 1 -width 0 -fill #24324a -tags "sk skcoat"
.c create oval 0 0 1 1 -width 0 -fill #f2d2ac -tags "sk skface"

proc tile {x y} { lindex $::grid $y $x }
# A push: the skater slides until a snowbank stops it, or snow holds it.
proc push {dir} {
    global sk
    lassign [dict get {up {0 -1} down {0 1} left {-1 0} right {1 0}} $dir] dx dy
    set x $sk(x); set y $sk(y); set dist 0
    while 1 {
        set c [tile [expr {$x + $dx}] [expr {$y + $dy}]]
        if {$c eq "#"} break
        incr x $dx; incr y $dy; incr dist
        if {$c eq "="} break
    }
    if {$dist == 0} return
    set sk(fx) $sk(x); set sk(fy) $sk(y); set sk(x) $x; set sk(y) $y
    set sk(t) 0; set sk(n) [expr {max(7, min(26, 5 + $dist * 3))}]
}
proc easeOut {t} { expr {1 - pow(1 - $t, 3)} }
# One frame: the tween goes on, and the items go to their place.
proc step {} {
    global sk T
    if {$sk(t) < 1} {
        set sk(t) [expr {min(1.0, $sk(t) + 1.0 / $sk(n))}]
    } elseif {$sk(queue) ne ""} {
        push $sk(queue); set sk(queue) {}
    }
    set e [easeOut $sk(t)]
    set x [expr {($sk(fx) + ($sk(x) - $sk(fx)) * $e) * $T + 14}]
    set y [expr {($sk(fy) + ($sk(y) - $sk(fy)) * $e) * $T + 25}]
    .c coords skcoat [expr {$x - 7}] [expr {$y - 14}] [expr {$x + 7}] [expr {$y - 2}]
    .c coords skface [expr {$x - 5}] [expr {$y - 22}] [expr {$x + 5}] [expr {$y - 13}]
}
proc tick {} { after 16 tick; step }
# A press during a slide waits for its end.
bind . <KeyPress> {
    set k [string tolower %K]
    if {$k in {up down left right}} {
        if {$sk(t) < 1} { set sk(queue) $k } else { push $k }
    }
}
tick
Slide on the ice
The pond with the skater in the middle of a slide to the right, on its way to a patch of snow
Live

Change pow(1 - $t, 3) to pow(1 - $t, 1), and the slide has the same speed from start to end. Change the 3 of $dist * 3 to 8, and each slide is slow.

Key bindings

bind . <KeyPress> {...} runs the script for each key that the window gets. %K in the script is the name of the key, such as Left or space. The binding is on ., the main window, so the keys work when the window has the focus. That is why you click the window first.

The game itself has no key binding. Its one entry for input is the proc toy_input KEY STATE, with KEY one of left, right, up, down, a, b, and start (the code). A host binds the keys and calls toy_input: the web page for the site, and toyhost.tcl for a desktop or a phone (8. Save and run everywhere). So the same game runs with a keyboard, a gamepad, or touch buttons.

A press during a slide waits

A player often presses the next arrow before the slide ends. The game keeps that press in sk(queue) and pushes it when the skater stops. Without this buffer, a quick player loses moves.

if {$sk(t) < 1} { set sk(queue) $key } else { push $key }

Feel: lean, squash, and particles

The game adds small motions in draw (the code):

Next: 3. Tap and swipe.