text stringlengths 0 897 |
|---|
This is the first program in this book that does not rely entirely on resources included on Mini Micro's `/sys` disk. Instead, you'll need to download a tile set from the internet, and install it on your `/usr` disk. This will become a very common task as you start building more of your own programs, so it's a good s... |
The tiles for this game are available on OpenGameArt.org, a web site dedicated to sharing artwork for making games. You can find it by searching for "treasure hunt" on the main page there, or by pointing your web browser directly to `https://opengameart.org/content/treasure-hunt-tiles`. When you find the right page, ... |
{width: "50%"} |
 |
Now you need to get this onto your `/usr` disk. You have several options for this: |
{pageBreak} |
- If you are using a *folder* for your user disk rather than a "minidisk" file, you can simply download the image with your web browser, and save it to that folder. |
- If not, you can download the image somewhere else, then use the `file.import` command in Mini Micro to copy it into your `usr` disk. |
- Or you can use `http.get` to download the file directly within Mini Micro, by right-clicking on the file link at OpenGameArt, then pasting it into a command like so: |
```terminal |
]img = http.get("https://opengameart.org/sites/default/files/treasurehunt_0.png") |
]file.saveImage "TreasureHunt.png", img |
``` |
No matter which method you use, at the end you should have a `TreasureHunt.png` file on your user disk, in the folder where you plan to save the program. If needed, review Chapter 14, which discusses how to convert your `/usr` disk between the minidisk (zip file) format and a folder of real files. When you've got tha... |
## Starting the Program |
Now it's time to start on the code! As in past chapters, we'll break this into sections, with a bit of explanation after each one. But we won't lead you explicitly through testing code anymore. You should still test as you go, though — *at least* run the program to make sure it doesn't generate any errors right away... |
{caption:"Listing 1 (Program set-up).", number-from: 1} |
```miniscript |
// Treasure Hunt! |
// A Minesweeper-type game with treasures. |
import "listUtil" |
// Constants |
kCols = 20 |
kRows = 15 |
kCellGrass = 16 |
kCellFlag = 17 |
kCellBomb = 18 |
kCellTreasure = 10 // (first treasure on sand background) |
kCellTransTreasure = 26 // (first one on transparent background) |
// Set up the displays |
clear |
display(4).mode = displayMode.tile |
td = display(4) |
td.tileSet = file.loadImage("TreasureHunt.png") |
td.tileSetTileSize = 32 |
td.cellSize = 32 |
td.extent = [kCols, kRows] |
td.clear kCellGrass |
td.scrollX = -80; td.scrollY = -80 |
gold = "#FFDD52FF" |
gfx.color = gold |
gfx.print "Treasures Found", 780, 540, gold, "small" |
gfx.line 780-4, 540-4, 780+"Treasures Found".len * 9 + 4, 540-4 |
treasureListY = 540 - 48 |
treasureListX = 780 |
text.color = gold |
text.row = 25; print "TREASURE HUNT!" |
score = 0 |
treasuresLeft = 0 |
// Load sounds |
treasureSnd = file.loadSound("/sys/sounds/cha-ching.wav") |
bombSnd = file.loadSound("/sys/sounds/airburst.wav") |
``` |
Here's a pretty typical first page of a Mini Micro game. After a header comment, we import the `listUtil` module on line 4. We'll be using the `list.init2d` method this module adds to conveniently create a 2D array to keep track of the hidden treasures and bombs. |
Then we define a bunch of constants on lines 7-13. `kCols` and `kRows` define the size of the play area, and the other constants define the indexes of various tiles in the tile map, so we're not stuck looking at mysterious numbers like `17` for the rest of the program. |
The next block of lines, from 16 through 24, set up a `TileDisplay` and name it `td`, just as we saw in the previous chapter. Then we set up the graphics display, with a nice gold color for the treasure list, and use the same color on the text display. So this game is using three of Mini Micro's eight displays: |
- a `PixelDisplay` called `gfx` in layer 5, for the treasure list; |
- a `TileDisplay` called `td` in layer 4, for the game board; and |
- a `TextDisplay` called `text` in layer 3, for the time and score. |
Note that `gfx` and `text` are the standard layers set up by the `clear` command, but there is no standard `TileDisplay`, which is why we have to explicitly create one on lines 17-18. |
Finally, on lines 39-40, we load a couple of sounds we'll need later. |
You should run at this point, and make sure you don't see any errors. You should also see a field of green tiles, a blank "Treasures Found" list on the right, and the "TREASURE HUNT!" title at the top. |
D> Also remember to save your work often! |
## The `GridPos` Class |
In this program we are going to be dealing with grid positions a lot. We'll have code that figures out what grid position the mouse is over, what secrets are buried at that grid position, which grid positions are neighboring this one, and so on. We could represent such grid positions as an `[x, y]` list, but a little... |
{caption:"Listing 2 (The `GridPos` class.)", number-from: 42} |
```miniscript |
// Define a "GridPos" class to represent a grid |
// position (i.e. column, row). |
GridPos = {"col":0, "row":0} |
// Function to make a GridPos at a given column and row |
GridPos.make = function(c,r) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.