• Register

FineSweeper is a Minesweeper clone with additional features such as level progression, items, achievements, and challenges.

Post news Report RSS Creating a Minesweeper playfield with GameMaker

Since I've received a couple of requests as how to practically code a Minesweeper-like game with Game Maker Studio, I thought I outline a bit of it here.

Posted by on

FineSweeper Title Screen Animation

Before diving in, I think I need to address how I usually do things in GameMaker because it’s heavily code driven. I don’t use much of the visual editors or different Rooms, instead everything gets a long script. Accordingly, all of my objects have just three events, Creation, Step, and Draw, and each of those just calls a corresponding script where all the magic happens. I use this so often, that I even coded a little tool in PureBasic, that I don’t have to set up a new object all the time manually.

So yes, everything an object does happens via a script in the Creation, Step, and Draw event. Additionally, there are invisible object that control the whole game, e.g. an object responsible for always checking the current view state, or an object that catches user input and sets flags in the game-control object, etc.

Also, I am a big fan of code legibility, so I heavily comment stuff, use long and clear variable names and am a big fan of the enumeration feature since it had been introduced to GameMaker Studio. Here’s a little glimpse into the script that's called when the game is started to initialize all the enum-sets:

Setting up the play field


For the play field, I use a grid data structure (ds_grid) which I thought to be pretty neat at the time of coding, but I probably would use a simple 1d-array instead if I did it all over again, but that’s a whole different (and long) story. Anyways, the grid has a certain width and height as defined by the current level. When the ds_grid gets created, all of its cells are set to GRID_TYPE.empty.

Now for the bombs: A random cell gets picked and a bomb is placed by changing GRID_TYPE.empty to GRID_TYPE.bomb until the number of placed bombs matches the amount of bombs to place in a given level, so that's really straight forward.

Once the bombs are randomly set in the grid, the draw-size of the play field in pixels gets calculated to center it nicely.

Then it's time to create the actual tiles as instances of a tile object, obj_tile in my case: For every cell in the grid, a tile instance gets created and some of its variables set, depending on whether the tile is a hidden bomb or not. Each tile-instance also gets the information stored which datas tructure is belongs to, where on the grid it is positioned, and that it should count how many bombs are among its neighbors (for when it gets revealed by the player later).
And, most importantly, if the value in a cell matched GRID_TYPE.bomb, then I set a flag in the tile instance to true (is_bomb = true). That’s all the tile itself needs to know.

Updating the play field

Whenever a tile gets clicked (= opened) by the player, the clicked tile instance knows which grid is belongs to and where in the gird it is located and sets the corresponding value to GRID_TYPE.open.

Of course, if the tile was hiding a bomb and the player stepped on it, it triggers an avalanche in the game’s control object. But again, that’s a whole different story…

Thanks for your interest and if you have questions, feel free to leave a comment!

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: