text
stringlengths
0
897
for row in range(0, kRows-1)
tile = grid[col][row]
tile.selected = false
tile.tint = Tile.tint
end for
end for
overlay.clear
globals.lastTile = null
end function
```
Let's discuss how these functions work. The `clearUsedTiles` function iterates over the columns of the grid. For each column, it iterates over the rows twice. First it goes top-down, deleting any selected tiles from the list of tiles in that column, and pushing an empty entry (`null`) onto the end of the list. Then...
The `deselectTiles` function is simpler. It just iterates over all rows and columns, turning off the `selected` flag, and setting the tint back to the standard `Tile` tint. Then it clears the `overlay` display where the tracing line is drawn, and while we're at it, also clears the global `lastTile` variable that keep...
After inserting that code, run the program again, and then use the mouse to trace out some letters. Then enter `deselectTiles` at the command prompt. The selected tiles should go back to normal, and the tracing overlay should disappear.
Then run the program again, and again select a sequence of tiles. This time call `clearUsedTiles`, and you should see the selected tiles disappear. The surrounding tiles (and new tiles, currently off the top of the screen) have had their targets set, but they need an animation loop in order to reach those targets. L...
```terminal
]while true; Tile.updateAll; yield; end while
```
This should cause the tiles to slide down into place. Press Control-C to break the loop once that is done.
## Adding a score and history
Now let's add some more functions to handle when a word has been entered. We want to draw the word near the top of the screen, above the tile grid; and then score it based on whether it's in the dictionary, and how long it is. Long words are much harder to find than short ones, so we want to really reward the player ...
In addition to the total score, we'll also keep a history of all the words found in the course of the game. This will be displayed on the left side of the screen, so when you find some really amazing word like EVACUATE, you can show people even if it's not the last word in the game.
Again insert the new code (in Listing 3 this time) above your existing test code.
{caption:"Listing 3 (Word drawing and scoring functions).", number-from: 195}
```miniscript
drawWord = function(word, valid)
if valid then c = color.aqua else c = color.teal
gfx.clear
gfx.print word, 480 - word.len/2*20.5, 550, c, "large"
end function
score = 0
wordsFound = [] // each element will be [word, points]
scoreWord = function(word)
valid = words.contains(word)
drawWord word, valid
if not valid then return false
points = 2 ^ word.len
wordsFound.insert 0, [word, points]
text.row = 21
for w in wordsFound
print (w[0] + " "*11)[:11] + (" "*5 + w[1])[-5:]
if text.row < 2 then break
end for
globals.score = score + points
text.row = 25
print "SCORE: " + score
return true
end function
```
There are two functions in this chunk as well. The `drawWord` function simply sets a color based on whether the word is valid, then clears the `gfx` PixelDisplay, and uses `gfx.print` to print the word in a large font.
The `scoreWord` function is responsible for determining whether the word is valid, by checking whether our `words` list contains it. (Recall that `contains` is a method added to the list type by the `listUtil` module.) It calls that `drawWord` function, and then if the word is not valid, bails out on line 206. Other...
To test this, go down to the last line of your test code. If we've stuck together on all the blank lines and whatnot, it'll be line 227. In any case, replace the `print` statement at the end of your test code with:
{number-from: 227}
```miniscript
scoreWord wordInProgress
```
Now run the program. First select a bunch of gibberish characters that do not make a valid word. You should see the word drawn above the tiles, but no score appear. Now run it again, but this time find some valid word. You should see the word drawn in a brighter color, a score appear in the top-left corner of the s...
## Wrapping up
Almost done! To finish the game, we're going to finally add that `updateTime` function, which puts a timer in the upper-right corner of the screen. And finally, as in most games, we'll have a main loop. Delete that test code at the end of your program, and enter the following.
{caption:"Listing 4 (Timer and main loop).", number-from: 220}
```miniscript
endTime = time + 120
updateTime = function()
timeLeft = ceil(endTime - time)
text.row = 25
text.column = 58
if timeLeft <= 0 then
print "TIME'S UP!"
else
mins = floor(timeLeft/60)
secs = ("00" + (timeLeft % 60))[-2:]
print "TIME: " + mins + ":" + secs
end if
end function
lastTile = null
while true
while Tile.updateAll; updateTime; yield; end while
if time > endTime then break
while not mouse.button; updateTime; yield; end while
tileHit = tileUnderMouse