text stringlengths 0 897 |
|---|
Finally, if we make it to line 109, then it's a valid click. Here we do different things depending on the mouse button used: either reveal the contents of the cell hit, or toggle between grass and flag. |
To test this, you will need to make a mock `reveal` method that takes one parameter. It doesn't need to do anything, but you might have it just print the value of the parameter for testing purposes. (We'll get to the real `reveal` method later.) Then, call `handleClick 0` while you hold the left mouse button down on... |
## Time, Score, and Burying Stuff |
Here are a handful of little functions we'll need. Dividing your program into small functions like this is almost always a good idea. Within the function, you can focus on a problem small enough to understand completely; and elsewhere, where you *use* the function, you can forget about the details and just trust that... |
{caption:"Listing 4 (Drawing time and score, and tracking hidden items.)", number-from: 123} |
```miniscript |
// Function to draw the score and time. |
drawTimeAndScore = function() |
minutes = floor(time/60) |
seconds = ("00" + (floor(time) % 60))[-2:] |
s = "Time: " + minutes + ":" + seconds |
s = s + " " * 6 + "Score: " + ("000000" + score)[-6:] |
text.row = 25; text.column = 38 |
print s |
end function |
drawTimeAndScore |
// Our map of where stuff is hidden. This is stored as a 2D |
// list, indexed by column, row, with values of 0, |
// kCellBomb, or kCellTreasure through kCellTreasure+5. |
hidden = list.init2d(kCols, kRows, 0) |
// Find an empty (grass) spot in the hidden map. |
findEmptyPos = function() |
while true |
p = GridPos.random |
if hidden[p.col][p.row] == 0 then return p |
end while |
end function |
// Populate the hidden map with bombs and treasures. |
buryStuff = function(qtyBombs=40, qtyTreasures=6) |
for i in range(1, qtyBombs) |
p = findEmptyPos |
hidden[p.col][p.row] = kCellBomb |
end for |
for i in range(1, qtyTreasures) |
p = findEmptyPos |
hidden[p.col][p.row] = kCellTreasure + (i % 6) |
end for |
globals.treasuresLeft = qtyTreasures |
end function |
``` |
Most of the `drawTimeAndScore` function is just composing the string we want to draw. Notice the trick being used to add leading zeros to the numbers: add a string of zeros to the front, and then use `[-2:]` to grab just the last two characters of the result (or in the case of score, `[-6:]` to grab the last six). |
The global variable called `hidden`, defined on line 137, is important. It is a two-dimensional list (i.e. list of lists) that keeps track of where all the treasures and bombs are buried. Each entry is either `0`, indicating nothing of interest, `kCellBomb`, indicating a bomb (of course), or the index of one of the t... |
The next two functions work with that `hidden` list. The `findEmptyPos` function uses an infinite loop to just keep trying random locations, until it finds one that is empty. The `buryStuff` function makes use of that to store a number of bombs and treasures. |
To test these functions is pretty easy. When you run the code at this point, you'll now see a time and score (both zero, of course) in the upper-right corner of the screen. You can print out `hidden` and see that it's all zeros, then print it again after calling `buryStuff`, and see that it now contains some nonzeros... |
## Game Over! |
Next we'll need a couple of functions to handle winning or losing the game. You lose the game when you click on a bomb, at which point it explodes, we reveal the positions of all the bombs, and then offer to play again. Winning is simpler, as we just display a happy message, and also offer to replay. |
{caption:"Listing 5 (Functions to handle losing or winning the game.)", number-from: 160} |
```miniscript |
// Show an explosion at the given grid position; end the game. |
doBoom = function(p) |
bombSnd.play |
for i in range(0,3) |
td.setCell p.col, p.row, kCellBomb + i |
wait 0.1 |
end for |
for c in range(0, kCols-1) |
for r in range(0, kRows-1) |
if hidden[c][r] == kCellBomb then |
td.setCell c, r, kCellBomb |
end if |
end for |
end for |
td.setCell p.col, p.row, kCellBomb + 2 |
text.row = 1 |
print "Play again (Y/N)?" |
if key.get.upper == "N" then exit |
run |
end function |
gameWon = function() |
text.row = 2; text.column = 30 |
print "YOU WIN!" |
print "Play again (Y/N)?" |
if key.get.upper == "N" then exit |
run |
end function |
``` |
To test the `gameWon` method is trivial (just call it on the command line). To test `doBoom`, you'll need to pass in a grid position. Remember that you can use `GridPos.make` to construct one of these very easily — for example, `GridPos.make(5,5)`. |
## Final Functions |
Only two more functions are needed in the Treasure Hunt game. The first one is called when you discover a treasure; it updates the display and your score, and checks for successful completion of the game. The other one reveals the contents of any cell, and takes appropriate action. |
{caption:"Listing 6 (`getTreasure` and `reveal` functions.)", number-from: 189} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.