In the first part of this tutorial, we created a table with 12 cards: shuffled, faced down and nicely positioned. In this second part of this tutorial, we are going to finish off and implement the game logic.
Turn a card
We are going to start by applying some click actions to our obj_card_facedown object.
- Open the obj_card_facedown object.
- Click Add Event button and choose Mouse > Left Pressed.
- From the main 1 tab, find the Change Sprite icon and drag it to the Action listbox.
- In the pop-window, change the Sprite to spr_facedown_card_larger and click OK.
- Add the Mouse > Left Released event to the obj_card_facedown.
- Apply the Execute a piece of code as an action to this event.
- Enter these lines of code to this action:
if(gameState == gamestate_NoUpcards || gameState == gamestate_FirstUpcard)
instance_change(obj_card_facedown_shrink, false);
else
instance_change(obj_card_facedown, false);
Then we are going to apply a action to the obj_card_facedown_shrink object.
- Add the Other > Animation End event to the obj_card_facedown_shrink.
- From the main 1 tab, find the Change instance icon and drag it to the Action listbox.
- In the pop-window, change the Change into to obj_card_faceup_empty_grow and click OK.
If we now Run the game, we see that when we click a face-down card, it first gets larger, then shrinks and disappears, then an empty face-up card appears. This should only occur if one or none of the cards on the table are faced up, but this game-state logic is not implemented yet.
Lets create a new script, that will contain the logic, when a card is turned face-up.
- From the Resources menu, select Create Script.
- Change the Name to script_turnCard.
- Enter these lines of code to this script:
// If no card is flipped face-up, then set this card as the first upcard
if (gameState == gamestate_NoUpcards)
{
upcard1 = id;
gameState = gamestate_FirstUpcard;
}
// Else this is the second card to be flipped face-up
else if (gameState == gamestate_FirstUpcard)
{
upcard2 = id;
gameState = gamestate_SecondUpcard;
}
// Find what position on the table this card is in
var pos, i, j, tmp;
pos = 0;
tmp = 0;
for (i = 0; i < horizontalPositions; i += 1)
{
for (j = 0; j < verticalPositions; j += 1)
{
if (table[i,j] == id)
pos = tmp;
tmp +=1;
}
}
// Then find what card value is in the shuffled list at that position
var card;
card = ds_list_find_value(cardsShuffled, pos);
instance_change(obj_card_facedup,false);
image_speed = 0;
switch (card)
{
case "A":
image_index = 0;
break;
case "B":
image_index = 1;
break;
case "C":
image_index = 2;
break;
case "D":
image_index = 3;
break;
case "E":
image_index = 4;
break;
case "F":
image_index = 5;
break;
}
// Then set the value of the upcard
if (upcardValue1 == "")
upcardValue1 = card;
else if (upcardValue2 == "")
upcardValue2 = card;
Then we are going to apply a action to the obj_card_faceup_empty_grow object.
- Add the Other > Animation End event to the obj_card_faceup_empty_grow .
- Apply the Execute a piece of code as an action to this event.
- Enter this line of code to this action:
script_turnCard();
If we now Run the game, we see that when we click a face-down card, that the right face-up card appears. But at the moment we can only select two cards – we are missing the logic that checks if the cards match and the logic that flips the cards face-down again.
Game loop
Lets create a new script, that will contain the logic, that checks the game-state at any time – the game-loop.
- From the Resources menu, select Create Script.
- Change the Name to script_gameLoop.
- Enter these lines of code to this script:
switch(gameState)
{
case gamestate_NoUpcards:
// If there are no cards left on the table, then switch state to gamestate_NoCardsLeftToFlip
if (!instance_exists(obj_card_facedown))
gameState = gamestate_NoCardsLeftToFlip;
break;
case gamestate_FirstUpcard:
break;
case gamestate_SecondUpcard:
// If the second card has been flipped face-up, then start count down
countDown = 50;
gameState = gamestate_CountingDown;
break;
case gamestate_CountingDown:
// If count down has completed
if (countDown == 0)
{
// Increment tries
tries += 1;
// Check to see if upcards match
if(upcardValue1 == upcardValue2)
{
gameState = gamestate_ThereWasAMatch;
exit;
}
upcardValue1 = "";
upcardValue2 = "";
with (obj_card_facedup)
{
instance_change(obj_card_faceup_empty_shrink, false);
image_speed = 1;
}
gameState = gamestate_NoUpcards;
}
// Else if count down has not completed, then continue count down
else
countDown -= 1;
break;
case gamestate_ThereWasAMatch:
// If there was a match, then remove these cards from the table
upcardValue1 = "";
upcardValue2 = "";
with (obj_card_facedup)
{
instance_destroy();
}
gameState = gamestate_NoUpcards;
break;
case gamestate_NoCardsLeftToFlip:
// If there are no cards left on the table, then show number of tries
// and display the "Try again" button
instance_create((room_width / 2), (room_height / 2), obj_result);
instance_create((room_width / 2), (room_height / 2)+125, obj_tryAgain)
break;
}
- Now open the obj_startGame object.
- Add the Step > Step event to the obj_startGame.
- Apply the Execute a piece of code as an action to this event.
- Enter this line of code to this action:
script_gameLoop();
- Add the Other > Animation End event to the obj_card_faceup_empty_shrink .
- From the main 1 tab, find the Change instance icon and drag it to the Action listbox.
- In the pop-window, change the Change into to obj_card_facedown and click OK.
If we now Run the game, we see that all of the game logic is almost in place: we flip two cards face-up, check if they match and flip cards face-down again. Cards that match are removed from the table.
Finishing touches
We also keep track of how many times we flip the cards, before all cards have been removed from the table. But we need to display this information and also implement the “Try again” button.
- From the Resources menu, select Create Font.
- Change the Name to font0.
- Choose a font-type. E.g. Arno Pro Display.
- Set Size to 30 and make it Bold.
- Add the Create event to the obj_result.
- Apply the Execute a piece of code as an action to this event.
- Enter these lines of code to this action:
draw_set_halign(fa_center);
draw_set_color(c_fuchsia);
draw_set_font(font0);
- Add the Draw > Draw event to the obj_result.
- Apply the Execute a piece of code as an action to this event.
- Enter these lines of code to this action:
draw_self();
draw_text(x, y-20, "You matched all cards in " + string(tries) + " tries!");
- Add the Mouse > Left Released event to the obj_tryAgain.
- Apply the Execute a piece of code as an action to this event.
- Enter these lines of code to this action:
game_restart();
And that´s all there is to it. You now have a simple memory game to play with. I hope you have enjoyed this tutorial and found it valuable.
We start by loading the image of the table as a background resource.
The next thing we want to do is create a room and display the background image inside of it.
Then there are only two more sprites to add.
Game Maker: Studio uses events and actions to specify how instances of objects should behave in the game. When a new instance of obj_startGame is created, we want to run our script.
We now want to make a little change to the obj_card_facedown object.