Games, Fonts & Politics

GameMaker Tips: Creating a ‘lights off’ game

Recently at Chequered Ink we released a game called Chippity Flip, our own take on ‘lights off’ puzzle games, where the player toggles multiple squares at once to make the whole board the same colour. We also released a stripped down version of the project called Turn ‘Em Off, which you can grab on itch.io for $4.99 if you want a head start. However, the process behind the game’s creation is pretty simple when you understand it and the solutions used could come in handy for making other puzzle and board games, especially if you’re new to game development. So here, I’m going to lay out how the project works.

Overview

To create this game we need to understand how to create the grid, populate it with a random puzzle, and have it respond to user interactions. From there you can add extra features like RPG battles, or a timer, or moves counters, but for now I’ll just cover the basics of creating the game as well as some of the reasons behind the method I have used.

Creating & populating the game board

To begin with we will set up some helpful variables to use later on. The reason we are setting these as variables at the beginning of our script, instead of just placing their values throughout the script, is so that we can easily change the game later without having to revise the same numbers in a whole bunch of places.

The variables gridX and gridY indicate where the top-left of your grid will appear on the game screen, gridCols is the number of vertical columns in your grid and gridSquares is the total number of squares (and therefore gridRows can be calculated automatically). cellWidth and cellHeight should be the width and height of the actual objects you intend to use as the “tiles” on your game board, and goodObject and badObject should be the names of those objects. Finally, badClicks is the number of times the game should create new ‘bad’ tiles on the board. More about this in the Inserting ‘bad’ tiles section below.

Then we add the following code to create a ds_grid which will store information about our game board before we construct it using objects:

Using a for loop, we can easily populate the entire grid with “0”, or “good tiles”. Note the use of mod and div to automatically populate the correct ‘row’ and ‘column’ of the grid. This kind of thing scared me when I first started out in game development, but this simple maths is extremely valuable for many different types of project, so it helps to get into the habit of using it. It is possible just to use ds_grid_clear, as shown in the example, to populate the whole grid at the same time. However, learning how to directly populate a grid is useful for all kinds of future applications.

A visual representation of what we have just created is as follows:

Next we need to populate the grid with ‘bad’ tiles to turn this game into a real puzzle.

Inserting ‘bad’ tiles

In a ‘lights off’ style game, some grid patterns are always possible to solve. For example, any puzzle on a 7×7 grid will be possible to complete no matter how hard it looks. However some grids, like a 5×5 grid, are not always possible to solve. If you want your game to be fair you can’t just randomise the tiles on the board since you might create an impossible puzzle.

The perfect way to ensure that your puzzles are always possible to solve is to insert ‘bad’ tiles as if the game were being played in reverse. Start with a clean grid of ‘good’ tiles, then have your code simulate finger presses to create ‘bad’ tiles. If this is done correctly the puzzle will always be solvable because the player can simply touch the tiles in reverse order, although they will obviously be unaware of the order in which the game set up the tiles.

The above code simulates clicks on the grid to set the ‘bad’ squares as if there were an imaginary ‘player’ playing the game in reverse, messing up the board rather than solving the puzzle. Below is an image depicting the result of the code above:

As you can see from the ds_grid on the right above, we already have something that resembles a lights out puzzle. All we have to do now is convert our ds_grid into real, clickable objects. In theory you could just use the ds_grid_* functions to code the entire game, but since the purpose of GameMaker: Studio is to make game development easy and visually appealing, I think it’s best to use real objects at this point.

Creating the clickable grid

Creating the playable grid itself is pretty easy, we just need to cycle through the ds_grid we created, check the value of each cell, and place either a ‘good’ or ‘bad’ tile in the room according to that value.

Our good old friend the for loop makes this possible in just a few lines of code as follows:

Notice how we use gridX, gridY, cellWidth etc. instead of hard-coding values into the script. This means that you can change the way the game board looks and behaves at any time simply by changing the variables at the start of the script. In our example, you should end up with something like this:

And that’s it, the hard part is over! Your game board has been created. All you need to do now is tell your ‘good’ and ‘bad’ objects how to respond to mouse clicks.

Flipping Tiles

Flipping tiles in your ‘lights off’ game is a simple case of toggling the object has been clicked and doing the same to four adjacent tiles that surround it. For example, below is a chunk of code that checks and toggles the tile to the left of a clicked tile:

If a tile exists next to the one that was clicked, its state is checked and it is replaced by the opposite ‘good’ or ‘bad’ tile. This code should be repeated for objects above, below and to the right of the object that was clicked, as well as that object itself. A visual representation of this code is as follows:

Of course, as I said earlier, you could continue to use the ds_grid functions instead and simply draw a visual representation of your grid to the screen. However, if you want to add some polish like fancy tile-flipping animations or rpg battles or some other crazy feature to your game, then it’s a lot easier to detach the puzzle from the ds_grid system and use clickable objects. It’s also easy to check whether the player has completed the puzzle, using the following simple code:

if !(instance_exists([bad tile])) {
// WINNING CODE GOES HERE
}

and that’s how you create your own fully-fledged ‘lights off’ game!

Summary

Learning how ds_grids work, and how they can be populated and read using simple for loops, will come in extremely handy for future projects. The principles set out in this tutorial could be used to create a minesweeper game, a battleship game or any other grid-based board game you can think of. As more of a visual learner it has often been hard for me to make this kind of project a reality, but now I am familiar with the concepts involved I know I can take these ideas into future projects to make them even better. I only hope this tutorial can be helpful in the same way for any beginners out there who find data structures a bit scary!

Help us out

If you like this article and want to give something back, feel free to download Chippity Flip on Android, iOS or Windows Phone and leave us a nice review. If you’re feeling really generous and you want a head start on your ‘lights off’ project, please consider buying Turn ‘Em Off on itch.io.