Ultra-Kit: MINEKIT

 

 

 

 

[NOTE: Before you can use this kit, you must first copy
its source listing file into your project's "Library" folder.]

 

 


 

The MINEKIT construction kit lets you create your own "Minesweeper" types of games that run in a 640x480 window.

 

But before you begin, first make a print-out of these instructions (by clicking on the word 'File' in the menu bar, and selecting the 'Print...' option).  You will want to follow that printed-out version as we work through the programming exercises.

 

 

How to Use the MINEKIT Construction Kit


(For a brief description of what each task in the kit does, click here.)

 

The MINEKIT construction kit employs a coordinate system in which the screen is divided into an array of tiles (1-40 horizontally, 1-25 vertically) as shown in the following illustration:

 

 

Location (1,1) is the tile in the upper-left corner of the screen, and location (40,25) is the tile in the lower-right corner of the screen.  Unless otherwise specified:

 

All the tasks in the MINEKIT construction kit use tile co-

ordinates (40x25) rather than pixel coordinates (640x480).

 

[If your program ever needs to convert between tile coordinates and pixel coordinates, the kit contains two conversion tasks: "tiletopixel" and "pixeltotile".]

 

Before we create an actual game, let's first play with some of the kit's tasks to get familiar with what they do.

 

The "victory" task (which can be used to let the player know that he/she has won) merely plays a short bugle call.  Let's test it out by writing the following program:

 

     Ultra-SiMPLE []

     Call victory @

     Append L: kit

 

 

The "drawfield" task draws the actual mine field (of a specified size and at a specified location) on the screen.  Let's test it out by writing the following program:

 

     Ultra-SiMPLE []

     Call draw field (12, 11, 28, 21) @

     Append L: kit

 

When you run the above program, an image of the playing field should appear on your screen:

 

 

Go ahead and change some of the calling parameter values in the program to see how they affect the size, location, and shape of the playing field.

 

Next, let's plant some hidden mines under a couple of the playing field's tiles.  The "plantmine" (and "removemine") tasks allow you to plant (or remove) a mine (from) under a specified tile.  Let's plant one mine at (21,15) and another one at (17,19):

 

     Ultra-SiMPLE []

     Call draw field (12, 11, 28, 21) @

     Call plant mine (21, 15) @

     Call plant mine (17, 19) @

     Append L: kit

 

If try running the above program you will, of course, see neither of the mines.  (They're hidden, remember?)  But just to convince you that they're really there, you can temporarily add a "Call show mines @" statement to the program:

 

     Ultra-SiMPLE []

     Call draw field (12, 11, 28, 21) @

     Call plant mine (21, 15) @

     Call plant mine (17, 19) @

     Call show mines @

     Append L: kit

 

 

You can continue to plant even more of your own mines, if you so choose.  But an easier way is to invoke the "plantmines" task, which instructs the computer to randomly plant the mines for you:

 

     Ultra-SiMPLE []

     Call draw field (12, 11, 28, 21) @

     Call plant mine (5) @

     Call show mines @

     Append L: kit

 

We will use the "plantmines" task later on when we start to create an actual game.  But for now, we will continue using the "plantmine" (without the "s") task so that we know the locations of our mines.

 

The "reveal" task "uncovers" a specified tile by displaying the mine proximity count for the tile.  The mine proximity count is the number of neighboring tiles (0-8) that are covering a mine.  If you run the following program:

 

     Ultra-SiMPLE []

     Call draw field (12, 11, 28, 21) @

     Call plant mine (21, 15) @

     Call plant mine (17, 19) @

     Call reveal (15, 14, 0) @

     Call reveal (20, 15, 0) @

     Call reveal (17, 19, 0) @

     Append L: kit

 

you will see the three different types of "revelations" that can occur.  In this case:

 

        *  The tile at (15,14) has a proximity count of zero, so an empty cell is

            revealed.

 

        *  The tile at (20,15) has a proximity count of one [because of the mine

            we planted right next to it at (21,15)], so a "1" is revealed.

 

        *  The tile at (17,19) had a mine planted under it, so a mine on a red

            background is revealed.

 

 

The "togglemarker" task can be used to toggle a mine marker on any tile suspected of hiding a mine, as can be observed in the following brief program:

 

     Ultra-SiMPLE []

     Call draw field (12, 11, 28, 21) @

     Do k = 1 To 10

        Call toggle marker (14, 12) @

        Call delay (500)

     Loop

     Append L: kit

 

(Notice that a tile can be marked even if it is not actually covering a mine.)

 

The only other Minekit task with which we need to become familiar (at least for now) is the "waitmouse" task.  "Waitmouse" is very similar to the "readmouse" task in the system library except that "waitmouse" waits for a mouse button to be pushed (instead of returning immediately), and the (x,y) values are returned in tile coordinates (rather than in pixel coordinates).

 

 

Ok, Let's Start Making a Game!

 

We'll begin by typing in the following listing:

 

     Ultra-SiMPLE []

     Int x, y, z

     draw field (12, 11, 28, 21) @

     plant mines (5) @

     Do

        wait mouse (x, y, z) @

        If (z=1) reveal (x, y, 0) @

        If (z=2) toggle marker (x, y) @

     Loop

     Append L: kit

 

Pressing the left mouse button reveals the tile; pressing the right mouse button marks a tile.  Pressing the 'Esc' key terminates the program.  (The "waitmouse" task automatically tests for the 'Esc' key.)

 

The above listing, as it stands, is not really a game.  It's only purpose is to illustrate how some of the modules can be used together to create the beginnings of a game.  However it's only a "stone's throw" away from becoming at least a mini-game.  All we need to do are:

 

          *  call the system library's "randomize" task (so that we don't always

              plant the mines under the same tiles),

 

          *  test for end-of-game,

 

          *  reward the player for winning,

 

          *  explode the mine when the player loses,

 

          *  and reveal (at the end of the game) where all the mines were planted.

 

 

A "Mini-game" Program

 

All five of these remaining features have been implemented in the "mini-game" listing shown below:

 

     Ultra-SiMPLE []

     Int x, y, z

     randomize

     draw field (12, 11, 28, 21) @

     plant mines (5) @

     Do

        wait mouse (x, y, z) @

        If (z=1)

           reveal (x, y, z) @

           If (z) Break

        Endif

        If (z=2) toggle marker (x, y) @

        test for done (z) @

        If z

           victory @

           quit

        Endif

     Loop

     tile to pixel (x, y) @

     explode (x, y) @

     show mines @

     Append L: kit

 

(The "testfordone" task looks to see if all tiles have been either successfully uncovered, or else correctly marked as mines.)

 

There are several other interesting Minekit tasks (such as "autoproc" and "drawbutton") that we have not discussed.   See if you can figure out how to use them on your own.

 

 

The MINE FIELD Game

 

The MINE FIELD game is one that we created using the MINEKIT construction kit.  (When you run the program, see if you can figure what the "M" and "S" buttons do!)

 

_________________________________________________

 

www.simplecodeworks.com