beetlebug

Skate Pond 3: Tap and swipe

In this step you play the pond with a finger or a mouse. A swipe pushes the skater, and a tap on a square sends the skater there by the fewest pushes. You learn the pointer bindings of a canvas and a search by breadth.

A mouse and a finger give the same events

Tk has three events for the main button of the pointer: <ButtonPress-1>, <B1-Motion> (a move with the button down), and <ButtonRelease-1>. On a touch screen, ztk sends these events for the first finger too. So one set of bindings serves a mouse, a touchpad, and a finger (the code):

bind .c <ButtonPress-1> {ptrPress %x %y}
bind .c <B1-Motion> {ptrMove %x %y}
bind .c <ButtonRelease-1> {ptrRelease %x %y}

%x and %y are the place of the pointer in the canvas, in pixels.

Swipe or tap

A finger touches an area, and it moves a little during a tap. The game uses a distance of 16 pixels to tell the two gestures apart:

proc ptrMove {x y} {
    global ptr g
    if {!$ptr(down) || $ptr(swiped) || $g(state) ne "play"} return
    set dx [expr {$x - $ptr(x)}]; set dy [expr {$y - $ptr(y)}]
    if {hypot($dx, $dy) < 16} return
    set ptr(swiped) 1
    toy_input [expr {abs($dx) > abs($dy) ? ($dx > 0 ? "right" : "left") : ($dy > 0 ? "down" : "up")}] press
}

A swipe calls toy_input, as a key does. So a swipe and an arrow key share one code path.

A tap goes to a square

A tap on a square does not mean one push. The skater can only slide, so the game must find the pushes that end on that square. The proc reach searches by breadth (the code):

  1. Start with the square of the skater. Its way is the empty list.
  2. For each square of the last round, try the four pushes. The proc slide gives the square where each slide stops.
  3. A square that has no way yet gets the way of its parent and the new push.
  4. Go on until a round finds no new square.

A search by breadth finds each square in the round of its fewest pushes. So the way of each square is a shortest way. A slide into the water is no way, and the game does not search past the flag.

The snippet marks each square of reach with a blue dot, so you can see the result of the search. Swipe on the pond, or tap a dot.

# Swipe on the pond to push, or tap a square to skate there.
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 {} route {}]; 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 {[llength $sk(route)]} {
        push [lindex $sk(route) 0]; set sk(route) [lrange $sk(route) 1 end]
    }
    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}]
}
# Where a slide from x y ends, as push finds it.
proc slide {x y dir} {
    lassign [dict get {up {0 -1} down {0 1} left {-1 0} right {1 0}} $dir] dx dy
    while 1 {
        set c [tile [expr {$x + $dx}] [expr {$y + $dy}]]
        if {$c eq "#"} break
        incr x $dx; incr y $dy
        if {$c eq "="} break
    }
    list $x $y
}
# A search by breadth: each square where the skater can stop, with the
# fewest pushes that reach it.
proc reach {} {
    global sk
    set from "$sk(x),$sk(y)"
    set way($from) {}
    set todo [list $from]
    while {[llength $todo]} {
        set next {}
        foreach p $todo {
            lassign [split $p ,] x y
            foreach dir {up down left right} {
                set q [join [slide $x $y $dir] ,]
                if {[info exists way($q)]} continue
                set way($q) [concat $way($p) $dir]
                lappend next $q
            }
        }
        set todo $next
    }
    array get way
}
# A dot on each square of reach, so you can see the search.
proc dots {} {
    global T
    .c delete dot
    foreach {q way} [reach] {
        lassign [split $q ,] x y
        set cx [expr {$x * $T + 14}]; set cy [expr {$y * $T + 14}]
        .c create oval [expr {$cx - 2}] [expr {$cy - 2}] [expr {$cx + 2}] [expr {$cy + 2}] -width 0 -fill #3f8fd8 -tags dot
    }
    .c lower dot sk
}
# A press, a move of 16 pixels or more, and a release: a swipe. A press
# and a release with no such move: a tap.
array set ptr {x 0 y 0 swiped 0}
bind .c <ButtonPress-1> { array set ptr {x %x y %y swiped 0} }
bind .c <B1-Motion> {
    set dx [expr {%x - $ptr(x)}]; set dy [expr {%y - $ptr(y)}]
    if {!$ptr(swiped) && hypot($dx, $dy) >= 16 && $sk(t) >= 1} {
        set ptr(swiped) 1
        push [expr {abs($dx) > abs($dy) ? ($dx > 0 ? "right" : "left") : ($dy > 0 ? "down" : "up")}]
    }
}
bind .c <ButtonRelease-1> { if {!$ptr(swiped)} { tap %x %y } }
# A tap on a square in reach: the pushes of its way, one after another.
proc tap {x y} {
    global sk T
    if {$sk(t) < 1} return
    set q "[expr {int($x / $T)}],[expr {int($y / $T)}]"
    array set way [reach]
    if {![info exists way($q)]} return
    set sk(route) $way($q)
    lassign [split $q ,] tx ty
    .c delete aim
    .c create rectangle [expr {$tx * $T + 2}] [expr {$ty * $T + 2}] [expr {$tx * $T + $T - 2}] [expr {$ty * $T + $T - 2}] \
        -outline #ffc93c -width 3 -tags aim
    .c lower aim sk
}
proc tick {} {
    global sk
    after 16 tick
    set moving [expr {$sk(t) < 1 || [llength $sk(route)]}]
    step
    if {$moving && $sk(t) >= 1 && ![llength $sk(route)]} { dots; .c delete aim }
}
dots
tick
Tap and swipe
The pond with blue dots on the squares where the skater can stop, and a gold ring on the square of the tap
Live

The game keeps the pushes of the way in sk(route), and the loop pushes the next one each time the skater stops. A gold ring marks the square of the tap.

A finger is larger than a square

On a phone, a square of the pond is about as small as a fingertip. A tap that is a little off must still work. When the tap is on no square of reach, the game uses the nearest square of reach within one square of the tap (the code). When there is none, a red cross shows for 400 milliseconds.

Skate Pond after a tap on the flag: a gold ring on the flag and the skater on its way there

A tap on the skater uses the bag

A tap on the skater, or on the water or the rock in front of it, uses an item of the bag, as A does (the code). The game selects the item that fits: the pick for a rock, the plank for the water. So a phone needs no button for A. The next step builds the bag.

Next: 4. The bag.