Discuss Scratch

mr-canoehead
New Scratcher
2 posts

Kaboom! (a clone of the Windows Minesweeper game)

If you've ever played Windows Minesweeper this game will look familiar. The goal is to flag all the mines on the gameboard without clicking on any.
Here's a link to the project:
Kaboom! (a Minesweeper clone)

Instructions
  • to clear a square, click on it with your mouse.

    if a cleared square shows a number, that's how many of its 8 neighbors contain a mine.

    to flag a square, press the Space key while your mouse pointer is over the square

    to remove a flag, either click it with your mouse or press the Space key while your mouse pointer is over the square.

    when all the mines are flagged, and all non-mine squares are cleared, the game is won and the timer stops

    click on the face to start a new game.



Building Kaboom!
My goal (aside from learning Scratch) was to replicate as closely as possible the look and gameplay of the original Minesweeper game.

Graphics
The graphics were straightforward, I just copied them from the original program, adjusted them a bit, and uploaded them as costumes for the appropriate sprites. I then drew a gameboard that fit the dimensions of the Scratch stage.

Timer/Flag counter
The timer and flag counter were a little tricky since they appear as 3 digit 7 segment displays. I accomplished this by isolating the digits of the numbers, changing the costume to the appropriate 7 segment display digit, moving to the appropriate place on the board, and stamping.
One feature I added was to pause the timer while empty squares are being cleared. This was accomplished by tracking the total ‘pause’ time and subtracting it from the total time before updating the display.

Clearing empty squares
This was really the crux of the project; in the original Minesweeper when you click on an empty square it automatically clears any surrounding empty squares. This eliminates the mindless and annoying task of having to click on dozens of empty squares to clear them manually.
In my first attempt I simply kept a single list of squares to be cleared and added to that list as more empty neighbors were found. This approach worked in that all the empty squares were cleared, but during gameplay it looked terrible: a strip of clearing squares zipping back and forth across the gameboard. In the original Minesweeper, the empty squares are cleared in a nice concentric ring pattern, expanding outward from the first cleared square.
I finally came up with a solution, which was to keep two lists of squares to be cleared:
  • the list of squares to be cleared in the current ‘pass’
    the next list of squares to be cleared (newly found empty neighbors are added to this list)
The ‘current’ list gets cleared, then the ‘next’ list is copied to the ‘current’ list. This process is repeated until the lists are empty.

Blocking input
At certain points during gameplay it is necessary to prevent the player from clicking on things. For example, when squares are being cleared automatically we don't want the player clicking on other unrelated squares.
I managed to block input by creating a screen overlay that is ghosted at 99%. To block input this overlay is shown, and to allow input it is hidden.

Data structures
In order to keep track of the gameboard I initially created a list of strings to emulate a two-dimensional array. This meant jumping through hoops in order to update the value of a square, since there is no ‘replace letter’ function in Scratch. After the game was written I changed the data structure to a single list (and re-wrote some of the code of course). This made reading/writing square values much more straightforward. While this change didn't improve performance much, the code is now much more readable and maintainable.

Bugs
I've managed to resolve all the bugs I've found except for one: when you first load the project, if you switch to the editor, change the speed to Turbo, and go back to the project page, odd things happen when you start playing. Random cells get cleared, sometimes the input blocker is shown (without ghosting). To resolve this issue, just stop the project and click the green flag again. Everything should function correctly.

Last edited by mr-canoehead (Jan. 13, 2016 19:47:52)

Powered by DjangoBB