text stringlengths 0 897 |
|---|
gp = new GridPos |
gp.col = c |
gp.row = r |
return gp |
end function |
// Function to make a random (but in-bounds) GridPos. |
GridPos.random = function() |
return GridPos.make(floor(kCols * rnd), floor(kRows * rnd)) |
end function |
// Function to find the GridPos at a screen position; |
// returns null if out of bounds. |
GridPos.atXY = function(screenPos) |
col = floor((screenPos.x + td.scrollX) / td.cellSize) |
row = floor((screenPos.y + td.scrollY) / td.cellSize) |
if col < 0 or col >= kCols then return null |
if row < 0 or row >= kRows then return null |
return GridPos.make(col, row) |
end function |
// Method to get all in-bounds neighbors of this GridPos |
GridPos.neighbors = function() |
result = [] |
for i in range(-1, 1) |
for j in range(-1, 1) |
if i == 0 and j == 0 then continue |
p = GridPos.make(self.col + i, self.row + j) |
if p.col < 0 or p.col >= kCols then continue |
if p.row < 0 or p.row >= kRows then continue |
result.push p |
end for |
end for |
return result |
end function |
// Method to get the tile cell through this GridPos |
GridPos.cell = function() |
return td.cell(self.col, self.row) |
end function |
``` |
Line 44 declares the `GridPos` class by simply assigning a map with default `col` and `row` properties. To make it easier to create an instance of this class, we have the `GridPos.make` function on line 47. Getting a *random* grid position is also a useful thing to do, especially when burying bombs and treasures, so ... |
Next, the `GridPos.atXY` function at line 61 works out what grid position is at a certain screen position (such as that given by `mouse`). The code here is fairly general; it would work for any tile display `td`, provided that its `cellSize` is a simple number (rather than a `[width, height]` list), and no column or r... |
Lines 70-82 define the `GridPos.neighbors` method. This is the first one intended to be called on a `GridPos` instance, i.e. some particular grid position, rather than on the `GridPos` class itself. Its job is to return a list of all the neighboring in-bounds grid positions. We need that in the game mainly to count ... |
Finally, the `GridPos.cell` method at line 85 is just a shortcut for looking up the cell index from the tile display. This turned out to be a sufficiently common thing in the rest of the code, that it was worth making this little helper method. |
At this point you should not only make sure the code runs, but also try out these `GridPos` functions. You can do this right on the command line: call each one, passing in some reasonable parameter value, and make sure you get a sensible result. |
## Handling mouse clicks |
The next part of the program is a function to handle mouse clicks. We want to treat each tile cell kind of like a button: darken it while the mouse is pressed and inside the cell, but return it to normal appearance fi the mouse is dragged outside the cell. If the mouse is released outside the cell, it should have no ... |
If it's released *inside* the cell, then the effect depends on which mouse button was used. A normal (button 0) click reveals the hidden content of that cell, but a right-click (button 1) toggles a flag. |
{caption:"Listing 3 (`handleClick` function.)", number-from: 89} |
```miniscript |
handleClick = function(btn=0) |
hitPos = GridPos.atXY(mouse) |
if hitPos == null or hitPos.cell < kCellGrass then |
// invalid click; just wait for mouse-up and return |
while mouse.button(btn); yield; end while |
return |
end if |
// Valid click: highlight until mouse-up |
while mouse.button(btn) |
inCell = (GridPos.atXY(mouse) == hitPos) |
if inCell then |
td.setCellTint hitPos.col, hitPos.row, "#DDDDDD" |
else |
td.setCellTint hitPos.col, hitPos.row, color.white |
end if |
yield |
end while |
// If mouse was not released in the hit cell, bail out |
if not inCell then return |
td.setCellTint hitPos.col, hitPos.row, color.white |
// Otherwise, process the click. |
// Left click (btn 0), reveal the cell. |
// Right click (btn 1), toggle flag. |
if btn == 0 then |
reveal hitPos |
else |
if hitPos.cell == kCellFlag then |
td.setCell hitPos.col, hitPos.row, kCellGrass |
else |
td.setCell hitPos.col, hitPos.row, kCellFlag |
end if |
end if |
end function |
``` |
The function begins by making sure we have clicked a valid cell. The click is ignored if it is out of bounds, or if the cell index at the clicked point is less than the grass cell; all the tiles in the tile set before that represent already-revealed locations, and clicking those should have no effect. |
The loop from line 97 to 105 handles the "button" behavior, darkening the cell while the mouse is pressed within its bounds. It also sets a local variable `inCell` to note whether the mouse is actually within the cell originally clicked. So, once the mouse is finally released, line 107 can bail out if it was released... |
D> If you're confused by all this, open up any ordinary program or web page that has a clickable button. Mouse-down on the button, then drag the mouse away before you release. The button should pop back up as soon as you're outside the button, and the button's effect is not triggered. That's all we're doing here. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.