Toys
Three games made with ztk. Each one runs in the page as WebAssembly, and a widget of Tk is part of its play: the tabs of a ttk::notebook are the streets of Lamplighter, a listbox is the bag of Skate Pond, and a ttk::scale is the burner of Tin Moon. Each game is a short Tcl script, and you can read and change its code under the frame.

The code: 575 lines of Tcl
# title: Lamplighter
# widget: ttk::notebook
# page: docs/widgets/panes.md
# pitch: Light the lamps of a hill town before the night comes. Each tab of the notebook is one street of the level.
# order: 1
# Lamplighter. Night falls on a hill town, and you carry the last flame.
# Each level is a walk through 2 or 3 streets, and each street is a tab
# of the ttk::notebook. Walk off the right edge to open the next tab.
# Touch a lamp to light it: each lamp pushes the night back. Light all
# the lamps of a level to open its gate. A click on a tab that you saw
# takes you back to its start.
set W 472; set H 232
array set toy {sfx {} music title save {} calm 0}
proc sfx {name} { lappend ::toy(sfx) $name }
# The palette: dusk and night, mixed by the dark of the level.
array set dusk {sky0 #24204a sky1 #4a3366 sky2 #9a4f6e sky3 #e0885e hill #3a2c52 hill2 #2c2242 ground #241b36 edge #6f5a8f}
array set night {sky0 #080a1c sky1 #10142e sky2 #1a1d3e sky3 #262650 hill #141530 hill2 #0f1026 ground #0c0b1a edge #2e2c52}
set ink(text) #f6ecd6; set ink(dim) #a89cc4; set ink(gold) #ffd27a; set ink(flame) #ff9a3c
set ink(lampoff) #4c4466; set ink(cloak) #4a86a8; set ink(face) #f2d2ac; set ink(water) #2b3f74
proc mix {a b t} {
scan $a #%2x%2x%2x r1 g1 b1
scan $b #%2x%2x%2x r2 g2 b2
set t [expr {max(0.0, min(1.0, $t))}]
format #%02x%02x%02x [expr {int($r1 + ($r2 - $r1) * $t)}] [expr {int($g1 + ($g2 - $g1) * $t)}] [expr {int($b1 + ($b2 - $b1) * $t)}]
}
proc ease {t} { expr {$t < 0.5 ? 4 * $t * $t * $t : 1 - pow(-2 * $t + 2, 3) / 2} }
# The levels. Each street has blocks {x0 top x1} that go to the floor,
# ledges {x0 y x1} that you can jump up through, lamps {x y}, springs
# {x y}, boats {x0 y x1 dx frames} that swing, crumbling boards {x0 y x1},
# and water {x0 x1}. The gate is at the right of the last street.
set levels {
{"Lantern Lane" {
{Gatehouse {{0 196 472}} {} {{236 166}} {} {} {} {}}
{Bakery {{0 196 180} {180 176 300} {300 196 472}} {} {{240 146} {420 166}} {} {} {} {}}
}}
{"Mill Steps" {
{Millrace {{0 196 150} {250 196 472}} {{150 170 250}} {{200 140} {400 166}} {} {} {} {{150 250}}}
{Mill {{0 196 472}} {{120 160 200} {230 124 310} {340 88 420}} {{380 58}} {{60 196}} {} {} {}}
{Granary {{0 196 120} {120 150 240} {240 196 472}} {} {{180 120} {360 166}} {} {} {} {}}
}}
{"The Ford" {
{Bank {{0 196 110} {390 196 472}} {} {{60 166} {430 166}} {} {{140 190 196 120 110}} {} {{110 390}}}
{Shallows {{0 196 80} {420 196 472}} {{196 150 276}} {{236 120}} {} {{90 190 146 90 90} {300 190 356 -80 100}} {} {{80 420}}}
{"Far Bank" {{0 196 472}} {{160 160 230} {270 128 340}} {{300 98} {440 166}} {} {} {} {}}
}}
{"Bell Tower" {
{Square {{0 196 472}} {{90 166 160} {200 140 270}} {{235 110}} {{360 196}} {} {} {}}
{Stairwell {{0 196 90} {380 196 472}} {} {{430 166}} {} {} {{100 176 160} {180 150 240} {260 124 320} {330 150 380}} {{90 380}}}
{Belfry {{0 196 472}} {{60 160 130} {160 124 230} {270 92 340} {370 60 440}} {{405 30} {100 130}} {} {} {} {}}
}}
{"Hilltop" {
{Orchard {{0 196 130} {210 176 300} {380 196 472}} {} {{255 146} {430 166}} {{100 196}} {} {} {{130 210} {300 380}}}
{Ridge {{0 196 70} {410 196 472}} {} {{240 60}} {} {{80 186 160 100 120} {250 160 330 -110 140}} {{190 120 240}} {{70 410}}}
{Summit {{0 196 472}} {{110 166 180} {220 136 290} {330 106 400}} {{365 76} {150 136} {40 166}} {{40 196}} {} {} {}}
}}
}
# The notebook in the colors of the night: the tabs are the street signs.
ztk::style add {
TNotebook { background-color: #15122a; }
TNotebook::client { background-color: #15122a; border: 1px solid #3a3358; border-radius: 6px; }
TNotebook::tab, TNotebook::tab-selected, TNotebook::tab-hover, TNotebook::tab-disabled { color: #8f86b0; padding: 4px 12px; }
TNotebook::tab-selected, TNotebook::tab-hover { color: #f6ecd6; }
TNotebook::tab-disabled { opacity: 0.4; }
TNotebook::indicator { background-color: #ffd27a; }
}
. configure -background #15122a
ttk::notebook .nb
pack .nb -fill both -expand 1 -padx 4 -pady 4
for {set i 0} {$i < 3} {incr i} {
canvas .nb.s$i -width $W -height $H -highlightthickness 0 -background $dusk(sky0)
}
# The state of the run.
array set g {state title level 0 scene 0 dark 0 frames 0 shake 0 freeze 0 pick 0 clear 0 msgT 0 unlocked 1}
array set best {}
array set h {x 30 y 150 vx 0 vy 0 ground 1 coyote 0 buffer 0 face 1 sx 1 sy 1 step 0 spawnS 0 spawnX 30 spawnY 150 ride -1 crumble -1}
array set hold {left 0 right 0 jump 0}
set parts {}
proc toy_restore {data} {
global g best
if {[catch {dict get $data unlocked} u]} return
set g(unlocked) [expr {max(1, min(5, $u))}]
if {![catch {dict get $data best} b]} { array set best $b }
title
}
proc save {} { set ::toy(save) [list unlocked $::g(unlocked) best [array get ::best]] }
proc toy_input {key state} {
global g hold h
set on [expr {$state eq "press"}]
switch $g(state) {
title {
if {!$on} return
switch $key {
left { if {$g(pick) > 0} { incr g(pick) -1; sfx move; title } }
right { if {$g(pick) < $g(unlocked) - 1} { incr g(pick); sfx move; title } }
a - start - up { sfx start; start $g(pick) }
}
}
play {
switch $key {
left - right { set hold($key) $on }
a - up - b {
set hold(jump) $on
if {$on} { set h(buffer) 7 } elseif {$h(vy) < -2} { set h(vy) [expr {$h(vy) * 0.45}] }
}
}
}
clear - night - done {
if {$on && $key in {a start up}} {
if {$g(state) eq "night"} { sfx start; start $g(level) } \
elseif {$g(state) eq "done"} { set g(state) title; title } \
elseif {$g(level) < 4} { sfx start; start [expr {$g(level) + 1}] } \
else { set g(state) done; ending }
}
}
}
}
# Draws one street on its canvas. The lit lamps keep their glow.
proc street {i} {
global levels g W H dusk ink
set c .nb.s$i
$c delete all
lassign [lindex $levels $g(level) 1 $i] name blocks ledges lamps springs boats crumbles water
# The sky in four bands, and stars that come out with the night.
for {set k 0} {$k < 20} {incr k} { $c create rectangle 0 [expr {$k * 9}] $W [expr {$k == 19 ? $H : ($k + 1) * 9}] -width 0 -tags "sky sky$k" }
for {set k 0} {$k < 26} {incr k} {
set x [expr {($k * 131 + $i * 57 + $g(level) * 23) % $W}]; set y [expr {($k * 37 + $i * 11) % 40}]
$c create rectangle $x $y [expr {$x + 2}] [expr {$y + 2}] -width 0 -fill $dusk(sky0) -tags "star"
}
# Two lines of hills, then the roofs of the town.
set pts {}; for {set x 0} {$x <= $W + 40} {incr x 40} { lappend pts $x [expr {150 + 18 * sin(($x + $i * 90 + $g(level) * 40) / 70.0)}] }
$c create polygon 0 $H {*}$pts $W $H -width 0 -tags hill
set pts {}; for {set x 0} {$x <= $W + 30} {incr x 30} { lappend pts $x [expr {170 + 10 * sin(($x * 1.7 + $i * 40) / 50.0)}] }
$c create polygon 0 $H {*}$pts $W $H -width 0 -tags hill2
for {set x 10} {$x < $W} {incr x 70} {
set w [expr {30 + ($x * 7 + $i) % 20}]; set top [expr {150 + ($x * 3 + $g(level) * 5) % 20}]
$c create polygon $x 190 $x $top [expr {$x + $w / 2}] [expr {$top - 14}] [expr {$x + $w}] $top [expr {$x + $w}] 190 -width 0 -tags hill2
$c create rectangle [expr {$x + 8}] [expr {$top + 8}] [expr {$x + 13}] [expr {$top + 14}] -width 0 -fill #e8b86a -tags "window"
}
foreach wt $water { lassign $wt x0 x1; $c create rectangle $x0 206 $x1 $H -width 0 -fill $ink(water) -tags water
for {set x [expr {$x0 + 6}]} {$x < $x1 - 6} {incr x 18} { $c create line $x 210 [expr {$x + 8}] 210 -fill #6a86c8 -width 2 -tags ripple } }
foreach b $blocks { lassign $b x0 y x1
$c create rectangle $x0 $y $x1 $H -width 0 -tags ground
$c create rectangle $x0 $y $x1 [expr {$y + 3}] -width 0 -tags edge
for {set x [expr {$x0 + 7}]} {$x < $x1 - 4} {incr x 23} { $c create line $x $y [expr {$x - 2}] [expr {$y - 4}] $x $y [expr {$x + 3}] [expr {$y - 5}] -fill #7d6aa0 -tags tuft } }
foreach l $ledges { lassign $l x0 y x1
$c create rectangle $x0 $y $x1 [expr {$y + 6}] -width 0 -tags ground
$c create rectangle $x0 $y $x1 [expr {$y + 2}] -width 0 -tags edge }
set n 0
foreach cr $crumbles { lassign $cr x0 y x1
$c create rectangle $x0 $y $x1 [expr {$y + 5}] -width 0 -fill #8a6a4a -tags "crumble cr$n"
for {set x [expr {$x0 + 10}]} {$x < $x1} {incr x 12} { $c create line $x $y $x [expr {$y + 5}] -fill #5a4430 -tags "crumble cr$n" }
incr n }
set n 0
foreach bt $boats { lassign $bt x0 y x1
$c create polygon $x0 $y $x1 $y [expr {$x1 - 6}] [expr {$y + 8}] [expr {$x0 + 6}] [expr {$y + 8}] -fill #a0703c -outline #d09a5a -tags "boat bt$n"
incr n }
foreach s $springs { lassign $s x y
$c create rectangle [expr {$x - 9}] [expr {$y - 5}] [expr {$x + 9}] $y -width 0 -fill #5fb27a -tags spring
$c create rectangle [expr {$x - 9}] [expr {$y - 7}] [expr {$x + 9}] [expr {$y - 5}] -width 0 -fill #a6e6b2 -tags spring }
set n 0
foreach l $lamps { lassign $l x y
foreach r {40 33 27 21 15 10} k {6 5 4 3 2 1} { $c create oval [expr {$x - $r}] [expr {$y - $r}] [expr {$x + $r}] [expr {$y + $r}] -width 0 -state hidden -tags "glow glow$n g$k" }
$c create rectangle [expr {$x - 1}] [expr {$y + 6}] [expr {$x + 1}] [expr {$y + 30}] -width 0 -fill #1a1426 -tags post
$c create polygon [expr {$x - 6}] [expr {$y - 4}] [expr {$x + 6}] [expr {$y - 4}] [expr {$x + 4}] [expr {$y + 6}] [expr {$x - 4}] [expr {$y + 6}] -fill $ink(lampoff) -outline #1a1426 -tags "lamp lamp$n"
$c create polygon [expr {$x - 8}] [expr {$y - 4}] $x [expr {$y - 10}] [expr {$x + 8}] [expr {$y - 4}] -fill #1a1426 -width 0 -tags post
incr n }
if {$i == [llength [lindex $levels $g(level) 1]] - 1} {
# The gate of the level: bars that go up when each lamp is lit.
$c create rectangle 440 120 444 196 -width 0 -fill #1a1426 -tags gatepost
$c create rectangle 466 120 470 196 -width 0 -fill #1a1426 -tags gatepost
for {set x 446} {$x < 466} {incr x 5} { $c create rectangle $x 124 [expr {$x + 2}] 196 -width 0 -fill #8f86b0 -tags bars }
}
# The hero: a cloak, a face, a hat, and the flame on a pole.
$c create rectangle 0 0 1 1 -width 0 -fill $ink(cloak) -tags "hero hcloak"
$c create rectangle 0 0 1 1 -width 0 -fill $ink(face) -tags "hero hface"
$c create rectangle 0 0 1 1 -width 0 -fill #1a1426 -tags "hero hhat"
$c create line 0 0 1 1 -fill #c8b088 -width 2 -tags "hero hpole"
$c create oval 0 0 1 1 -width 0 -fill $ink(flame) -tags "hero hflame"
$c create oval 0 0 1 1 -width 0 -fill $ink(gold) -tags "hero hcore"
for {set k 0} {$k < 20} {incr k} { $c create rectangle -9 -9 -8 -8 -width 0 -fill $ink(gold) -tags "part p$k"; set ::pcolor($k) $ink(gold) }
$c create text 8 6 -anchor nw -fill $ink(text) -font {TkFixedFont 10 bold} -tags hud
$c create rectangle 360 9 464 15 -outline $ink(dim) -width 1 -tags hudbar
$c create rectangle 361 10 463 14 -width 0 -fill $ink(gold) -tags hudfill
$c create text [expr {$W / 2}] 92 -fill $ink(text) -font {TkFixedFont 20 bold} -tags "msg"
$c create text [expr {$W / 2}] 122 -fill $ink(dim) -font {TkFixedFont 10 bold} -tags "msg2"
paint $i
}
# The colors of a street for the dark of the level, from 0 to 1.
proc paint {i} {
global dusk night g
set c .nb.s$i
set t [ease $g(dark)]
# Twenty bands of the sky, mixed between the four colors of the palette.
for {set k 0} {$k < 20} {incr k} {
set u [expr {$k / 19.0 * 3}]; set a [expr {min(2, int($u))}]; set f [expr {$u - $a}]
set top [mix $dusk(sky$a) $dusk(sky[expr {$a + 1}]) $f]
set low [mix $night(sky$a) $night(sky[expr {$a + 1}]) $f]
$c itemconfigure sky$k -fill [mix $top $low $t]
}
foreach k {hill hill2 ground edge} { $c itemconfigure $k -fill [mix $dusk($k) $night($k) $t] }
$c itemconfigure star -fill [mix [mix $dusk(sky0) $night(sky0) $t] #dcd6ff [expr {($t - 0.3) * 1.6}]]
$c configure -background [mix $dusk(sky0) $night(sky0) $t]
# Each glow mixes the gold of the lamp into the sky behind it.
set sky [mix $dusk(sky1) $night(sky1) $t]
foreach k {6 5 4 3 2 1} f {0.07 0.13 0.2 0.29 0.4 0.56} { $c itemconfigure g$k -fill [mix $sky #ffd27a $f] }
}
proc lvl {} { lindex $::levels $::g(level) 1 }
# The parts of the current street, read once when you enter it.
proc loadStreet {} {
foreach what {name blocks ledges lamps springs boats crumbles water} v [lindex [lvl] $::g(scene)] { set ::cur($what) $v }
}
proc data {what} { return $::cur($what) }
proc start {n} {
global g h levels toy lit crumb
array set g [list state play level $n scene 0 dark 0 frames 0 shake 0 freeze 0 clear 0 msgT 120]
array unset lit; array unset crumb
set toy(music) play
set scenes [lvl]
foreach t [.nb tabs] { .nb forget $t }
for {set i 0} {$i < [llength $scenes]} {incr i} {
.nb add .nb.s$i -text [lindex $scenes $i 0] -state [expr {$i ? "disabled" : "normal"}]
street $i
}
tabtext
array set h {x 24 y 150 vx 0 vy 0 ground 0 coyote 0 buffer 0 face 1 sx 1 sy 1 ride -1 crumble -1}
array set h {spawnS 0 spawnX 24 spawnY 150}
.nb select 0
enter 0 24
say [lindex $levels $n 0] "Level [expr {$n + 1}] of 5"
}
# The name of each tab, with a dot for each lamp: lit or not.
proc tabtext {} {
global lit
set i 0
foreach s [lvl] {
set dots ""
for {set k 0} {$k < [llength [lindex $s 3]]} {incr k} { append dots [expr {[info exists lit($i,$k)] ? "●" : "○"}] }
.nb tab $i -text "[lindex $s 0] $dots"
incr i
}
}
proc say {a b} {
set c .nb.s$::g(scene)
$c itemconfigure msg -text $a
$c itemconfigure msg2 -text $b
set ::g(msgT) 150
}
# A click on a tab of a street that you saw takes you to its start.
bind .nb <<NotebookTabChanged>> {
set n [.nb index current]
if {$g(state) eq "play" && $n != $g(scene)} { enter $n 24; set h(y) 120; set h(vy) 0 }
}
proc enter {n x} {
global g h
foreach i {0 1 2} { if {[winfo exists .nb.s$i]} { .nb.s$i itemconfigure hero -state hidden } }
set g(scene) $n; set h(x) $x
loadStreet
.nb.s$n itemconfigure hero -state normal
paint $n
if {[.nb index current] != $n} { .nb select $n }
}
proc burst {x y n color {up 0}} {
global parts
for {set k 0} {$k < $n} {incr k} {
set a [expr {rand() * 6.283}]; set s [expr {0.6 + rand() * 1.8}]
lappend parts [list $x $y [expr {cos($a) * $s}] [expr {sin($a) * $s - $up}] [expr {24 + int(rand() * 18)}] $color]
}
set parts [lrange $parts end-19 end]
}
proc hurt {} {
global h g
sfx hurt
set g(shake) 10
set g(dark) [expr {min(1.0, $g(dark) + 0.05)}]
burst $h(x) $h(y) 12 #ff9a3c 1
if {$h(spawnS) != $g(scene)} { enter $h(spawnS) $h(spawnX) }
set h(x) $h(spawnX); set h(y) $h(spawnY); set h(vx) 0; set h(vy) 0
}
# The solid tops under the feet: blocks, ledges, boats, and boards.
proc floors {} {
global g h crumb
set out {}
foreach b [data blocks] { lappend out [concat $b solid -1] }
foreach l [data ledges] { lappend out [concat $l ledge -1] }
set n 0
foreach bt [data boats] {
lassign $bt x0 y x1 dx per
set off [expr {$dx * sin($g(frames) * 6.283 / $per)}]
lappend out [list [expr {$x0 + $off}] $y [expr {$x1 + $off}] boat $n]
incr n
}
set n 0
foreach cr [data crumbles] {
if {![info exists crumb($g(scene),$n)] || [lindex $crumb($g(scene),$n) 0] < 40} { lappend out [concat $cr crumble $n] }
incr n
}
return $out
}
proc tick {} {
global g
after 16 tick
if {$g(state) eq "title"} { titleTick; return }
if {$g(freeze) > 0} { incr g(freeze) -1; return }
if {$g(state) eq "play"} { step }
draw
}
proc step {} {
global g h hold lit crumb toy levels
incr g(frames)
set c .nb.s$g(scene)
# The night comes in about 100 seconds; each lamp holds it back.
set g(dark) [expr {min(1.0, $g(dark) + 1.0 / 6000)}]
if {$g(frames) % 20 == 0} { paint $g(scene) }
if {$g(dark) >= 1.0} {
set g(state) night; set toy(music) ""; sfx lose
say "Night fell" "Press A to try again"; return
}
# Run: speed up on a press, slow down on a release, less in the air.
set dir [expr {$hold(right) - $hold(left)}]
set acc [expr {$h(ground) ? 0.45 : 0.28}]
if {$dir != 0} {
set h(face) $dir
set h(vx) [expr {max(-2.7, min(2.7, $h(vx) + $dir * $acc))}]
} else {
set f [expr {$h(ground) ? 0.42 : 0.12}]
set h(vx) [expr {abs($h(vx)) <= $f ? 0 : $h(vx) - ($h(vx) > 0 ? $f : -$f)}]
}
# A jump: the press waits 7 frames (the buffer), and the ground
# stays for 6 frames after the edge (coyote time).
if {$h(buffer) > 0} { incr h(buffer) -1 }
if {$h(ground)} { set h(coyote) 6 } elseif {$h(coyote) > 0} { incr h(coyote) -1 }
if {$h(buffer) > 0 && $h(coyote) > 0} {
set h(vy) -6.3; set h(buffer) 0; set h(coyote) 0; set h(ground) 0; set h(ride) -1
set h(sx) 0.7; set h(sy) 1.35; sfx jump
burst $h(x) [expr {$h(y) + 7}] 4 #c8b8e0
}
set h(vy) [expr {min(6.0, $h(vy) + ($h(vy) < 0 && $hold(jump) ? 0.30 : 0.42))}]
# Ride a boat: move with it.
if {$h(ride) >= 0} {
foreach f [floors] { lassign $f x0 y x1 kind n; if {$kind eq "boat" && $n == $h(ride)} { set h(x) [expr {$h(x) + ($x0 - $h(rideX))}]; set h(rideX) $x0 } }
}
set h(x) [expr {$h(x) + $h(vx)}]
# The sides of the blocks.
foreach b [data blocks] {
lassign $b x0 top x1
if {$h(y) + 7 > $top + 2 && $h(x) + 5 > $x0 && $h(x) - 5 < $x1} {
set h(x) [expr {$h(vx) > 0 ? $x0 - 5 : $x1 + 5}]; set h(vx) 0
}
}
set oy $h(y)
set h(y) [expr {$h(y) + $h(vy)}]
set was $h(ground)
set h(ground) 0
if {$h(vy) >= 0} {
foreach f [floors] {
lassign $f x0 y x1 kind n
if {$oy + 7 <= $y + 1 && $h(y) + 7 >= $y && $h(x) + 4 > $x0 && $h(x) - 4 < $x1} {
set h(y) [expr {$y - 7}]
if {!$was && $h(vy) > 2.5} {
set h(sx) 1.35; set h(sy) 0.7; sfx land
burst $h(x) $y [expr {$h(vy) > 5 ? 8 : 4}] #c8b8e0
if {$h(vy) > 5.5} { set g(shake) 4 }
}
set h(vy) 0; set h(ground) 1
set h(ride) [expr {$kind eq "boat" ? $n : -1}]
if {$kind eq "boat"} { set h(rideX) $x0 }
if {$kind eq "crumble" && ![info exists crumb($g(scene),$n)]} { set crumb($g(scene),$n) {0} }
break
}
}
}
if {!$h(ground)} { set h(ride) -1 }
# The boards break 40 frames after a step, and come back after 3 s.
foreach k [array names crumb $g(scene),*] {
set n [lindex [split $k ,] 1]
set t [expr {[lindex $crumb($k) 0] + 1}]
set crumb($k) $t
if {$t == 40} { sfx crash; .nb.s$g(scene) itemconfigure cr$n -state hidden; lassign [lindex [data crumbles] $n] x0 y x1; burst [expr {($x0 + $x1) / 2}] $y 10 #8a6a4a }
if {$t > 220} { unset crumb($k); .nb.s$g(scene) itemconfigure cr$n -state normal }
if {$t < 40 && $t % 6 < 3} { .nb.s$g(scene) move cr$n [expr {$t % 2 ? 1 : -1}] 0 }
}
# A spring sends you high.
foreach s [data springs] {
lassign $s sx sy
if {$h(ground) && abs($h(x) - $sx) < 10 && abs($h(y) + 7 - $sy) < 3} {
set h(vy) -9.6; set h(ground) 0; set h(sx) 0.6; set h(sy) 1.5; sfx pop; burst $sx $sy 6 #a6e6b2 1
}
}
# The lamps.
set k 0
foreach l [data lamps] {
lassign $l lx ly
if {![info exists lit($g(scene),$k)] && abs($h(x) - $lx) < 11 && abs($h(y) - 4 - $ly) < 22} {
set lit($g(scene),$k) 1
$c itemconfigure lamp$k -fill #ffd27a
$c itemconfigure glow$k -state normal
$c raise glow$k window
$c lower glow$k post
set g(dark) [expr {max(0.0, $g(dark) - 0.22)}]
paint $g(scene)
burst $lx $ly 16 #ffd27a 0.5
set g(freeze) 4; set g(shake) 3; sfx light
tabtext
if {[allLit]} { sfx win; say "The gate is open" "Walk on to the right" }
}
incr k
}
# The edges of a street.
set last [expr {[llength [lvl]] - 1}]
if {$h(x) > $::W - 4} {
if {$g(scene) < $last} {
set n [expr {$g(scene) + 1}]
.nb tab $n -state normal
set h(spawnS) $n; set h(spawnX) 20; set h(spawnY) 150
enter $n 8; sfx select
} elseif {[allLit]} { finish; return } else {
set h(x) [expr {$::W - 4}]
}
}
if {$g(scene) == $last && ![allLit] && $h(x) > 436} { set h(x) 436; set h(vx) 0 }
if {$h(x) < 5} {
if {$g(scene) > 0} { enter [expr {$g(scene) - 1}] [expr {$::W - 10}] } else { set h(x) 5 }
}
if {$h(y) > $::H + 10} { hurt }
# Steps on the ground.
if {$h(ground) && abs($h(vx)) > 1 && [incr h(step)] % 14 == 0} { sfx step }
}
proc allLit {} {
global lit g
set i 0
foreach s [lvl] { for {set k 0} {$k < [llength [lindex $s 3]]} {incr k} { if {![info exists lit($i,$k)]} { return 0 } }; incr i }
return 1
}
proc finish {} {
global g best toy
set secs [expr {$g(frames) / 60.0}]
set n $g(level)
set new [expr {![info exists best($n)] || $secs < $best($n)}]
if {$new} { set best($n) [format %.1f $secs] }
set g(unlocked) [expr {max($g(unlocked), min(5, $n + 2))}]
save
set g(state) clear; set toy(music) ""; sfx win
set g(shake) 0
say "[lindex $::levels $n 0] is lit" "[format %.1f $secs] s[expr {$new ? " new best" : " best $best($n) s"}] A: next"
}
proc ending {} {
set c .nb.s$::g(scene)
say "The town is lit" "Thank you for the light. A: title"
set ::toy(music) title
}
# Moves the items of the street to the state of the frame.
proc draw {} {
global g h parts ink
set c .nb.s$g(scene)
# The squash and the stretch go back to 1.
set h(sx) [expr {$h(sx) + (1 - $h(sx)) * 0.25}]
set h(sy) [expr {$h(sy) + (1 - $h(sy)) * 0.25}]
set ox 0; set oy 0
if {$g(shake) > 0 && !$::toy(calm)} {
set ox [expr {int((rand() - 0.5) * $g(shake))}]; set oy [expr {int((rand() - 0.5) * $g(shake))}]
}
if {$g(shake) > 0} { incr g(shake) -1 }
set x [expr {$h(x) + $ox}]; set y [expr {$h(y) + 7 + $oy}]
set w [expr {6.5 * $h(sx)}]; set hh [expr {19 * $h(sy)}]
set bob [expr {$h(ground) && abs($h(vx)) > 0.5 ? sin($g(frames) * 0.5) : 0}]
$c coords hcloak [expr {$x - $w}] [expr {$y - $hh * 0.72}] [expr {$x + $w}] $y
$c coords hface [expr {$x - $w * 0.7}] [expr {$y - $hh}] [expr {$x + $w * 0.7}] [expr {$y - $hh * 0.68 + $bob}]
$c coords hhat [expr {$x - $w - 1}] [expr {$y - $hh - 2}] [expr {$x + $w + 1}] [expr {$y - $hh}]
set px [expr {$x + $h(face) * 7}]; set py [expr {$y - $hh - 6}]
$c coords hpole [expr {$x + $h(face) * 3}] [expr {$y - 4}] $px $py
set fl [expr {2.4 + sin($g(frames) * 0.4) * 0.8}]
$c coords hflame [expr {$px - $fl - 1}] [expr {$py - $fl - 3}] [expr {$px + $fl + 1}] [expr {$py + $fl}]
$c coords hcore [expr {$px - 1.2}] [expr {$py - 2}] [expr {$px + 1.2}] [expr {$py + 1}]
if {$g(frames) % 5 == 0 && $g(state) eq "play"} { lappend parts [list $px [expr {$py - 2}] [expr {(rand() - 0.5) * 0.4}] -0.6 30 #ffb35c] }
# The particles: embers, dust, and sparks.
set keep {}
set k 0
foreach p $parts {
lassign $p px py vx vy life color
if {[incr life -1] <= 0 || $k >= 20} continue
set px [expr {$px + $vx}]; set py [expr {$py + $vy}]; set vy [expr {$vy + 0.05}]
set s [expr {$life > 12 ? 2 : 1}]
$c coords p$k $px $py [expr {$px + $s}] [expr {$py + $s}]
if {$::pcolor($k) ne $color} { $c itemconfigure p$k -fill $color; set ::pcolor($k) $color }
lappend keep [list $px $py $vx $vy $life $color]
incr k
}
for {} {$k < 20} {incr k} { $c coords p$k -9 -9 -8 -8 }
set parts $keep
# The boats swing.
set n 0
foreach bt [data boats] {
lassign $bt x0 y x1 dx per
set off [expr {$dx * sin($g(frames) * 6.283 / $per)}]
$c coords bt$n [expr {$x0 + $off}] $y [expr {$x1 + $off}] $y [expr {$x1 + $off - 6}] [expr {$y + 8}] [expr {$x0 + $off + 6}] [expr {$y + 8}]
incr n
}
if {[allLit]} { foreach id [$c find withtag bars] { lassign [$c coords $id] a b cc d; if {$b > 60} { $c coords $id $a [expr {$b - 2}] $cc [expr {$d - 2}] } } }
if {$g(state) eq "title"} return
$c itemconfigure hud -text "[lindex $::levels $g(level) 0] [format %02d:%02d [expr {$g(frames) / 3600}] [expr {($g(frames) / 60) % 60}]]"
set left [expr {1 - $g(dark)}]
$c coords hudfill 361 10 [expr {361 + 102 * $left}] 14
$c itemconfigure hudfill -fill [expr {$left < 0.25 && $g(frames) % 30 < 15 && !$::toy(calm) ? "#ff6a5c" : "#ffd27a"}]
if {$g(msgT) > 0 && $g(state) eq "play" && [incr g(msgT) -1] == 0} { say "" "" }
}
# The title screen: the first street of the picked level, with its name.
proc title {} {
global g levels toy ink best
set g(state) title
set toy(music) title
foreach t [.nb tabs] { .nb forget $t }
set g(level) $g(pick); set g(scene) 0; set g(dark) 0.15
loadStreet
.nb add .nb.s0 -text "Lamplighter"
street 0
set c .nb.s0
$c itemconfigure hero -state normal
$c itemconfigure hud -text ""
$c itemconfigure hudbar -state hidden; $c itemconfigure hudfill -state hidden
$c create text 236 70 -text "LAMPLIGHTER" -fill $ink(gold) -font {TkFixedFont 30 bold} -tags title
set n $g(pick)
set b [expr {[info exists best($n)] ? "best $best($n) s" : "not lit yet"}]
$c create text 236 104 -text "◀ [expr {$n + 1}]. [lindex $levels $n 0] ▶" -fill $ink(text) -font {TkFixedFont 12 bold} -tags title
$c create text 236 124 -text $b -fill $ink(dim) -font {TkFixedFont 10} -tags title
$c create text 236 222 -text "A or Enter: start Left and Right: level" -fill $ink(dim) -font {TkFixedFont 9 bold} -tags "title blink"
for {set k 1} {$k < 5} {incr k} {
set fill [expr {$k < $g(unlocked) ? $ink(gold) : "#4c4466"}]
$c create oval [expr {206 + $k * 12}] 136 [expr {212 + $k * 12}] 142 -width 0 -fill $fill -tags title
}
$c create oval 206 136 212 142 -width 0 -fill $ink(gold) -tags title
array set ::h {x 60 y 189 vx 0.8 vy 0 face 1 sx 1 sy 1 ground 1}
# The lamp of the title is lit.
$c itemconfigure lamp0 -fill $ink(gold)
$c itemconfigure glow0 -state normal
$c raise glow0 window
$c lower glow0 post
}
proc titleTick {} {
global g h
incr g(frames)
set h(x) [expr {$h(x) + 0.8}]
if {$h(x) > 500} { set h(x) -20 }
set h(vx) 0.8; set h(ground) 1
if {$g(frames) % 40 == 0} { .nb.s0 itemconfigure blink -state [expr {$g(frames) % 80 ? "hidden" : "normal"}] }
draw
}
title
tick
Lamplighter. Light the lamps of a hill town before the night comes. Each tab of the notebook is one street of the level.
The widget: ttk::notebook. The design of Lamplighter.
Skate Pond. A sliding puzzle on a frozen pond. The listbox is your bag: pick up a plank or a pick on the ice, select it, and use it.
The widget: listbox. The design of Skate Pond.
Tin Moon. Fly a tin balloon over a small moon. You steer only with height: each layer of the sky has its own wind, and the ttk::scale is your burner.
The widget: ttk::scale. The design of Tin Moon.
# title: Lamplighter
# widget: ttk::notebook
# page: docs/widgets/panes.md
# pitch: Light the lamps of a hill town before the night comes. Each tab of the notebook is one street of the level.
# order: 1
# Lamplighter. Night falls on a hill town, and you carry the last flame.
# Each level is a walk through 2 or 3 streets, and each street is a tab
# of the ttk::notebook. Walk off the right edge to open the next tab.
# Touch a lamp to light it: each lamp pushes the night back. Light all
# the lamps of a level to open its gate. A click on a tab that you saw
# takes you back to its start.
set W 472; set H 232
array set toy {sfx {} music title save {} calm 0}
proc sfx {name} { lappend ::toy(sfx) $name }
# The palette: dusk and night, mixed by the dark of the level.
array set dusk {sky0 #24204a sky1 #4a3366 sky2 #9a4f6e sky3 #e0885e hill #3a2c52 hill2 #2c2242 ground #241b36 edge #6f5a8f}
array set night {sky0 #080a1c sky1 #10142e sky2 #1a1d3e sky3 #262650 hill #141530 hill2 #0f1026 ground #0c0b1a edge #2e2c52}
set ink(text) #f6ecd6; set ink(dim) #a89cc4; set ink(gold) #ffd27a; set ink(flame) #ff9a3c
set ink(lampoff) #4c4466; set ink(cloak) #4a86a8; set ink(face) #f2d2ac; set ink(water) #2b3f74
proc mix {a b t} {
scan $a #%2x%2x%2x r1 g1 b1
scan $b #%2x%2x%2x r2 g2 b2
set t [expr {max(0.0, min(1.0, $t))}]
format #%02x%02x%02x [expr {int($r1 + ($r2 - $r1) * $t)}] [expr {int($g1 + ($g2 - $g1) * $t)}] [expr {int($b1 + ($b2 - $b1) * $t)}]
}
proc ease {t} { expr {$t < 0.5 ? 4 * $t * $t * $t : 1 - pow(-2 * $t + 2, 3) / 2} }
# The levels. Each street has blocks {x0 top x1} that go to the floor,
# ledges {x0 y x1} that you can jump up through, lamps {x y}, springs
# {x y}, boats {x0 y x1 dx frames} that swing, crumbling boards {x0 y x1},
# and water {x0 x1}. The gate is at the right of the last street.
set levels {
{"Lantern Lane" {
{Gatehouse {{0 196 472}} {} {{236 166}} {} {} {} {}}
{Bakery {{0 196 180} {180 176 300} {300 196 472}} {} {{240 146} {420 166}} {} {} {} {}}
}}
{"Mill Steps" {
{Millrace {{0 196 150} {250 196 472}} {{150 170 250}} {{200 140} {400 166}} {} {} {} {{150 250}}}
{Mill {{0 196 472}} {{120 160 200} {230 124 310} {340 88 420}} {{380 58}} {{60 196}} {} {} {}}
{Granary {{0 196 120} {120 150 240} {240 196 472}} {} {{180 120} {360 166}} {} {} {} {}}
}}
{"The Ford" {
{Bank {{0 196 110} {390 196 472}} {} {{60 166} {430 166}} {} {{140 190 196 120 110}} {} {{110 390}}}
{Shallows {{0 196 80} {420 196 472}} {{196 150 276}} {{236 120}} {} {{90 190 146 90 90} {300 190 356 -80 100}} {} {{80 420}}}
{"Far Bank" {{0 196 472}} {{160 160 230} {270 128 340}} {{300 98} {440 166}} {} {} {} {}}
}}
{"Bell Tower" {
{Square {{0 196 472}} {{90 166 160} {200 140 270}} {{235 110}} {{360 196}} {} {} {}}
{Stairwell {{0 196 90} {380 196 472}} {} {{430 166}} {} {} {{100 176 160} {180 150 240} {260 124 320} {330 150 380}} {{90 380}}}
{Belfry {{0 196 472}} {{60 160 130} {160 124 230} {270 92 340} {370 60 440}} {{405 30} {100 130}} {} {} {} {}}
}}
{"Hilltop" {
{Orchard {{0 196 130} {210 176 300} {380 196 472}} {} {{255 146} {430 166}} {{100 196}} {} {} {{130 210} {300 380}}}
{Ridge {{0 196 70} {410 196 472}} {} {{240 60}} {} {{80 186 160 100 120} {250 160 330 -110 140}} {{190 120 240}} {{70 410}}}
{Summit {{0 196 472}} {{110 166 180} {220 136 290} {330 106 400}} {{365 76} {150 136} {40 166}} {{40 196}} {} {} {}}
}}
}
# The notebook in the colors of the night: the tabs are the street signs.
ztk::style add {
TNotebook { background-color: #15122a; }
TNotebook::client { background-color: #15122a; border: 1px solid #3a3358; border-radius: 6px; }
TNotebook::tab, TNotebook::tab-selected, TNotebook::tab-hover, TNotebook::tab-disabled { color: #8f86b0; padding: 4px 12px; }
TNotebook::tab-selected, TNotebook::tab-hover { color: #f6ecd6; }
TNotebook::tab-disabled { opacity: 0.4; }
TNotebook::indicator { background-color: #ffd27a; }
}
. configure -background #15122a
ttk::notebook .nb
pack .nb -fill both -expand 1 -padx 4 -pady 4
for {set i 0} {$i < 3} {incr i} {
canvas .nb.s$i -width $W -height $H -highlightthickness 0 -background $dusk(sky0)
}
# The state of the run.
array set g {state title level 0 scene 0 dark 0 frames 0 shake 0 freeze 0 pick 0 clear 0 msgT 0 unlocked 1}
array set best {}
array set h {x 30 y 150 vx 0 vy 0 ground 1 coyote 0 buffer 0 face 1 sx 1 sy 1 step 0 spawnS 0 spawnX 30 spawnY 150 ride -1 crumble -1}
array set hold {left 0 right 0 jump 0}
set parts {}
proc toy_restore {data} {
global g best
if {[catch {dict get $data unlocked} u]} return
set g(unlocked) [expr {max(1, min(5, $u))}]
if {![catch {dict get $data best} b]} { array set best $b }
title
}
proc save {} { set ::toy(save) [list unlocked $::g(unlocked) best [array get ::best]] }
proc toy_input {key state} {
global g hold h
set on [expr {$state eq "press"}]
switch $g(state) {
title {
if {!$on} return
switch $key {
left { if {$g(pick) > 0} { incr g(pick) -1; sfx move; title } }
right { if {$g(pick) < $g(unlocked) - 1} { incr g(pick); sfx move; title } }
a - start - up { sfx start; start $g(pick) }
}
}
play {
switch $key {
left - right { set hold($key) $on }
a - up - b {
set hold(jump) $on
if {$on} { set h(buffer) 7 } elseif {$h(vy) < -2} { set h(vy) [expr {$h(vy) * 0.45}] }
}
}
}
clear - night - done {
if {$on && $key in {a start up}} {
if {$g(state) eq "night"} { sfx start; start $g(level) } \
elseif {$g(state) eq "done"} { set g(state) title; title } \
elseif {$g(level) < 4} { sfx start; start [expr {$g(level) + 1}] } \
else { set g(state) done; ending }
}
}
}
}
# Draws one street on its canvas. The lit lamps keep their glow.
proc street {i} {
global levels g W H dusk ink
set c .nb.s$i
$c delete all
lassign [lindex $levels $g(level) 1 $i] name blocks ledges lamps springs boats crumbles water
# The sky in four bands, and stars that come out with the night.
for {set k 0} {$k < 20} {incr k} { $c create rectangle 0 [expr {$k * 9}] $W [expr {$k == 19 ? $H : ($k + 1) * 9}] -width 0 -tags "sky sky$k" }
for {set k 0} {$k < 26} {incr k} {
set x [expr {($k * 131 + $i * 57 + $g(level) * 23) % $W}]; set y [expr {($k * 37 + $i * 11) % 40}]
$c create rectangle $x $y [expr {$x + 2}] [expr {$y + 2}] -width 0 -fill $dusk(sky0) -tags "star"
}
# Two lines of hills, then the roofs of the town.
set pts {}; for {set x 0} {$x <= $W + 40} {incr x 40} { lappend pts $x [expr {150 + 18 * sin(($x + $i * 90 + $g(level) * 40) / 70.0)}] }
$c create polygon 0 $H {*}$pts $W $H -width 0 -tags hill
set pts {}; for {set x 0} {$x <= $W + 30} {incr x 30} { lappend pts $x [expr {170 + 10 * sin(($x * 1.7 + $i * 40) / 50.0)}] }
$c create polygon 0 $H {*}$pts $W $H -width 0 -tags hill2
for {set x 10} {$x < $W} {incr x 70} {
set w [expr {30 + ($x * 7 + $i) % 20}]; set top [expr {150 + ($x * 3 + $g(level) * 5) % 20}]
$c create polygon $x 190 $x $top [expr {$x + $w / 2}] [expr {$top - 14}] [expr {$x + $w}] $top [expr {$x + $w}] 190 -width 0 -tags hill2
$c create rectangle [expr {$x + 8}] [expr {$top + 8}] [expr {$x + 13}] [expr {$top + 14}] -width 0 -fill #e8b86a -tags "window"
}
foreach wt $water { lassign $wt x0 x1; $c create rectangle $x0 206 $x1 $H -width 0 -fill $ink(water) -tags water
for {set x [expr {$x0 + 6}]} {$x < $x1 - 6} {incr x 18} { $c create line $x 210 [expr {$x + 8}] 210 -fill #6a86c8 -width 2 -tags ripple } }
foreach b $blocks { lassign $b x0 y x1
$c create rectangle $x0 $y $x1 $H -width 0 -tags ground
$c create rectangle $x0 $y $x1 [expr {$y + 3}] -width 0 -tags edge
for {set x [expr {$x0 + 7}]} {$x < $x1 - 4} {incr x 23} { $c create line $x $y [expr {$x - 2}] [expr {$y - 4}] $x $y [expr {$x + 3}] [expr {$y - 5}] -fill #7d6aa0 -tags tuft } }
foreach l $ledges { lassign $l x0 y x1
$c create rectangle $x0 $y $x1 [expr {$y + 6}] -width 0 -tags ground
$c create rectangle $x0 $y $x1 [expr {$y + 2}] -width 0 -tags edge }
set n 0
foreach cr $crumbles { lassign $cr x0 y x1
$c create rectangle $x0 $y $x1 [expr {$y + 5}] -width 0 -fill #8a6a4a -tags "crumble cr$n"
for {set x [expr {$x0 + 10}]} {$x < $x1} {incr x 12} { $c create line $x $y $x [expr {$y + 5}] -fill #5a4430 -tags "crumble cr$n" }
incr n }
set n 0
foreach bt $boats { lassign $bt x0 y x1
$c create polygon $x0 $y $x1 $y [expr {$x1 - 6}] [expr {$y + 8}] [expr {$x0 + 6}] [expr {$y + 8}] -fill #a0703c -outline #d09a5a -tags "boat bt$n"
incr n }
foreach s $springs { lassign $s x y
$c create rectangle [expr {$x - 9}] [expr {$y - 5}] [expr {$x + 9}] $y -width 0 -fill #5fb27a -tags spring
$c create rectangle [expr {$x - 9}] [expr {$y - 7}] [expr {$x + 9}] [expr {$y - 5}] -width 0 -fill #a6e6b2 -tags spring }
set n 0
foreach l $lamps { lassign $l x y
foreach r {40 33 27 21 15 10} k {6 5 4 3 2 1} { $c create oval [expr {$x - $r}] [expr {$y - $r}] [expr {$x + $r}] [expr {$y + $r}] -width 0 -state hidden -tags "glow glow$n g$k" }
$c create rectangle [expr {$x - 1}] [expr {$y + 6}] [expr {$x + 1}] [expr {$y + 30}] -width 0 -fill #1a1426 -tags post
$c create polygon [expr {$x - 6}] [expr {$y - 4}] [expr {$x + 6}] [expr {$y - 4}] [expr {$x + 4}] [expr {$y + 6}] [expr {$x - 4}] [expr {$y + 6}] -fill $ink(lampoff) -outline #1a1426 -tags "lamp lamp$n"
$c create polygon [expr {$x - 8}] [expr {$y - 4}] $x [expr {$y - 10}] [expr {$x + 8}] [expr {$y - 4}] -fill #1a1426 -width 0 -tags post
incr n }
if {$i == [llength [lindex $levels $g(level) 1]] - 1} {
# The gate of the level: bars that go up when each lamp is lit.
$c create rectangle 440 120 444 196 -width 0 -fill #1a1426 -tags gatepost
$c create rectangle 466 120 470 196 -width 0 -fill #1a1426 -tags gatepost
for {set x 446} {$x < 466} {incr x 5} { $c create rectangle $x 124 [expr {$x + 2}] 196 -width 0 -fill #8f86b0 -tags bars }
}
# The hero: a cloak, a face, a hat, and the flame on a pole.
$c create rectangle 0 0 1 1 -width 0 -fill $ink(cloak) -tags "hero hcloak"
$c create rectangle 0 0 1 1 -width 0 -fill $ink(face) -tags "hero hface"
$c create rectangle 0 0 1 1 -width 0 -fill #1a1426 -tags "hero hhat"
$c create line 0 0 1 1 -fill #c8b088 -width 2 -tags "hero hpole"
$c create oval 0 0 1 1 -width 0 -fill $ink(flame) -tags "hero hflame"
$c create oval 0 0 1 1 -width 0 -fill $ink(gold) -tags "hero hcore"
for {set k 0} {$k < 20} {incr k} { $c create rectangle -9 -9 -8 -8 -width 0 -fill $ink(gold) -tags "part p$k"; set ::pcolor($k) $ink(gold) }
$c create text 8 6 -anchor nw -fill $ink(text) -font {TkFixedFont 10 bold} -tags hud
$c create rectangle 360 9 464 15 -outline $ink(dim) -width 1 -tags hudbar
$c create rectangle 361 10 463 14 -width 0 -fill $ink(gold) -tags hudfill
$c create text [expr {$W / 2}] 92 -fill $ink(text) -font {TkFixedFont 20 bold} -tags "msg"
$c create text [expr {$W / 2}] 122 -fill $ink(dim) -font {TkFixedFont 10 bold} -tags "msg2"
paint $i
}
# The colors of a street for the dark of the level, from 0 to 1.
proc paint {i} {
global dusk night g
set c .nb.s$i
set t [ease $g(dark)]
# Twenty bands of the sky, mixed between the four colors of the palette.
for {set k 0} {$k < 20} {incr k} {
set u [expr {$k / 19.0 * 3}]; set a [expr {min(2, int($u))}]; set f [expr {$u - $a}]
set top [mix $dusk(sky$a) $dusk(sky[expr {$a + 1}]) $f]
set low [mix $night(sky$a) $night(sky[expr {$a + 1}]) $f]
$c itemconfigure sky$k -fill [mix $top $low $t]
}
foreach k {hill hill2 ground edge} { $c itemconfigure $k -fill [mix $dusk($k) $night($k) $t] }
$c itemconfigure star -fill [mix [mix $dusk(sky0) $night(sky0) $t] #dcd6ff [expr {($t - 0.3) * 1.6}]]
$c configure -background [mix $dusk(sky0) $night(sky0) $t]
# Each glow mixes the gold of the lamp into the sky behind it.
set sky [mix $dusk(sky1) $night(sky1) $t]
foreach k {6 5 4 3 2 1} f {0.07 0.13 0.2 0.29 0.4 0.56} { $c itemconfigure g$k -fill [mix $sky #ffd27a $f] }
}
proc lvl {} { lindex $::levels $::g(level) 1 }
# The parts of the current street, read once when you enter it.
proc loadStreet {} {
foreach what {name blocks ledges lamps springs boats crumbles water} v [lindex [lvl] $::g(scene)] { set ::cur($what) $v }
}
proc data {what} { return $::cur($what) }
proc start {n} {
global g h levels toy lit crumb
array set g [list state play level $n scene 0 dark 0 frames 0 shake 0 freeze 0 clear 0 msgT 120]
array unset lit; array unset crumb
set toy(music) play
set scenes [lvl]
foreach t [.nb tabs] { .nb forget $t }
for {set i 0} {$i < [llength $scenes]} {incr i} {
.nb add .nb.s$i -text [lindex $scenes $i 0] -state [expr {$i ? "disabled" : "normal"}]
street $i
}
tabtext
array set h {x 24 y 150 vx 0 vy 0 ground 0 coyote 0 buffer 0 face 1 sx 1 sy 1 ride -1 crumble -1}
array set h {spawnS 0 spawnX 24 spawnY 150}
.nb select 0
enter 0 24
say [lindex $levels $n 0] "Level [expr {$n + 1}] of 5"
}
# The name of each tab, with a dot for each lamp: lit or not.
proc tabtext {} {
global lit
set i 0
foreach s [lvl] {
set dots ""
for {set k 0} {$k < [llength [lindex $s 3]]} {incr k} { append dots [expr {[info exists lit($i,$k)] ? "●" : "○"}] }
.nb tab $i -text "[lindex $s 0] $dots"
incr i
}
}
proc say {a b} {
set c .nb.s$::g(scene)
$c itemconfigure msg -text $a
$c itemconfigure msg2 -text $b
set ::g(msgT) 150
}
# A click on a tab of a street that you saw takes you to its start.
bind .nb <<NotebookTabChanged>> {
set n [.nb index current]
if {$g(state) eq "play" && $n != $g(scene)} { enter $n 24; set h(y) 120; set h(vy) 0 }
}
proc enter {n x} {
global g h
foreach i {0 1 2} { if {[winfo exists .nb.s$i]} { .nb.s$i itemconfigure hero -state hidden } }
set g(scene) $n; set h(x) $x
loadStreet
.nb.s$n itemconfigure hero -state normal
paint $n
if {[.nb index current] != $n} { .nb select $n }
}
proc burst {x y n color {up 0}} {
global parts
for {set k 0} {$k < $n} {incr k} {
set a [expr {rand() * 6.283}]; set s [expr {0.6 + rand() * 1.8}]
lappend parts [list $x $y [expr {cos($a) * $s}] [expr {sin($a) * $s - $up}] [expr {24 + int(rand() * 18)}] $color]
}
set parts [lrange $parts end-19 end]
}
proc hurt {} {
global h g
sfx hurt
set g(shake) 10
set g(dark) [expr {min(1.0, $g(dark) + 0.05)}]
burst $h(x) $h(y) 12 #ff9a3c 1
if {$h(spawnS) != $g(scene)} { enter $h(spawnS) $h(spawnX) }
set h(x) $h(spawnX); set h(y) $h(spawnY); set h(vx) 0; set h(vy) 0
}
# The solid tops under the feet: blocks, ledges, boats, and boards.
proc floors {} {
global g h crumb
set out {}
foreach b [data blocks] { lappend out [concat $b solid -1] }
foreach l [data ledges] { lappend out [concat $l ledge -1] }
set n 0
foreach bt [data boats] {
lassign $bt x0 y x1 dx per
set off [expr {$dx * sin($g(frames) * 6.283 / $per)}]
lappend out [list [expr {$x0 + $off}] $y [expr {$x1 + $off}] boat $n]
incr n
}
set n 0
foreach cr [data crumbles] {
if {![info exists crumb($g(scene),$n)] || [lindex $crumb($g(scene),$n) 0] < 40} { lappend out [concat $cr crumble $n] }
incr n
}
return $out
}
proc tick {} {
global g
after 16 tick
if {$g(state) eq "title"} { titleTick; return }
if {$g(freeze) > 0} { incr g(freeze) -1; return }
if {$g(state) eq "play"} { step }
draw
}
proc step {} {
global g h hold lit crumb toy levels
incr g(frames)
set c .nb.s$g(scene)
# The night comes in about 100 seconds; each lamp holds it back.
set g(dark) [expr {min(1.0, $g(dark) + 1.0 / 6000)}]
if {$g(frames) % 20 == 0} { paint $g(scene) }
if {$g(dark) >= 1.0} {
set g(state) night; set toy(music) ""; sfx lose
say "Night fell" "Press A to try again"; return
}
# Run: speed up on a press, slow down on a release, less in the air.
set dir [expr {$hold(right) - $hold(left)}]
set acc [expr {$h(ground) ? 0.45 : 0.28}]
if {$dir != 0} {
set h(face) $dir
set h(vx) [expr {max(-2.7, min(2.7, $h(vx) + $dir * $acc))}]
} else {
set f [expr {$h(ground) ? 0.42 : 0.12}]
set h(vx) [expr {abs($h(vx)) <= $f ? 0 : $h(vx) - ($h(vx) > 0 ? $f : -$f)}]
}
# A jump: the press waits 7 frames (the buffer), and the ground
# stays for 6 frames after the edge (coyote time).
if {$h(buffer) > 0} { incr h(buffer) -1 }
if {$h(ground)} { set h(coyote) 6 } elseif {$h(coyote) > 0} { incr h(coyote) -1 }
if {$h(buffer) > 0 && $h(coyote) > 0} {
set h(vy) -6.3; set h(buffer) 0; set h(coyote) 0; set h(ground) 0; set h(ride) -1
set h(sx) 0.7; set h(sy) 1.35; sfx jump
burst $h(x) [expr {$h(y) + 7}] 4 #c8b8e0
}
set h(vy) [expr {min(6.0, $h(vy) + ($h(vy) < 0 && $hold(jump) ? 0.30 : 0.42))}]
# Ride a boat: move with it.
if {$h(ride) >= 0} {
foreach f [floors] { lassign $f x0 y x1 kind n; if {$kind eq "boat" && $n == $h(ride)} { set h(x) [expr {$h(x) + ($x0 - $h(rideX))}]; set h(rideX) $x0 } }
}
set h(x) [expr {$h(x) + $h(vx)}]
# The sides of the blocks.
foreach b [data blocks] {
lassign $b x0 top x1
if {$h(y) + 7 > $top + 2 && $h(x) + 5 > $x0 && $h(x) - 5 < $x1} {
set h(x) [expr {$h(vx) > 0 ? $x0 - 5 : $x1 + 5}]; set h(vx) 0
}
}
set oy $h(y)
set h(y) [expr {$h(y) + $h(vy)}]
set was $h(ground)
set h(ground) 0
if {$h(vy) >= 0} {
foreach f [floors] {
lassign $f x0 y x1 kind n
if {$oy + 7 <= $y + 1 && $h(y) + 7 >= $y && $h(x) + 4 > $x0 && $h(x) - 4 < $x1} {
set h(y) [expr {$y - 7}]
if {!$was && $h(vy) > 2.5} {
set h(sx) 1.35; set h(sy) 0.7; sfx land
burst $h(x) $y [expr {$h(vy) > 5 ? 8 : 4}] #c8b8e0
if {$h(vy) > 5.5} { set g(shake) 4 }
}
set h(vy) 0; set h(ground) 1
set h(ride) [expr {$kind eq "boat" ? $n : -1}]
if {$kind eq "boat"} { set h(rideX) $x0 }
if {$kind eq "crumble" && ![info exists crumb($g(scene),$n)]} { set crumb($g(scene),$n) {0} }
break
}
}
}
if {!$h(ground)} { set h(ride) -1 }
# The boards break 40 frames after a step, and come back after 3 s.
foreach k [array names crumb $g(scene),*] {
set n [lindex [split $k ,] 1]
set t [expr {[lindex $crumb($k) 0] + 1}]
set crumb($k) $t
if {$t == 40} { sfx crash; .nb.s$g(scene) itemconfigure cr$n -state hidden; lassign [lindex [data crumbles] $n] x0 y x1; burst [expr {($x0 + $x1) / 2}] $y 10 #8a6a4a }
if {$t > 220} { unset crumb($k); .nb.s$g(scene) itemconfigure cr$n -state normal }
if {$t < 40 && $t % 6 < 3} { .nb.s$g(scene) move cr$n [expr {$t % 2 ? 1 : -1}] 0 }
}
# A spring sends you high.
foreach s [data springs] {
lassign $s sx sy
if {$h(ground) && abs($h(x) - $sx) < 10 && abs($h(y) + 7 - $sy) < 3} {
set h(vy) -9.6; set h(ground) 0; set h(sx) 0.6; set h(sy) 1.5; sfx pop; burst $sx $sy 6 #a6e6b2 1
}
}
# The lamps.
set k 0
foreach l [data lamps] {
lassign $l lx ly
if {![info exists lit($g(scene),$k)] && abs($h(x) - $lx) < 11 && abs($h(y) - 4 - $ly) < 22} {
set lit($g(scene),$k) 1
$c itemconfigure lamp$k -fill #ffd27a
$c itemconfigure glow$k -state normal
$c raise glow$k window
$c lower glow$k post
set g(dark) [expr {max(0.0, $g(dark) - 0.22)}]
paint $g(scene)
burst $lx $ly 16 #ffd27a 0.5
set g(freeze) 4; set g(shake) 3; sfx light
tabtext
if {[allLit]} { sfx win; say "The gate is open" "Walk on to the right" }
}
incr k
}
# The edges of a street.
set last [expr {[llength [lvl]] - 1}]
if {$h(x) > $::W - 4} {
if {$g(scene) < $last} {
set n [expr {$g(scene) + 1}]
.nb tab $n -state normal
set h(spawnS) $n; set h(spawnX) 20; set h(spawnY) 150
enter $n 8; sfx select
} elseif {[allLit]} { finish; return } else {
set h(x) [expr {$::W - 4}]
}
}
if {$g(scene) == $last && ![allLit] && $h(x) > 436} { set h(x) 436; set h(vx) 0 }
if {$h(x) < 5} {
if {$g(scene) > 0} { enter [expr {$g(scene) - 1}] [expr {$::W - 10}] } else { set h(x) 5 }
}
if {$h(y) > $::H + 10} { hurt }
# Steps on the ground.
if {$h(ground) && abs($h(vx)) > 1 && [incr h(step)] % 14 == 0} { sfx step }
}
proc allLit {} {
global lit g
set i 0
foreach s [lvl] { for {set k 0} {$k < [llength [lindex $s 3]]} {incr k} { if {![info exists lit($i,$k)]} { return 0 } }; incr i }
return 1
}
proc finish {} {
global g best toy
set secs [expr {$g(frames) / 60.0}]
set n $g(level)
set new [expr {![info exists best($n)] || $secs < $best($n)}]
if {$new} { set best($n) [format %.1f $secs] }
set g(unlocked) [expr {max($g(unlocked), min(5, $n + 2))}]
save
set g(state) clear; set toy(music) ""; sfx win
set g(shake) 0
say "[lindex $::levels $n 0] is lit" "[format %.1f $secs] s[expr {$new ? " new best" : " best $best($n) s"}] A: next"
}
proc ending {} {
set c .nb.s$::g(scene)
say "The town is lit" "Thank you for the light. A: title"
set ::toy(music) title
}
# Moves the items of the street to the state of the frame.
proc draw {} {
global g h parts ink
set c .nb.s$g(scene)
# The squash and the stretch go back to 1.
set h(sx) [expr {$h(sx) + (1 - $h(sx)) * 0.25}]
set h(sy) [expr {$h(sy) + (1 - $h(sy)) * 0.25}]
set ox 0; set oy 0
if {$g(shake) > 0 && !$::toy(calm)} {
set ox [expr {int((rand() - 0.5) * $g(shake))}]; set oy [expr {int((rand() - 0.5) * $g(shake))}]
}
if {$g(shake) > 0} { incr g(shake) -1 }
set x [expr {$h(x) + $ox}]; set y [expr {$h(y) + 7 + $oy}]
set w [expr {6.5 * $h(sx)}]; set hh [expr {19 * $h(sy)}]
set bob [expr {$h(ground) && abs($h(vx)) > 0.5 ? sin($g(frames) * 0.5) : 0}]
$c coords hcloak [expr {$x - $w}] [expr {$y - $hh * 0.72}] [expr {$x + $w}] $y
$c coords hface [expr {$x - $w * 0.7}] [expr {$y - $hh}] [expr {$x + $w * 0.7}] [expr {$y - $hh * 0.68 + $bob}]
$c coords hhat [expr {$x - $w - 1}] [expr {$y - $hh - 2}] [expr {$x + $w + 1}] [expr {$y - $hh}]
set px [expr {$x + $h(face) * 7}]; set py [expr {$y - $hh - 6}]
$c coords hpole [expr {$x + $h(face) * 3}] [expr {$y - 4}] $px $py
set fl [expr {2.4 + sin($g(frames) * 0.4) * 0.8}]
$c coords hflame [expr {$px - $fl - 1}] [expr {$py - $fl - 3}] [expr {$px + $fl + 1}] [expr {$py + $fl}]
$c coords hcore [expr {$px - 1.2}] [expr {$py - 2}] [expr {$px + 1.2}] [expr {$py + 1}]
if {$g(frames) % 5 == 0 && $g(state) eq "play"} { lappend parts [list $px [expr {$py - 2}] [expr {(rand() - 0.5) * 0.4}] -0.6 30 #ffb35c] }
# The particles: embers, dust, and sparks.
set keep {}
set k 0
foreach p $parts {
lassign $p px py vx vy life color
if {[incr life -1] <= 0 || $k >= 20} continue
set px [expr {$px + $vx}]; set py [expr {$py + $vy}]; set vy [expr {$vy + 0.05}]
set s [expr {$life > 12 ? 2 : 1}]
$c coords p$k $px $py [expr {$px + $s}] [expr {$py + $s}]
if {$::pcolor($k) ne $color} { $c itemconfigure p$k -fill $color; set ::pcolor($k) $color }
lappend keep [list $px $py $vx $vy $life $color]
incr k
}
for {} {$k < 20} {incr k} { $c coords p$k -9 -9 -8 -8 }
set parts $keep
# The boats swing.
set n 0
foreach bt [data boats] {
lassign $bt x0 y x1 dx per
set off [expr {$dx * sin($g(frames) * 6.283 / $per)}]
$c coords bt$n [expr {$x0 + $off}] $y [expr {$x1 + $off}] $y [expr {$x1 + $off - 6}] [expr {$y + 8}] [expr {$x0 + $off + 6}] [expr {$y + 8}]
incr n
}
if {[allLit]} { foreach id [$c find withtag bars] { lassign [$c coords $id] a b cc d; if {$b > 60} { $c coords $id $a [expr {$b - 2}] $cc [expr {$d - 2}] } } }
if {$g(state) eq "title"} return
$c itemconfigure hud -text "[lindex $::levels $g(level) 0] [format %02d:%02d [expr {$g(frames) / 3600}] [expr {($g(frames) / 60) % 60}]]"
set left [expr {1 - $g(dark)}]
$c coords hudfill 361 10 [expr {361 + 102 * $left}] 14
$c itemconfigure hudfill -fill [expr {$left < 0.25 && $g(frames) % 30 < 15 && !$::toy(calm) ? "#ff6a5c" : "#ffd27a"}]
if {$g(msgT) > 0 && $g(state) eq "play" && [incr g(msgT) -1] == 0} { say "" "" }
}
# The title screen: the first street of the picked level, with its name.
proc title {} {
global g levels toy ink best
set g(state) title
set toy(music) title
foreach t [.nb tabs] { .nb forget $t }
set g(level) $g(pick); set g(scene) 0; set g(dark) 0.15
loadStreet
.nb add .nb.s0 -text "Lamplighter"
street 0
set c .nb.s0
$c itemconfigure hero -state normal
$c itemconfigure hud -text ""
$c itemconfigure hudbar -state hidden; $c itemconfigure hudfill -state hidden
$c create text 236 70 -text "LAMPLIGHTER" -fill $ink(gold) -font {TkFixedFont 30 bold} -tags title
set n $g(pick)
set b [expr {[info exists best($n)] ? "best $best($n) s" : "not lit yet"}]
$c create text 236 104 -text "◀ [expr {$n + 1}]. [lindex $levels $n 0] ▶" -fill $ink(text) -font {TkFixedFont 12 bold} -tags title
$c create text 236 124 -text $b -fill $ink(dim) -font {TkFixedFont 10} -tags title
$c create text 236 222 -text "A or Enter: start Left and Right: level" -fill $ink(dim) -font {TkFixedFont 9 bold} -tags "title blink"
for {set k 1} {$k < 5} {incr k} {
set fill [expr {$k < $g(unlocked) ? $ink(gold) : "#4c4466"}]
$c create oval [expr {206 + $k * 12}] 136 [expr {212 + $k * 12}] 142 -width 0 -fill $fill -tags title
}
$c create oval 206 136 212 142 -width 0 -fill $ink(gold) -tags title
array set ::h {x 60 y 189 vx 0.8 vy 0 face 1 sx 1 sy 1 ground 1}
# The lamp of the title is lit.
$c itemconfigure lamp0 -fill $ink(gold)
$c itemconfigure glow0 -state normal
$c raise glow0 window
$c lower glow0 post
}
proc titleTick {} {
global g h
incr g(frames)
set h(x) [expr {$h(x) + 0.8}]
if {$h(x) > 500} { set h(x) -20 }
set h(vx) 0.8; set h(ground) 1
if {$g(frames) % 40 == 0} { .nb.s0 itemconfigure blink -state [expr {$g(frames) % 80 ? "hidden" : "normal"}] }
draw
}
title
tick
# title: Skate Pond
# widget: listbox
# page: docs/widgets/listbox.md
# pitch: A sliding puzzle on a frozen pond. The listbox is your bag: pick up a plank or a pick on the ice, select it, and use it.
# order: 2
# Skate Pond. A push sends the skater over the ice until something stops
# it: a snowbank, a rock, or a patch of snow. Open water ends the run.
# Reach the flag. The listbox on the right is the bag: a plank on the ice
# goes in it, and A lays the selected plank on the water in front of
# you. A pick breaks the rock in front of you. B takes back a move.
# Each level has three stars: the flag, the flag within par, and each
# snowflake on the way.
array set toy {sfx {} music title save {} calm 0}
proc sfx {name} { lappend ::toy(sfx) $name }
set T 28; set COLS 12; set ROWS 9
# The levels, checked with a solver: each one has a way to the flag.
set levels {
{"First Ice" 2 {
############
#S........##
#.........##
#.........##
#.........##
#.........##
#.........##
#........F##
############
}}
{"Snow Stops" 2 {
############
#S....=....#
#..........#
#..........#
#..........#
#.....F....#
#..........#
#..........#
############
}}
{"Thin Spot" 3 {
############
#S.......#.#
#..........#
#.ooooo....#
#.........##
#*.#.......#
#..........#
#......#..F#
############
}}
{"A Plank" 6 {
############
#S..P....#.#
#........#.#
#.......=#.#
#oooooooo#.#
#..........#
#.*.....=..#
#F.........#
############
}}
{"The Pick" 6 {
############
#S..K...#..#
#.##.......#
#..........#
#......#...#
#..*.......#
#RRRRRRRRRR#
#....F.....#
############
}}
{"Crossing" 5 {
############
#S......P.R#
#..........#
#.#......=.#
#oooooooooo#
#........*.#
#....#.....#
#.....F....#
############
}}
{"Long Night" 7 {
############
#S.#......P#
#.......#..#
#=...#.....#
#..........#
#.#..*....=#
#oooooooooo#
#RRRRRRRRRF#
############
}}
}
set ink(bg) #0f1b2d; set ink(panel) #14233a; set ink(text) #eef5fb; set ink(dim) #8fa9c4
set ink(ice) #d4ebf7; set ink(ice2) #c6e2f2; set ink(crack) #a9cde4; set ink(snow) #f6fafd
set ink(shadow) #9dbad2; set ink(rock) #6f7d90; set ink(rock2) #9aa8ba; set ink(water) #1b3a66
set ink(red) #e5484d; set ink(gold) #ffc93c; set ink(flake) #3f8fd8
ztk::style add {
Listbox { background-color: #14233a; color: #eef5fb; border: 1px solid #2c4566; border-radius: 6px; }
}
. configure -background $ink(bg)
canvas .c -width [expr {$T * $COLS}] -height [expr {$T * $ROWS}] -background $ink(bg) -highlightthickness 0
frame .side -background $ink(bg)
label .side.name -background $ink(bg) -foreground $ink(text) -font {TkFixedFont 11 bold} -anchor w -justify left
label .side.moves -background $ink(bg) -foreground $ink(dim) -font {TkFixedFont 9 bold} -anchor w
label .side.stars -background $ink(bg) -foreground $ink(gold) -font {TkFixedFont 10 bold} -anchor w
label .side.bagT -text "BAG" -background $ink(bg) -foreground $ink(dim) -font {TkFixedFont 9 bold} -anchor w
listbox .side.bag -height 5 -width 12 -activestyle none -font {TkFixedFont 10 bold} -exportselection 0 \
-selectbackground $ink(gold) -selectforeground $ink(bg) -highlightthickness 0
label .side.hint -background $ink(bg) -foreground $ink(dim) -font {TkFixedFont 8} -anchor nw -justify left
pack .c -side left -padx {6 4} -pady 9
pack .side -side left -fill both -expand 1 -pady 9 -padx {0 6}
pack .side.name .side.moves .side.stars .side.bagT -fill x
pack .side.bag -fill x -pady {2 6}
pack .side.hint -fill both -expand 1
bind .side.bag <<ListboxSelect>> hint
array set g {state title level 0 pick 0 unlocked 1 frames 0 moves 0 shake 0 dead 0 card 0}
set cardseq 0
array set stars {}
array set sk {x 1 y 1 dx 0 dy 1 px 0 py 0 fx 0 fy 0 t 1 n 1 sx 1 sy 1 queue {}}
set undo {}
set parts {}
proc toy_restore {data} {
global g stars
if {[catch {dict get $data unlocked} u]} return
set g(unlocked) [expr {max(1, min([llength $::levels], $u))}]
if {![catch {dict get $data stars} s]} { array set stars $s }
title
}
proc save {} { set ::toy(save) [list unlocked $::g(unlocked) stars [array get ::stars]] }
proc tile {x y} { lindex $::grid $y $x }
proc setTile {x y c} { lset ::grid $y $x $c }
proc toy_input {key state} {
global g sk
if {$state ne "press"} return
switch $g(state) {
title {
switch $key {
left { if {$g(pick) > 0} { incr g(pick) -1; sfx move; title } }
right { if {$g(pick) < $g(unlocked) - 1} { incr g(pick); sfx move; title } }
a - start { sfx start; start $g(pick) }
}
}
play {
switch $key {
up - down - left - right {
# A press during a slide waits for its end (the buffer).
if {$sk(t) < 1} { set sk(queue) $key } else { push $key }
}
a { use }
b { back }
start { start $g(level) }
}
}
clear {
if {$key in {a start}} {
if {$g(level) + 1 < [llength $::levels]} { sfx start; start [expr {$g(level) + 1}] } else { set g(pick) 0; title }
} elseif {$key eq "b"} { start $g(level) }
}
}
}
# Draws the pond of the level.
proc pond {} {
global grid T COLS ROWS ink
.c delete all
for {set y 0} {$y < $ROWS} {incr y} {
for {set x 0} {$x < $COLS} {incr x} {
set x0 [expr {$x * $T}]; set y0 [expr {$y * $T}]; set x1 [expr {$x0 + $T}]; set y1 [expr {$y0 + $T}]
set c [tile $x $y]
set ice [expr {($x + $y) % 2 ? $ink(ice) : $ink(ice2)}]
.c create rectangle $x0 $y0 $x1 $y1 -width 0 -fill $ice -tags "ice t$x,$y"
if {($x * 7 + $y * 3) % 5 == 0} { .c create line [expr {$x0 + 5}] [expr {$y0 + 9}] [expr {$x0 + 14}] [expr {$y0 + 6}] [expr {$x0 + 20}] [expr {$y0 + 12}] -fill $ink(crack) -tags "t$x,$y" }
switch $c {
# {
.c create rectangle $x0 $y0 $x1 $y1 -width 0 -fill $ink(snow) -tags "t$x,$y"
.c create rectangle $x0 [expr {$y1 - 5}] $x1 $y1 -width 0 -fill $ink(shadow) -tags "t$x,$y"
}
o { water $x $y }
= {
.c create rectangle [expr {$x0 + 2}] [expr {$y0 + 2}] [expr {$x1 - 2}] [expr {$y1 - 2}] -width 0 -fill $ink(snow) -tags "t$x,$y"
foreach {a b} {7 8 16 12 10 19 20 21} { .c create rectangle [expr {$x0 + $a}] [expr {$y0 + $b}] [expr {$x0 + $a + 2}] [expr {$y0 + $b + 2}] -width 0 -fill $ink(shadow) -tags "t$x,$y" }
}
R { rock $x $y }
F {
.c create line [expr {$x0 + 9}] [expr {$y0 + 4}] [expr {$x0 + 9}] [expr {$y1 - 3}] -fill #3a4a60 -width 2 -tags "t$x,$y"
.c create polygon [expr {$x0 + 10}] [expr {$y0 + 4}] [expr {$x0 + 23}] [expr {$y0 + 9}] [expr {$x0 + 10}] [expr {$y0 + 14}] -fill $ink(red) -width 0 -tags "flag t$x,$y"
.c create oval [expr {$x0 + 5}] [expr {$y1 - 5}] [expr {$x0 + 14}] [expr {$y1 - 2}] -width 0 -fill $ink(shadow) -tags "t$x,$y"
}
* { flake $x $y }
P { .c create rectangle [expr {$x0 + 5}] [expr {$y0 + 11}] [expr {$x1 - 5}] [expr {$y0 + 17}] -fill #a8743c -outline #6e4a22 -tags "item t$x,$y" }
K {
.c create line [expr {$x0 + 14}] [expr {$y0 + 8}] [expr {$x0 + 14}] [expr {$y1 - 6}] -fill #6e4a22 -width 3 -tags "item t$x,$y"
.c create line [expr {$x0 + 6}] [expr {$y0 + 10}] [expr {$x0 + 14}] [expr {$y0 + 7}] [expr {$x0 + 22}] [expr {$y0 + 10}] -fill #5c6b7e -width 3 -tags "item t$x,$y"
}
}
}
}
# The skater: skates, a coat, a scarf, and a face.
.c create rectangle 0 0 1 1 -width 0 -fill #24324a -tags "sk skcoat"
.c create rectangle 0 0 1 1 -width 0 -fill $ink(red) -tags "sk skscarf"
.c create rectangle 0 0 1 1 -width 0 -fill $ink(red) -tags "sk sktail"
.c create oval 0 0 1 1 -width 0 -fill #f2d2ac -tags "sk skface"
.c create rectangle 0 0 1 1 -width 0 -fill #c9d4e0 -tags "sk skblade"
for {set k 0} {$k < 16} {incr k} { .c create rectangle -9 -9 -8 -8 -width 0 -fill white -tags "part p$k" }
.c create rectangle 40 86 296 168 -width 2 -outline #2c4566 -fill $ink(bg) -state hidden -tags msgbox
.c create text [expr {$COLS * $T / 2}] [expr {$ROWS * $T / 2 - 12}] -fill $ink(text) -font {TkFixedFont 16 bold} -tags msg
.c create text [expr {$COLS * $T / 2}] [expr {$ROWS * $T / 2 + 16}] -fill $ink(dim) -font {TkFixedFont 9 bold} -tags msg2
}
proc water {x y} {
global T ink
set x0 [expr {$x * $T}]; set y0 [expr {$y * $T}]
.c create rectangle $x0 $y0 [expr {$x0 + $T}] [expr {$y0 + $T}] -width 0 -fill $ink(water) -tags "water t$x,$y"
.c create line [expr {$x0 + 5}] [expr {$y0 + 10}] [expr {$x0 + 13}] [expr {$y0 + 10}] -fill #3f6aa8 -width 2 -tags "ripple t$x,$y"
.c create line [expr {$x0 + 14}] [expr {$y0 + 19}] [expr {$x0 + 23}] [expr {$y0 + 19}] -fill #3f6aa8 -width 2 -tags "ripple t$x,$y"
}
proc rock {x y} {
global T ink
set x0 [expr {$x * $T}]; set y0 [expr {$y * $T}]
.c create oval [expr {$x0 + 3}] [expr {$y0 + 6}] [expr {$x0 + 25}] [expr {$y0 + 26}] -width 0 -fill $ink(rock) -tags "rock t$x,$y"
.c create oval [expr {$x0 + 7}] [expr {$y0 + 9}] [expr {$x0 + 15}] [expr {$y0 + 14}] -width 0 -fill $ink(rock2) -tags "rock t$x,$y"
}
proc flake {x y} {
global T ink
set cx [expr {$x * $T + 14}]; set cy [expr {$y * $T + 14}]
foreach {a b} {0 7 6 4 6 -4} { .c create line [expr {$cx - $a}] [expr {$cy - $b}] [expr {$cx + $a}] [expr {$cy + $b}] -fill $ink(flake) -width 2 -tags "flake t$x,$y" }
}
# Puts plain ice on a tile.
proc clearTile {x y} {
global T ink
.c delete t$x,$y
set x0 [expr {$x * $T}]; set y0 [expr {$y * $T}]
.c create rectangle $x0 $y0 [expr {$x0 + $T}] [expr {$y0 + $T}] -width 0 -fill [expr {($x + $y) % 2 ? $ink(ice) : $ink(ice2)}] -tags "ice t$x,$y"
.c lower t$x,$y sk
.c lower t$x,$y part
}
proc start {n} {
global g levels grid sk undo toy flakes bag
array set g [list state play level $n moves 0 frames 0 dead 0]
set toy(music) play
lassign [lindex $levels $n] name par rows
set grid {}
set y 0
foreach row $rows {
set r [split $row ""]
set x [lsearch $r S]
if {$x >= 0} { set sk(x) $x; set sk(y) $y; lset r $x . }
lappend grid $r
incr y
}
array set sk {dx 0 dy 1 t 1 n 1 sx 1 sy 1 queue {}}
set sk(fx) $sk(x); set sk(fy) $sk(y)
set undo {}; set flakes 0; set bag {}
.side.bag delete 0 end
pond
side
say $name "Par $par"
set g(card) [incr ::cardseq]
after 900 [list cardOff $g(card)]
}
# Hides the level card: at the first action of the player, or after a
# short time. The number makes sure an old timer does not hide the card
# of a later level.
proc cardOff {id} {
global g
if {$g(state) eq "play" && $g(card) == $id} { say "" ""; set g(card) 0 }
}
proc say {a b} {
.c itemconfigure msg -text $a; .c itemconfigure msg2 -text $b
.c itemconfigure msgbox -state [expr {$a eq "" && $b eq "" ? "hidden" : "normal"}]
.c delete msgstar
}
# Three stars on the canvas: gold for each one that you got.
proc stars {x y n tag} {
for {set k 0} {$k < 3} {incr k} {
set cx [expr {$x + ($k - 1) * 26}]; set pts {}
for {set i 0} {$i < 10} {incr i} {
set r [expr {$i % 2 ? 4.5 : 11}]; set a [expr {-1.5708 + $i * 0.6283}]
lappend pts [expr {$cx + $r * cos($a)}] [expr {$y + $r * sin($a)}]
}
.c create polygon {*}$pts -fill [expr {$k < $n ? "#ffc93c" : "#2c4566"}] -outline [expr {$k < $n ? "#ffe08a" : "#4a6a90"}] -tags $tag
}
}
# The labels and the hint of the side panel.
proc side {} {
global g levels flakes
lassign [lindex $levels $g(level)] name par rows
.side.name configure -text "[expr {$g(level) + 1}]. $name"
.side.moves configure -text "MOVES $g(moves) PAR $par"
set total [regexp -all {\*} [join $rows ""]]
.side.stars configure -text [expr {$total ? "FLAKES $flakes/$total" : ""}]
hint
}
proc hint {} {
set i [.side.bag curselection]
if {$i eq ""} {
.side.hint configure -text "Arrows: push\nB: undo\nEnter: restart"
} else {
set item [.side.bag get $i]
.side.hint configure -text [expr {$item eq "Plank" ? "A: lay the plank\non the water\nin front of you." : "A: break the rock\nin front of you."}]
}
}
# A push: the skater slides to the first stop. The slide is a tween with
# an ease-out, so a long slide is fast and settles at the end.
proc push {dir} {
global sk grid g undo flakes bag
if {$g(card)} { cardOff $g(card) }
lassign [dict get {up {0 -1} down {0 1} left {-1 0} right {1 0}} $dir] dx dy
set sk(dx) $dx; set sk(dy) $dy
set x $sk(x); set y $sk(y); set dist 0; set end stop
while 1 {
set nx [expr {$x + $dx}]; set ny [expr {$y + $dy}]
set c [tile $nx $ny]
if {$c in {# R}} break
set x $nx; set y $ny; incr dist
if {$c eq "o"} { set end water; break }
if {$c eq "="} { set end snow; break }
if {$c eq "F"} { set end flag; break }
}
if {$dist == 0} {
# A push into a wall turns the skater and bumps.
set sk(sx) 1.25; set sk(sy) 0.8; sfx bump; return
}
lappend undo [list $sk(x) $sk(y) $grid $flakes $bag $g(moves)]
incr g(moves)
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))}]; set sk(end) $end
sfx slide
side
}
# The tiles that the skater crossed: snowflakes and items.
proc crossed {x y} {
global flakes bag
set c [tile $x $y]
if {$c eq "*"} { setTile $x $y .; clearTile $x $y; incr flakes; sfx pickup; burst [expr {$x * $::T + 14}] [expr {$y * $::T + 14}] 8 #7ec8ff; side }
if {$c in {P K}} {
setTile $x $y .; clearTile $x $y
set name [expr {$c eq "P" ? "Plank" : "Pick"}]
lappend bag $c
.side.bag insert end $name
.side.bag itemconfigure end -foreground [expr {$c eq "P" ? "#e0a86a" : "#b8c6d8"}]
.side.bag selection clear 0 end
.side.bag selection set end
.side.bag see end
sfx pickup; burst [expr {$x * $::T + 14}] [expr {$y * $::T + 14}] 8 #ffc93c
hint
}
}
proc use {} {
global sk bag grid g undo flakes
if {$g(card)} { cardOff $g(card) }
set i [.side.bag curselection]
if {$i eq "" || $sk(t) < 1} { sfx bump; return }
set item [lindex $bag $i]
set x [expr {$sk(x) + $sk(dx)}]; set y [expr {$sk(y) + $sk(dy)}]
set c [tile $x $y]
if {($item eq "P" && $c ne "o") || ($item eq "K" && $c ne "R")} {
set ::g(shake) 4; sfx bump
.side.hint configure -text [expr {$item eq "P" ? "Face open water\nto lay the plank." : "Face a rock\nto break it."}]
return
}
lappend undo [list $sk(x) $sk(y) $grid $flakes $bag $g(moves)]
incr g(moves)
setTile $x $y .; clearTile $x $y
if {$item eq "P"} {
.c create rectangle [expr {$x * $::T + 1}] [expr {$y * $::T + 4}] [expr {$x * $::T + 27}] [expr {$y * $::T + 24}] -fill #a8743c -outline #6e4a22 -tags "t$x,$y"
.c lower t$x,$y sk
sfx land
} else { sfx crash; set g(shake) 6; burst [expr {$x * $::T + 14}] [expr {$y * $::T + 14}] 12 #9aa8ba }
set bag [lreplace $bag $i $i]
.side.bag delete $i
if {[llength $bag]} { .side.bag selection set 0 }
side
}
proc back {} {
global undo sk grid flakes bag g
if {![llength $undo] || $sk(t) < 1} return
lassign [lindex $undo end] sk(x) sk(y) grid flakes bag g(moves)
set undo [lrange $undo 0 end-1]
set sk(fx) $sk(x); set sk(fy) $sk(y)
pond
.side.bag delete 0 end
foreach c $bag { .side.bag insert end [expr {$c eq "P" ? "Plank" : "Pick"}]; .side.bag itemconfigure end -foreground [expr {$c eq "P" ? "#e0a86a" : "#b8c6d8"}] }
if {[llength $bag]} { .side.bag selection set 0 }
set g(dead) 0; set g(state) play
say "" ""; sfx undo; side
}
proc burst {x y n color} {
global parts
for {set k 0} {$k < $n} {incr k} {
set a [expr {rand() * 6.283}]; set s [expr {0.5 + rand() * 1.6}]
lappend parts [list $x $y [expr {cos($a) * $s}] [expr {sin($a) * $s}] [expr {18 + int(rand() * 14)}] $color]
}
set parts [lrange $parts end-15 end]
}
proc tick {} {
global g sk T
after 16 tick
incr g(frames)
if {$g(state) eq "title"} { titleTick; draw; return }
if {$sk(t) < 1} {
set before [expr {int(($sk(fx) + ($sk(x) - $sk(fx)) * [easeOut $sk(t)]) + 0.5)}],[expr {int(($sk(fy) + ($sk(y) - $sk(fy)) * [easeOut $sk(t)]) + 0.5)}]
set sk(t) [expr {min(1.0, $sk(t) + 1.0 / $sk(n))}]
set e [easeOut $sk(t)]
set cx [expr {int(($sk(fx) + ($sk(x) - $sk(fx)) * $e) + 0.5)}]; set cy [expr {int(($sk(fy) + ($sk(y) - $sk(fy)) * $e) + 0.5)}]
# Each tile on the way: pick up what is on it.
lassign [split $before ,] bx by
while {$bx != $cx || $by != $cy} {
if {$bx != $cx} { incr bx [expr {$cx > $bx ? 1 : -1}] } else { incr by [expr {$cy > $by ? 1 : -1}] }
crossed $bx $by
}
if {$g(frames) % 2 == 0} { burst [expr {($sk(fx) + ($sk(x) - $sk(fx)) * $e) * $T + 14}] [expr {($sk(fy) + ($sk(y) - $sk(fy)) * $e) * $T + 24}] 1 #ffffff }
if {$sk(t) >= 1} { arrive }
} elseif {$sk(queue) ne "" && $g(state) eq "play"} {
set k $sk(queue); set sk(queue) {}; push $k
}
if {$g(state) eq "dead" && [incr g(dead)] > 50} { back }
draw
}
proc easeOut {t} { expr {1 - pow(1 - $t, 3)} }
proc arrive {} {
global sk g T levels stars toy
switch $sk(end) {
stop { set sk(sx) [expr {$sk(dx) ? 0.75 : 1.25}]; set sk(sy) [expr {$sk(dy) ? 0.75 : 1.2}]; sfx bump; set g(shake) 3 }
snow { set sk(sx) 1.15; set sk(sy) 0.85; sfx land; burst [expr {$sk(x) * $T + 14}] [expr {$sk(y) * $T + 22}] 8 #f6fafd }
water {
set g(state) dead; set g(dead) 0; set sk(queue) {}
sfx hurt; set g(shake) 8
burst [expr {$sk(x) * $T + 14}] [expr {$sk(y) * $T + 14}] 14 #7ec8ff
say "Splash" "The run goes back one move"
}
flag {
set lvl $g(level)
lassign [lindex $levels $lvl] name par rows
set total [regexp -all {\*} [join $rows ""]]
set got [expr {1 + ($g(moves) <= $par) + ($::flakes == $total)}]
if {![info exists stars($lvl)] || $got > $stars($lvl)} { set stars($lvl) $got }
set g(unlocked) [expr {max($g(unlocked), min([llength $levels], $lvl + 2))}]
save
set g(state) clear; set toy(music) ""; sfx win
burst [expr {$sk(x) * $T + 14}] [expr {$sk(y) * $T + 10}] 16 #ffc93c
say "" "$g(moves) moves, par $par\nA: next B: again"
.c itemconfigure msgbox -state normal
stars 168 114 $got msgstar
}
}
}
# Moves the skater and the particles to the state of the frame.
proc draw {} {
global sk g T parts
set e [easeOut $sk(t)]
set ox 0; set oy 0
if {$g(shake) > 0} { if {!$::toy(calm)} { set ox [expr {(rand() - 0.5) * $g(shake)}]; set oy [expr {(rand() - 0.5) * $g(shake)}] }; incr g(shake) -1 }
set x [expr {($sk(fx) + ($sk(x) - $sk(fx)) * $e) * $T + 14 + $ox}]
set y [expr {($sk(fy) + ($sk(y) - $sk(fy)) * $e) * $T + 25 + $oy}]
if {$g(state) eq "dead"} { set y [expr {$y + min(10, $g(dead) / 3)}] }
set sk(sx) [expr {$sk(sx) + (1 - $sk(sx)) * 0.2}]
set sk(sy) [expr {$sk(sy) + (1 - $sk(sy)) * 0.2}]
set w [expr {7 * $sk(sx)}]; set h [expr {20 * $sk(sy)}]
# The skater leans into the slide.
set lean [expr {$sk(t) < 1 ? $sk(dx) * 3 : 0}]
.c coords skblade [expr {$x - $w - 1}] [expr {$y - 1}] [expr {$x + $w + 1}] $y
.c coords skcoat [expr {$x - $w + $lean / 2}] [expr {$y - $h * 0.68}] [expr {$x + $w + $lean / 2}] [expr {$y - 2}]
.c coords skscarf [expr {$x - $w + $lean}] [expr {$y - $h * 0.72}] [expr {$x + $w + $lean}] [expr {$y - $h * 0.6}]
set tail [expr {$sk(t) < 1 ? -$sk(dx) * 9 : -3}]
set wave [expr {sin($g(frames) * 0.4) * 2}]
.c coords sktail [expr {$x + $lean + min(0, $tail)}] [expr {$y - $h * 0.72 + $wave}] [expr {$x + $lean + max(0, $tail)}] [expr {$y - $h * 0.62 + $wave}]
.c coords skface [expr {$x - 5 + $lean}] [expr {$y - $h - 2}] [expr {$x + 5 + $lean}] [expr {$y - $h * 0.66}]
set keep {}; set k 0
foreach p $parts {
lassign $p px py vx vy life color
if {[incr life -1] <= 0 || $k >= 16} continue
set px [expr {$px + $vx}]; set py [expr {$py + $vy}]; set vx [expr {$vx * 0.93}]; set vy [expr {$vy * 0.93}]
.c coords p$k $px $py [expr {$px + 2}] [expr {$py + 2}]
.c itemconfigure p$k -fill $color
lappend keep [list $px $py $vx $vy $life $color]
incr k
}
for {} {$k < 16} {incr k} { .c coords p$k -9 -9 -8 -8 }
set parts $keep
if {$g(frames) % 30 == 0} { .c move ripple [expr {$g(frames) % 60 ? 2 : -2}] 0 }
}
# The title: the first pond, the skater on a loop, and the level picker.
proc title {} {
global g levels stars toy ink grid T COLS ROWS
set g(state) title; set toy(music) title
set grid {}
foreach row [lindex $levels 0 2] { lappend grid [split [string map {S . F . o .} $row] ""] }
pond
.side.bag delete 0 end
.c create rectangle 30 46 306 186 -width 0 -fill $ink(bg) -tags title
.c create text 168 76 -text "SKATE POND" -fill $ink(text) -font {TkFixedFont 22 bold} -tags title
set n $g(pick)
set s [expr {[info exists stars($n)] ? $stars($n) : 0}]
.c create text 168 110 -text "◀ [expr {$n + 1}]. [lindex $levels $n 0] ▶" -fill $ink(text) -font {TkFixedFont 11 bold} -tags title
stars 168 136 $s title
.c create text 168 166 -text "A: skate Left, Right: level" -fill $ink(dim) -font {TkFixedFont 9 bold} -tags "title blink"
.c raise sk
.side.name configure -text "Skate Pond"
.side.moves configure -text "[expr {$g(unlocked)}] of [llength $levels] ponds open"
set total 0; foreach k [array names stars] { incr total $stars($k) }
.side.stars configure -text "STARS $total/[expr {3 * [llength $levels]}]"
.side.hint configure -text "Ice stops only\nat snowbanks,\nrocks, and snow."
array set ::sk {t 1 dx 1 dy 0 sx 1 sy 1 queue {}}
}
proc titleTick {} {
global sk g
# A slow loop around the box of the title.
set a [expr {$g(frames) * 0.02}]
set sk(fx) [expr {5.5 + 4.6 * cos($a)}]; set sk(fy) [expr {3.6 + 3.5 * sin($a) * cos($a)}]
set sk(x) $sk(fx); set sk(y) $sk(fy); set sk(t) 1
set sk(dx) [expr {-sin($a) > 0 ? 1 : -1}]
if {$g(frames) % 40 == 0} { .c itemconfigure blink -state [expr {$g(frames) % 80 ? "hidden" : "normal"}] }
}
title
tick
# title: Tin Moon
# widget: ttk::scale
# page: docs/widgets/ttk.md
# pitch: Fly a tin balloon over a small moon. You steer only with height: each layer of the sky has its own wind, and the ttk::scale is your burner.
# order: 3
# Tin Moon. The ttk::scale on the right is the burner. More heat makes
# the balloon rise, slowly, as warm air does. You cannot steer left or
# right: each layer of the sky has a wind, and the streaks show its way.
# Climb or sink into the wind that you need, and land softly on the
# lit pad. The ttk::progressbar is the fuel. Moon motes on the way are
# extra points. Up and Down move the scale, or drag it.
array set toy {sfx {} music title save {} calm 0}
proc sfx {name} { lappend ::toy(sfx) $name }
set VW 420; set VH 236
# The levels: the width of the world, the winds of the layers from the
# top {top bottom speed}, the ground {x y ...}, the start, the pad
# {x0 x1 y}, the motes {x y}, and the fuel.
set levels {
{"Drift" 900 {{0 236 1.1}}
{0 200 160 200 260 190 420 205 600 196 700 200 900 200} {80 200} {700 780 200} {{300 140} {460 120} {560 150}} 100}
{"Two Winds" 1100 {{0 110 1.6} {110 236 -0.6}}
{0 205 180 205 300 170 380 200 560 186 760 200 880 200 1100 205} {90 205} {880 960 200} {{260 80} {520 60} {700 140}} 100}
{"Back Home" 1000 {{0 90 -1.4} {90 160 0.0} {160 236 1.2}}
{0 200 120 200 240 180 420 200 640 192 800 200 1000 200} {700 200} {120 200 200} {{560 60} {380 120} {240 70}} 90}
{"Spires" 1300 {{0 80 1.8} {80 170 0.4} {170 236 -0.5}}
{0 205 150 205 260 205 300 120 330 205 520 205 560 100 600 205 820 205 860 130 890 205 1060 205 1300 205} {80 205} {1100 1180 205} {{300 70} {560 50} {870 80} {700 180}} 90}
{"Long Night" 1600 {{0 70 -1.0} {70 140 1.6} {140 236 -0.4}}
{0 205 200 205 330 160 420 205 640 190 720 120 780 205 1000 205 1080 150 1150 205 1400 205 1600 205} {100 205} {1420 1500 205} {{420 100} {720 60} {1000 120} {1300 50} {560 160}} 85}
}
set ink(sky0) #0d1726; set ink(sky1) #1a2c47; set ink(sky2) #2a4266; set ink(moon) #f3e6c0
set ink(ground) #1b2333; set ink(ridge) #33415c; set ink(text) #f1ead8; set ink(dim) #8fa3bf
set ink(env1) #f2a541; set ink(env2) #f6e7c8; set ink(basket) #8a5a2b; set ink(pad) #6fd3a8; set ink(mote) #b9f0ff
ztk::style add {
TScale::trough { background-color: #22324d; }
TScale::slider { background-color: #f2a541; }
TProgressbar::trough { background-color: #22324d; }
TProgressbar::bar { background-color: #6fd3a8; }
}
. configure -background $ink(sky0)
canvas .c -width $VW -height $VH -background $ink(sky0) -highlightthickness 0
frame .side -background $ink(sky0)
label .side.heatT -text HEAT -background $ink(sky0) -foreground $ink(dim) -font {TkFixedFont 8 bold}
ttk::scale .side.heat -orient vertical -from 1 -to 0 -variable heat -length 150
label .side.fuelT -text FUEL -background $ink(sky0) -foreground $ink(dim) -font {TkFixedFont 8 bold}
ttk::progressbar .fuel -orient horizontal -maximum 100 -variable fuel -length $VW
grid .c -row 0 -column 0 -padx {6 4} -pady {6 2}
grid .side -row 0 -column 1 -rowspan 2 -sticky ns -pady 6
grid .fuel -row 1 -column 0 -sticky ew -padx {6 4} -pady {0 6}
pack .side.heatT -pady {4 2}
pack .side.heat -fill y -expand 1
label .side.score -background $ink(sky0) -foreground $ink(text) -font {TkFixedFont 9 bold} -text ""
pack .side.score -pady 4
set heat 0.45; set fuel 100
array set g {state title level 0 pick 0 unlocked 1 frames 0 shake 0 cam 0 hold 0 motes 0 layer -1 t 0}
array set best {}
array set b {x 80 y 180 vx 0 vy 0 temp 0.45 sway 0 swayV 0 landed 1}
set parts {}
proc toy_restore {data} {
global g best
if {[catch {dict get $data unlocked} u]} return
set g(unlocked) [expr {max(1, min([llength $::levels], $u))}]
if {![catch {dict get $data best} v]} { array set best $v }
title
}
proc save {} { set ::toy(save) [list unlocked $::g(unlocked) best [array get ::best]] }
proc toy_input {key state} {
global g
set on [expr {$state eq "press"}]
switch $g(state) {
title {
if {!$on} return
switch $key {
left { if {$g(pick) > 0} { incr g(pick) -1; sfx move; title } }
right { if {$g(pick) < $g(unlocked) - 1} { incr g(pick); sfx move; title } }
a - start - up { sfx start; start $g(pick) }
}
}
play {
# Up and Down hold the scale: it moves while the key is down.
if {$key in {up a}} { set g(hold) [expr {$on ? 1 : 0}] }
if {$key in {down b}} { set g(hold) [expr {$on ? -1 : 0}] }
if {$key eq "start" && $on} { start $g(level) }
}
landed - crashed {
if {$on && $key in {a start}} {
if {$g(state) eq "crashed"} { start $g(level) } \
elseif {$g(level) + 1 < [llength $::levels]} { sfx start; start [expr {$g(level) + 1}] } \
else { set g(pick) 0; title }
} elseif {$on && $key eq "b"} { start $g(level) }
}
}
}
proc level {what} {
set k [lsearch {name width winds ground start pad motes fuel} $what]
lindex $::levels $::g(level) $k
}
# The height of the ground at x, from the points of the level.
proc groundY {x} {
set pts [level ground]
foreach {x0 y0} [lrange $pts 0 end-2] {x1 y1} [lrange $pts 2 end] {
if {$x1 eq ""} break
if {$x >= $x0 && $x <= $x1} { return [expr {$y0 + ($y1 - $y0) * ($x - $x0) / double($x1 - $x0)}] }
}
return [lindex $pts end]
}
# The wind at a height: the layers blend over 14 pixels.
proc wind {y} {
set w 0.0
foreach l [level winds] {
lassign $l top bot speed
if {$y >= $top && $y < $bot} { set w $speed }
}
return $w
}
proc world {} {
global ink VW VH g
.c delete all
set W [level width]
.c configure -scrollregion [list 0 0 $W $VH]
# The sky: bands, stars, and the moon, which move with the camera.
for {set k 0} {$k < 12} {incr k} {
set t [expr {$k / 11.0}]
.c create rectangle -40 [expr {$k * 20}] [expr {$VW + 40}] [expr {($k + 1) * 20}] -width 0 -fill [mixc $ink(sky0) $ink(sky2) $t] -tags sky
}
for {set k 0} {$k < 40} {incr k} {
set x [expr {($k * 97) % $VW}]; set y [expr {($k * 53) % 150}]
.c create rectangle $x $y [expr {$x + 1 + $k % 2}] [expr {$y + 1 + $k % 2}] -width 0 -fill [expr {$k % 3 ? "#7f93b8" : "#dfe8ff"}] -tags sky
}
.c create oval 300 18 352 70 -width 0 -fill $ink(moon) -tags sky
.c create oval 312 30 322 40 -width 0 -fill #e2d3a8 -tags sky
.c create oval 330 46 340 54 -width 0 -fill #e2d3a8 -tags sky
# The far hills at half speed.
set pts {}; for {set x 0} {$x <= $W / 2 + $VW} {incr x 30} { lappend pts $x [expr {175 + 18 * sin($x / 47.0) + 10 * sin($x / 13.0)}] }
.c create polygon 0 $VH {*}$pts [expr {$W / 2 + $VW}] $VH -width 0 -fill #16213a -tags far
# The layers of wind: a faint line at each border, and an arrow.
foreach l [level winds] {
lassign $l top bot speed
if {$top > 0} { .c create line 0 $top $W $top -fill #243756 -dash {4 6} -tags layer }
set mid [expr {($top + $bot) / 2}]
for {set x 60} {$x < $W} {incr x 240} {
set a [expr {$speed > 0.05 ? "▶" : $speed < -0.05 ? "◀" : "●"}]
.c create text $x $mid -text $a -fill #2f4770 -font {TkFixedFont 9 bold} -tags layer
}
}
.c create polygon 0 $VH {*}[level ground] $W $VH -width 0 -fill $ink(ground) -tags ground
.c create line {*}[level ground] -fill $ink(ridge) -width 2 -tags ground
lassign [level pad] x0 x1 y
.c create rectangle $x0 [expr {$y - 4}] $x1 $y -width 0 -fill $ink(pad) -tags pad
.c create rectangle [expr {$x0 + 4}] [expr {$y - 12}] [expr {$x0 + 6}] [expr {$y - 4}] -width 0 -fill $ink(pad) -tags pad
.c create rectangle [expr {$x1 - 6}] [expr {$y - 12}] [expr {$x1 - 4}] [expr {$y - 4}] -width 0 -fill $ink(pad) -tags pad
.c create oval [expr {$x0 + 2}] [expr {$y - 17}] [expr {$x0 + 8}] [expr {$y - 11}] -width 0 -fill #c8ffe6 -tags "pad padlight"
.c create oval [expr {$x1 - 8}] [expr {$y - 17}] [expr {$x1 - 2}] [expr {$y - 11}] -width 0 -fill #c8ffe6 -tags "pad padlight"
set k 0
foreach m [level motes] {
lassign $m x y
.c create oval [expr {$x - 7}] [expr {$y - 7}] [expr {$x + 7}] [expr {$y + 7}] -width 0 -fill #1f3a55 -tags "mote m$k"
.c create oval [expr {$x - 3}] [expr {$y - 3}] [expr {$x + 3}] [expr {$y + 3}] -width 0 -fill $ink(mote) -tags "mote m$k"
incr k
}
# The wind streaks: short lines that move with the wind of their layer.
for {set k 0} {$k < 26} {incr k} { .c create line 0 0 1 1 -fill #4c6a96 -width 1 -tags "streak s$k" }
# The balloon: the envelope in two stripes, ropes, a basket, a flame.
.c create oval 0 0 1 1 -width 0 -fill $ink(env1) -tags "bal benv"
.c create polygon 0 0 1 1 2 2 -width 0 -fill $ink(env2) -tags "bal bstripe"
.c create polygon 0 0 1 1 2 2 -width 0 -fill $ink(env1) -tags "bal bneck"
.c create line 0 0 1 1 -fill #c9b48a -tags "bal brope1"
.c create line 0 0 1 1 -fill #c9b48a -tags "bal brope2"
.c create rectangle 0 0 1 1 -width 0 -fill $ink(basket) -tags "bal bbasket"
.c create oval 0 0 1 1 -width 0 -fill #ffcf5c -state hidden -tags "bal bflame"
for {set k 0} {$k < 18} {incr k} { .c create rectangle -9 -9 -8 -8 -width 0 -fill #ffcf5c -tags "part p$k" }
set g(motes) 0
set g(skyX) 0
}
proc mixc {a b t} {
scan $a #%2x%2x%2x r1 g1 b1; scan $b #%2x%2x%2x r2 g2 b2
format #%02x%02x%02x [expr {int($r1 + ($r2 - $r1) * $t)}] [expr {int($g1 + ($g2 - $g1) * $t)}] [expr {int($b1 + ($b2 - $b1) * $t)}]
}
proc start {n} {
global g b heat fuel toy streaks
array set g [list state play level $n frames 0 shake 0 hold 0 layer -1 cam 0]
set toy(music) play
world
lassign [level start] x y
array set b [list x $x y [expr {$y - 1}] vx 0 vy 0 temp 0.3 sway 0 swayV 0 landed 1]
set heat 0.3; set fuel [level fuel]
set streaks {}
for {set k 0} {$k < 26} {incr k} { lappend streaks [list [expr {rand() * $::VW}] [expr {10 + rand() * 190}]] }
.side.score configure -text "[expr {$n + 1}]/5"
say [level name] "Up: more heat. Land on the green pad."
}
proc say {a b2} {
global VW
.c delete msg
if {$a eq "" && $b2 eq ""} return
set x [expr {([info exists ::g(skyX)] ? $::g(skyX) : 0) + $VW / 2}]
.c create rectangle [expr {$x - 150}] 74 [expr {$x + 150}] 136 -width 1 -outline #2f4770 -fill $::ink(sky0) -tags msg
.c create text $x 94 -text $a -fill $::ink(text) -font {TkFixedFont 15 bold} -tags msg
.c create text $x 120 -text $b2 -fill $::ink(dim) -font {TkFixedFont 9 bold} -tags msg
set ::g(msgT) 180
}
proc burst {x y n color {vy 0}} {
global parts
for {set k 0} {$k < $n} {incr k} {
set a [expr {rand() * 6.283}]; set s [expr {0.4 + rand() * 1.4}]
lappend parts [list $x $y [expr {cos($a) * $s}] [expr {sin($a) * $s + $vy}] [expr {18 + int(rand() * 16)}] $color]
}
set parts [lrange $parts end-17 end]
}
proc tick {} {
global g
after 16 tick
incr g(frames)
if {$g(state) eq "title"} { titleTick; draw; return }
if {$g(state) eq "play"} { step }
draw
}
proc step {} {
global g b heat fuel VW VH
# The keys move the scale; the scale sets the burner.
if {$g(hold)} { set heat [expr {max(0.0, min(1.0, $heat + $g(hold) * 0.025))}] }
if {$fuel <= 0} { set heat 0 }
# Warm air follows the burner with a lag: the balloon is slow to answer.
set b(temp) [expr {$b(temp) + ($heat - $b(temp)) * 0.025}]
set lift [expr {($b(temp) - 0.42) * 0.09}]
set b(vy) [expr {($b(vy) - $lift + 0.0) * 0.985}]
set b(vy) [expr {max(-1.6, min(2.2, $b(vy)))}]
set fuel [expr {max(0, $fuel - $heat * 0.045)}]
if {$heat > 0.55 && $g(frames) % 9 == 0} { sfx burn; burst $b(x) [expr {$b(y) - 22}] 1 #ffcf5c -0.6 }
# The wind of the layer moves the balloon; it takes a moment to change.
set w [wind $b(y)]
set ax [expr {($w - $b(vx)) * 0.03}]
if {$b(landed)} { set ax [expr {-$b(vx) * 0.3}] }
set b(vx) [expr {$b(vx) + $ax}]
# The basket swings when the balloon speeds up.
set b(swayV) [expr {($b(swayV) - $ax * 2.2 - $b(sway) * 0.04) * 0.96}]
set b(sway) [expr {$b(sway) + $b(swayV)}]
set layer [lsearch -exact [lmap l [level winds] {expr {$b(y) >= [lindex $l 0] && $b(y) < [lindex $l 1]}}] 1]
if {$layer != $g(layer)} { if {$g(layer) >= 0} { sfx gust }; set g(layer) $layer }
set b(x) [expr {$b(x) + $b(vx)}]
set b(y) [expr {$b(y) + $b(vy)}]
set W [level width]
if {$b(x) < 14} { set b(x) 14; set b(vx) 0 }
if {$b(x) > $W - 14} { set b(x) [expr {$W - 14}]; set b(vx) 0 }
# The envelope stays in the view: the sky has a ceiling.
if {$b(y) < 72} { set b(y) 72; set b(vy) [expr {max(0, $b(vy))}] }
# The ground: a soft touch lands, a fast one crashes.
set gy [groundY $b(x)]
if {$b(y) >= $gy} {
# A wall of the ground, such as a spire, is a crash too.
if {$b(vy) > 1.5 || abs($b(sway)) > 12 || $b(y) - $gy > 6} { set b(y) $gy; crash; return }
set b(y) $gy
if {!$b(landed) && $b(vy) > 0.3} { sfx land; burst $b(x) $gy 6 #8fa3bf; if {$b(vy) > 0.8} { set g(shake) 4 } }
set b(vy) 0; set b(landed) 1
lassign [level pad] x0 x1 py
if {$b(x) > $x0 + 4 && $b(x) < $x1 - 4 && abs($gy - $py) < 3 && $g(frames) > 60} { landed; return }
} elseif {$b(y) < $gy - 2} { set b(landed) 0 }
# The motes.
set k 0
foreach m [level motes] {
lassign $m mx my
if {[.c itemcget m$k -state] ne "hidden" && hypot($mx - $b(x), $my - ($b(y) - 24)) < 18} {
.c itemconfigure m$k -state hidden
incr g(motes); sfx pickup; burst $mx $my 10 #b9f0ff
}
incr k
}
# The camera follows the balloon, a third of the view ahead of it.
set want [expr {max(0, min($W - $VW, $b(x) - $VW / 2 + $b(vx) * 40))}]
set g(cam) [expr {$g(cam) + ($want - $g(cam)) * 0.08}]
if {[info exists g(msgT)] && $g(msgT) > 0 && [incr g(msgT) -1] == 0} { say "" "" }
}
proc crash {} {
global g b toy
set g(state) crashed; set toy(music) ""
sfx crash; set g(shake) 12
burst $b(x) [expr {$b(y) - 20}] 18 #f2a541
say "Too fast" "Touch down slowly. A: try again"
}
proc landed {} {
global g b fuel best toy
set n $g(level)
set score [expr {int($fuel) * 10 + $g(motes) * 250}]
set new [expr {![info exists best($n)] || $score > $best($n)}]
if {$new} { set best($n) $score }
set g(unlocked) [expr {max($g(unlocked), min([llength $::levels], $n + 2))}]
save
set g(state) landed; set toy(music) ""; sfx win
burst $b(x) [expr {$b(y) - 30}] 16 #6fd3a8
say "Delivered: $score" "[expr {$new ? "New best" : "Best $best($n)"}]. A: next B: again"
}
proc draw {} {
global g b parts VW streaks heat
set ox 0; set oy 0
if {$g(shake) > 0} { if {!$::toy(calm)} { set ox [expr {(rand() - 0.5) * $g(shake)}]; set oy [expr {(rand() - 0.5) * $g(shake)}] }; incr g(shake) -1 }
set W [level width]
set cam [expr {$g(cam) + $ox}]
# The left edge of the view, as xview sets it from the fraction.
set cx [expr {max(0.0, min($W - $VW, $cam))}]
.c xview moveto [expr {$cx / double($W)}]
# The sky stays; the far hills move at half the speed.
set d [expr {$cx - $g(skyX)}]
.c move sky $d 0
.c move far [expr {$d * 0.5}] 0
set g(skyX) $cx
.c move msg [expr {$cx - [expr {[info exists g(msgX)] ? $g(msgX) : $cx}]}] 0
set g(msgX) $cx
# The streaks of wind.
set out {}; set k 0
foreach s $streaks {
lassign $s sx sy
set sx [expr {$sx + [wind $sy] * 1.6}]
if {$sx > $VW + 20} { set sx -20 } elseif {$sx < -20} { set sx [expr {$VW + 20}] }
set len [expr {abs([wind $sy]) * 9}]
.c coords s$k [expr {$cx + $sx}] $sy [expr {$cx + $sx + ($len < 1 ? 1 : $len)}] $sy
lappend out [list $sx $sy]; incr k
}
set streaks $out
# The balloon.
set x $b(x); set y [expr {$b(y) + $oy}]
set top [expr {$y - 62}]
set inflate [expr {$g(state) eq "crashed" ? 0.6 : 1.0}]
set r [expr {19 * $inflate}]
.c coords benv [expr {$x - $r}] $top [expr {$x + $r}] [expr {$top + 2 * $r + 4}]
.c coords bstripe [expr {$x - 5}] [expr {$top + 1}] [expr {$x + 5}] [expr {$top + 1}] [expr {$x + 4}] [expr {$top + 2 * $r + 3}] [expr {$x - 4}] [expr {$top + 2 * $r + 3}]
set nb [expr {$top + 2 * $r + 2}]
.c coords bneck [expr {$x - 9}] [expr {$nb - 4}] [expr {$x + 9}] [expr {$nb - 4}] [expr {$x + 4}] [expr {$nb + 6}] [expr {$x - 4}] [expr {$nb + 6}]
set bx [expr {$x + $b(sway)}]; set by [expr {$y - 9}]
.c coords brope1 [expr {$x - 4}] [expr {$nb + 6}] [expr {$bx - 5}] $by
.c coords brope2 [expr {$x + 4}] [expr {$nb + 6}] [expr {$bx + 5}] $by
.c coords bbasket [expr {$bx - 6}] $by [expr {$bx + 6}] [expr {$y - 1}]
set fl [expr {$heat * 6 + sin($g(frames) * 0.6) * 1.2}]
if {$g(state) eq "play" && $heat > 0.05} {
.c itemconfigure bflame -state normal
.c coords bflame [expr {$x - 2.5}] [expr {$nb + 6 - $fl}] [expr {$x + 2.5}] [expr {$nb + 8}]
} else { .c itemconfigure bflame -state hidden }
set keep {}; set k 0
foreach p $parts {
lassign $p px py vx vy life color
if {[incr life -1] <= 0 || $k >= 18} continue
set px [expr {$px + $vx}]; set py [expr {$py + $vy}]; set vy [expr {$vy + 0.03}]
.c coords p$k $px $py [expr {$px + 2}] [expr {$py + 2}]
.c itemconfigure p$k -fill $color
lappend keep [list $px $py $vx $vy $life $color]; incr k
}
for {} {$k < 18} {incr k} { .c coords p$k -9 -9 -8 -8 }
set parts $keep
if {$g(frames) % 30 == 0} { .c itemconfigure padlight -fill [expr {$g(frames) % 60 ? "#c8ffe6" : "#3c8a6a"}] }
if {$g(state) ne "title"} { .side.score configure -text "● $g(motes)" }
}
# The title: the balloon drifts over the first level, with the picker.
proc title {} {
global g best toy ink VW
set g(state) title; set toy(music) title
set lv $g(pick); set g(level) 0
world
array set ::b {x 200 y 150 vx 0 vy 0 sway 0 swayV 0 landed 0}
set ::streaks {}
for {set k 0} {$k < 26} {incr k} { lappend ::streaks [list [expr {rand() * $VW}] [expr {10 + rand() * 190}]] }
set g(cam) 0
.c create text 210 70 -text "TIN MOON" -fill $ink(text) -font {TkFixedFont 26 bold} -tags title
.c create text 210 104 -text "◀ [expr {$lv + 1}]. [lindex $::levels $lv 0] ▶" -fill $ink(text) -font {TkFixedFont 11 bold} -tags title
.c create text 210 124 -text [expr {[info exists best($lv)] ? "best $best($lv)" : "not flown yet"}] -fill $ink(dim) -font {TkFixedFont 9} -tags title
.c create text 210 222 -text "A: fly Left, Right: level" -fill $ink(dim) -font {TkFixedFont 9 bold} -tags "title blink"
.side.score configure -text "[expr {$g(unlocked)}]/5"
}
proc titleTick {} {
global g b
set b(x) [expr {290 + 60 * sin($g(frames) * 0.01)}]
set b(y) [expr {176 + 8 * sin($g(frames) * 0.03)}]
set b(sway) [expr {3 * sin($g(frames) * 0.05)}]
set ::heat [expr {0.5 + 0.3 * sin($g(frames) * 0.03)}]
if {$g(frames) % 40 == 0} { .c itemconfigure blink -state [expr {$g(frames) % 80 ? "hidden" : "normal"}] }
}
title
tick
Play
- Click the frame, or press Enter on its button, to give the game the keys. A ring shows that the game has them.
- The arrow keys or W, A, S, and D move. Space or Z is A, and X is B. Enter starts.
- Escape or P pauses the game and gives the keys back to the page. The page scrolls as usual while the game has no keys.
- A gamepad works while the game has the keys. A touch screen shows a pad and two buttons in the frame.
- The sound and the music are off until you turn them on under the frame. "Shake off" turns off the screen shake and the flashes.
- Each game keeps your open levels and your best scores in this browser only.
How the games are made
Each game is one Tcl script for ztk, from 400 to 600 lines. The design notes give the goals, the controls, the levels, and the checks of each game:
The sound is made at run time in the page, with oscillators and noise of the Web Audio API. No game uses a sample, a font of its own, or an image file.