Memo game

Technical details

Engine: Unity
Version: Unity 2020.3.30f1
Genre: Memory
Assets: DALL-E
Itch.io: Link

Description

To have the main functionality, that is flipping the card, I have created a script for it, which is presented below. Then in the FlipCard script I am calling the setter with changing its current value to the negative of the current one. Thanks to making it this way I can call this few lines of code as Flip and use it to flip the card when player clicks it as well as to flip it back.

private bool isFlipped = false;
public bool IsFlipped
{
    get => isFlipped;
    set
    {
        isFlipped = value;
        transform.GetChild(0).gameObject.SetActive(!value);
        transform.GetChild(1).gameObject.SetActive(value);
    }
}



Below is an image of perhaps the most difficult part, creating cards that cover the screen symmetrically with respect to a point in the center of the screen.

I had to make a lot of calculations related to possible positions. Eventually comming up with below idea:

float screenWidth = Screen.width;
float screenHeight = Screen.height;

int spawnableRows = 2;
int spawnableColumns = numOfPairs;
// Calculate the size of the card based on the screen size
float cardWidth = screenWidth / (spawnableColumns + 1); // +1 for some padding
float cardHeight = screenHeight / (spawnableRows + 1); // +1 for some padding

// Calculate the total width and height of all the cards
float totalCardWidth = spawnableColumns * cardWidth;
float totalCardHeight = spawnableRows * cardHeight;

// Calculate the initial position and card offset
Vector3 initialPosition = new Vector3(cardWidth, totalCardHeight, 0);
Vector3 cardOffset = new Vector3(cardWidth, -cardHeight, 0);

I have chosen limit to 2 rows and then number of columns as number of pairs. With changing number of cards I need a total card width and lenght. Then I can calculate the initial position and card offset. The initial position is the top left corner of the first card. Then I can calculate the position of the next card by adding the card offset to the previous card position.

Vector3 spawnPosition = initialPosition + (Vector3)(cardOffset * new Vector2(column, row));

Key takeouts

I have learned how math is important in games. Choosing the places of things may be really pain without some spatial imagination. Another thing is Game Manager, how to make it accessible everywhere in the code, how to communicate different scripts with it. And the last but not least thing is the place in the code and way of operating of Game Objects that are already unnecessary, that you can't just delete or deactivate it, sometimes you need to do some adjustments before you proceed with that.