Have you ever played Candy Crush? In this post we see how to create the game of Candy Crush in Excel using VBA macros. It’s all about moving and copy or cut shapes on the worksheet. The game in Excel is limited compared to the original Candy Crush game, but can be leveraged to have the full functionality also in Excel. The rules are basically the same, and the goal of Excel Candy Crush is to collect as many points as possible to hit the target with the given number of moves allowed.
Candy Crush Main Board
In this Excel version of Candy Crush, the board spans 6x6 cells within range D4:I9. The column width and row height are adjusted to fit the shapes representing each of the candies, and to try to look as close as possible to the original Candy Crush game.
Each cell has a given value that corresponds to the type of candy added. That’s key in the routine to fill the board (the gaps are cells with no values), and to check the possible combinations of candies later (see next section).
Additionally, we see the target, moves, and points, on the left. The goal of Excel Candy Crush is to collect as many points as possible to hit the target with the given number of moves allowed. On the right, we have the start and reset buttons. Reset is used in case you want to continue the game but there aren’t any possible combinations to move forward. It is altogether a bit different than the original Candy Crush game, but tries to resemble and follow the rules as much as possible.
Types of Candies
There are six basic types of candies, which look exactly as in the original Candy Crush game (as they are pictures from the game). Each of them corresponds to an index that is the value in the cell they are added. That index actually corresponds to the row number in the worksheet with the pictures of each candy (a number from 1 to 6 for each type).
There are two types of special candies: striped candies (with either horizontal or vertical stripes), and the sugar bomb multi-color candy. The value of cells with stripped candies has two numbers. The first number is the index for that type of candy (also the row number), and that’s a number between 1 and 6, as with basic candies. The second number is either 7 or 8 for horizontal or vertical stripes respectively. The multi-color candy has the value of 9.
There are some other pictures or shapes for points and selection borders in that same worksheet. All the shapes are copied from that worksheet to the main board while playing.
Fill vs Check the Board
These two macros are the pillars of Excel Candy Crush. The first one fills the gaps with candies from the bottom to the top of the board. When starting a new game, it will just fill all the cells in the board. But while playing, it fills gaps or empty cells, where a group or combination of candies have been removed. That happens in the second macro (see later). First, it checks for cells with candies above the gaps, and then moves those down. That’s all achieved using either Copy or Cut from the source cell. If there are no candies above, it adds a new candy randomly. Below is part of the code to add a new candy. It gets a random number from 1 to 6, which represents the type of candy and the row number, and copies the shape for that row on the worksheet “pics” to the target cell.
Then it changes the name of the shapes that have been moved or copied to the board. Each shape on the board has a 2-digit name that corresponds to the row and column numbers that they occupied. Furthermore, it assigns a macro to the shape, thus making it clickable. This is important when selecting and moving shapes while playing (see next section). Here’s an example of the code to change the name and assign a macro to the shape with OnAction.
The other macro checks the moves, either user moves or moves as a result of the elimination of groups and the cascade of new shapes filling the gaps. It checks by columns and rows for combinations of 5, 4, and 3 candies (in that order), looking at the values in the cells. When a group is found, it deletes the shapes and values in those cells, and calls again the fill macro. While deleting the shapes, it adds another picture with the points obtained in the move that disappear short after. The process repeats and continues for as long as there are gaps to fill or combinations of candies to eliminate.
Select and Move a Candy
Every candy added to the board is associated to a macro. Thus, clicking on any shape will trigger a macro to select or move the candy. If no candy is selected (value of variable ShpSel is False), it selects the candy and adds braces (one of the auto shapes in Excel) around the candy to show it as selected.
If a candy is selected, clicking on any other candy triggers the macro that checks if the move is allowed, and if so, then triggers the macro that checks for combinations of candies (explained earlier). If the move does not lead to any combination of candies, the two shapes swap positions back to the original places. In this process, the name of the shapes plays a key role and needs to be updated every time a candy is moved to other position. The macro gets the name of the shape that was clicked using the Caller property of the Application object. The shape with that name is selected calling the procedure GetCellSel (see below).
It is also here where the macro checks if any of the shapes involved in the move are the multi-color candy, or a swap between two stripped candies. In that case, it runs other routines to create the corresponding cascade of moves and update the points accordingly.
And that was an overview of the main components of the Candy Crush game in Excel. Download the file following the link below and check the full code or have some fun playing Excel Candy Crush.
No comments:
Post a Comment