Skate Pond 1: Draw the pond
In this step you draw the pond of Skate Pond on a canvas, from a list of strings. You learn the canvas items, their options, and the tags that name a group of items.
A level is a list of strings
Each level of the game is a list of rows, and each character is one square. A text editor shows the shape of the pond, so a level is easy to make by hand. The game keeps its seven levels of this kind in the list levels (the code):
{"Snow Stops" 2 {
############
#S....=....#
#..........#
#..........#
#..........#
#.....F....#
#..........#
#..........#
############
}}
The name comes first, then par (the fewest moves to the flag), then the rows. S is the start of the skater.
One square, several canvas items
A canvas draws items: rectangle, oval, line, polygon, and text. create makes an item at its coordinates, and the options give its look: -fill is the color inside, -width 0 draws no outline, and -tags gives the item names. The snippet below reads the rows and makes the items of each square, as the proc pond of the game does (the code). Each square is 28 by 28 pixels.
# The pond: # is a snowbank, = snow, o water, R a rock,
# * a snowflake, and F the flag.
set rows {
############
#.....=...F#
#..........#
#.ooo...R..#
#.....*....#
#..R.......#
#......=...#
############
}
set T 28
array set ink {
bg #0f1b2d ice #d4ebf7 ice2 #c6e2f2 snow #f6fafd shadow #9dbad2
water #1b3a66 rock #6f7d90 rock2 #9aa8ba red #e5484d flake #3f8fd8
}
canvas .c -width [expr {12 * $T}] -height [expr {8 * $T}] -background $ink(bg) -highlightthickness 0
pack .c
. configure -background $ink(bg)
set y 0
foreach row $rows {
set x 0
foreach c [split $row ""] {
set x0 [expr {$x * $T}]; set y0 [expr {$y * $T}]
set x1 [expr {$x0 + $T}]; set y1 [expr {$y0 + $T}]
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"
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"
}
= {
.c create rectangle [expr {$x0 + 2}] [expr {$y0 + 2}] [expr {$x1 - 2}] [expr {$y1 - 2}] \
-width 0 -fill $ink(snow) -tags "t$x,$y"
}
o {
.c create rectangle $x0 $y0 $x1 $y1 -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"
}
R {
.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"
}
* {
set cx [expr {$x0 + 14}]; set cy [expr {$y0 + 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"
}
}
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"
}
}
incr x
}
incr y
}

Change a character of the rows, for example a . to R, and a rock shows on that square.
- The ice has two colors in a checker:
($x + $y) % 2picks one of them. - A snowbank is two rectangles: the snow, and a strip of shadow at its foot.
- A rock is two ovals: the stone, and a light spot on it.
- The flag is a line for the pole and a
polygonof three points for the cloth.
The game draws each picture of the pond in this way. It has no image file, so a new color or a new size is a change of a number in the code.
Tags name a group of items
A tag is a name on an item, and an item can have many tags. A command that accepts an item also accepts a tag, and then it acts on each item with that tag. The game gives each item of a square the tag t$x,$y, for example t3,4. Then one command changes the whole square. When the skater picks up a snowflake, the proc clearTile deletes the items of the square and draws plain ice (the code):
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
}
A new item goes on top of the items before it. lower t$x,$y sk puts the new ice under the items of the skater (the tag sk), so the skater stays in view. Other tags name a kind: water, rock, flake, and ripple. Each 30 frames, one command moves each ripple of the pond 2 pixels to the side and back (the code):
if {$g(frames) % 30 == 0} { .c move ripple [expr {$g(frames) % 60 ? 2 : -2}] 0 }
Try it in the snippet: add the line .c itemconfigure water -fill #6a1b9a at the end, and the water turns purple.
Next: 2. Slide on the ice.