text
stringlengths
0
897
## Sprites vs. Tiles
Remember those letter tiles in the word game of Chapters 23 and 24? Those were *sprites*, drawn in a `SpriteDisplay`, and for good reason: we wanted them to fall smoothly into position, and we might even want to add a little bit of rotation to each one to make them look more natural. That sort of fine control is not ...
Why would you ever use a `TileDisplay`, then? There are two chief advantages. First, a `TileDisplay` is somewhat easier to manage, since you need only assign a tile number to any cell of the display, and the graphics on screen automatically update. You'll see how easy that is by the end of the chapter. Second, whil...
## The `TileDisplay` class
When you set a display's `mode` to `displayMode.tile`, it changes the type of that display to `TileDisplay`, which means it gets the following assignable properties:
{caption:"Properties of the `TileDisplay` class (where `td` is any `TileDisplay` reference). All these may be both read and assigned to. The `extent`, `tileSetTileSize`, `cellSize`, and `overlap` properties may be either a single number applied to both X and Y, or an `[x,y]` list."}
|`td.extent`|[columns, rows] display size|
|`td.tileSet`|`Image` containing tiles to draw from|
|`td.tileSetTileSize`|size of tiles in `tileSet`|
|`td.cellSize`|size of tiles (cells) on screen|
|`td.overlap`|cell overlap on screen, in pixels|
|`td.oddRowOffset`|amount to shift odd rows horizontally; use 0.5 for hex rows|
|`td.oddColOffset`|amount to shift odd columns vertically; use 0.5 for hex columns|
|`td.scrollX`|horizontal scroll amount, in pixels|
|`td.scrollY`|vertical scroll amount, in pixels|
{pageBreak}
And it gets the following methods (you can only call these, not assign to them):
{caption:"Methods of the `TileDisplay` class."}
|`td.clear` *toIndex*|set all tiles to null (the default) or the given index|
|`td.cell(x,y)|get the tile index of cell `x`,`y`|
|`td.setCell x, y, idx|set the tile index of cell `x`,`y` to `idx`|
|`td.cellTint(x,y)|get the tint color of cell `x`,`y`|
|`td.setCellTint x, y, c|set the tint of cell `x`,`y` to color `c`|
That seems like a lot, but as we'll see shortly, they fall into just a couple of groups: things for configuring the TileDisplay at setup time, and things for controlling the content (the tiles shown and their tint color) as your program runs.
## Basic Configuration
EBOOK>It's time to put hands to keyboard and start using this thing! You may recall from Day 19 the default display types that Mini Micro gets at startup (or when you use the `clear` command). These include a PixelDisplay (display 5), a SpriteDisplay (4), and of course a TextDisplay (3). They do not, however, includ...
EBOOK>So the first thing to do is create such a display! You can put it in any layer you want, but for this chapter, let's repurpose layer 5, as we won't be doing any pixel drawing anyway. Fire up Mini Micro, and let's get started.
{width: "50%", position:"top"}
![The tile set image `/sys/pics/TileShapes.png`, with tile index numbers added.](SimpleTileShapes.png)
PRINT>It's time to put hands to keyboard and start using this thing! You may recall from Day 19 the default display types that Mini Micro gets at startup (or when you use the `clear` command). These include a PixelDisplay (display 5), a SpriteDisplay (4), and of course a TextDisplay (3). They do not, however, includ...
PRINT>So the first thing to do is create such a display! You can put it in any layer you want, but for this chapter, let's repurpose layer 5, as we won't be doing any pixel drawing anyway. Fire up Mini Micro, and let's get started.
```terminal
]display(5).mode = displayMode.tile
]td = display(5)
```
Nothing much appears to happen at this point, but invisibly, display 5 has transmogrified from a pixel display into a tile display. We've also assigned it to a variable called `td` so we can refer to it more easily. Now let's load a tile set (and also use the `view` command so you can get a glimpse of what it looks l...
```terminal
]td.tileSet = file.loadImage("/sys/pics/TileShapes.png")
]view td.tileSet
```
The tile set we've loaded here is a selection of eight different simple shapes, in eight different colors. The image is 512 by 512 pixels, and since there are 8 rows and columns, this means the tile size (in the tile set) is 64 pixels wide and tall. This is the next thing we have to do: tell the tile display how big ...
```terminal
]td.tileSetTileSize = 64
```
We used just a simple number here (64) because the tiles are square. That's usually the case, but if you ever find or make a tile set with non-square tiles, you can specify the width and height in a list like `[48,32]`.
Finally we're ready to get some tiles onto the display! The easiest way to do that is with the `.clear` method, which takes a *tile index* to know which tile it should clear everything to.
tile index
: a number which identifies a tile in a tile set, starting with 0 at the top-left, and counting left-to-right, then top-to-bottom
The figure above shows the tile indexes for this tile set. Let's clear the tile display to the dark blue circle, which is tile index 20.
```terminal
]td.clear 20
```
The result should look something like this:
{width:"75%"}
![Initial setup of a TileDisplay.](TileDisplay1.png)
Now we can see the tile display: it's that grid of blue circles! Check your understanding of tile index by trying a few other values. Can you fill the display with red diamonds? How about purple hexagons?
You probably noticed that the tile display does not fill the whole screen. There 12 columns and 7 rows of cells, which is called the *extent* of the display.
extent
: how many columns and rows a tile display has
In fact if you type `td.extent` at the prompt, it will tell you `[12, 7]`. You can change it by simply assigning a new pair of numbers. Note that if you make the tile set *smaller*, you see the change right away; but if you make it bigger, it's not so obvious, because the new rows and columns are filled in with empty...
```terminal
]td.extent = [8,6]
]td.extent = [15,10]
]td.clear 20
```
Now the screen should be full of blue circles. But that's a function of the extent of the tile display, and also the cell size. The default cell size is 64, which happens to be the same as our tile size in this case. But you can easily change it. Explore: