text stringlengths 0 897 |
|---|
]t.x = 200; t.y = 300 |
``` |
This should create a large (due to `Tile.scale`), yellowish (due to `Tile.tint`) letter "E" tile. The `Tile` class includes some simple animation code, too. Let's test it: |
```terminal |
]t.goTo 800, 600 |
]while true; t.update; yield; end while |
``` |
If you see your "E" tile slide up towards the upper-right corner of the screen, then you're ready to press Control-C and move on to the next code listing. If not, go back and check your typing carefully. |
{caption: "Listing 4 (Letter grid storage and set-up).", number-from: 86) |
```miniscript |
// 2D array to keep track of the tiles that belong in each |
// row and column of the grid. Indexed first by column, |
// and within a column indexed bottom-up, so within a column |
// we can easily remove something and the rest shift down. |
grid = list.init2d(kCols, kRows) |
makeSprite = function(letter) |
gfx.fillRect 0, 0, 40, 40, color.clear |
drawBlock letter, 0, 0 |
spr = new Tile |
spr.image = gfx.getImage(0, 0, 40, 40) |
spr.letter = letter |
return spr |
end function |
letterSprites = {} // key: letter; value: Tile |
for code in range("A".code, "Z".code) |
gfx.clear |
c = char(code) |
if c == "Q" then c = "Qu" |
letterSprites[c] = makeSprite(c) |
end for |
gfx.clear |
display(4).mode = displayMode.sprite |
spriteDisp = display(4) |
spriteDisp.clear |
newTile = function(col, row) |
tile = new letterSprites[randomLetter] |
tile.target = {"x":0, "y":0} // make sure tile has its own target map |
spriteDisp.sprites.push tile |
tile.goToGridPos col, row |
tile.x = tile.target.x |
tile.y = tile.target.y + 500 + 50 * rnd + 100*row |
grid[col][row] = tile |
return tile |
end function |
for col in range(0, kCols-1) |
for row in range(0, kRows-1) |
newTile col, row |
end for |
end for |
``` |
This code uses the `list.init2d` method from the `listUtil` module to initialize a two-dimensional array, that is, a list of lists. It's stored in global variable `grid`, and to get to any particular Tile, you index into this twice: `grid[c][r]` gets you the Tile in column `c` and row `r`. Columns start at 0 on the l... |
The `makeSprite` function does what we did by hand earlier: it draws the given letter into the `gfx` PixelDisplay, then grabs that image and assigns it to a freshly created Tile. To save time later on, we do this once for each letter in the loop on lines 102-107. Also notice the special trick we do on line 105, where... |
Lines 110-112 just set up the sprite display, and the `newTile` function starting on line 114 creates a new tile, with a random letter (using the `randomLetter` function we made earlier), and stores it in `grid`. It also tells the tile to go to its grid position, but then on lines 119-120, sets its *current* position ... |
Finally, the nested `for` loops at the end of Listing 4 call `newTile` for every row and column, creating our initial grid of tiles. Our program does not yet have a main loop to make them animate, so after you enter all that and run it, let's test it out by manually entering, on the REPL command line: |
```terminal |
]while true; Tile.updateAll; yield; end while |
``` |
PRINT>This should cause all the tiles to slide neatly down from the top of the screen. The final result should look something like the image on the next page. |
EBOOK>This should cause all the tiles to slide neatly down from the top of the screen. The final result should look something like the image below. |
{width: "80%"} |
 |
{pageBreak} |
And we're going to stop there for today. You've worked hard and made a great start on your most complex program yet — you deserve a break! |
A> **Chapter Review** |
A> - You explored the `englishWords.txt` file included with Mini Micro. |
A> - You learned how to use your coding powers to get answers about large datasets. |
A> - You wrote code to load the word set, draw tiles, turn those into sprites, and animate them into place. |
{chapterHead: "Day 24: Word-Find Game", startingPageNum:293} |
{width: "50%"} |
 |
Q> When to use iterative development? You should use iterative development only on projects that you want to succeed. |
Q>— Martin Fowler (software developer and author) |
A> **Chapter Objectives** |
A> - Finish the word game we started yesterday. |
A> - Get practice at making sprites interact with the mouse. |
A> - Learn techniques for debugging with a Read, Eval, Print Loop. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.