내일배움캠프

내일배움캠프 Unity - 사전캠프 (르탄이 카드 뒤집기 게임)

달시_Dalsi 2025. 1. 10. 16:48
728x90

뒤집혀져있는 카드를 한장씩 확인하여 제한시간내에 모든 짝을 맞추는 익숙한 게임입니다.

 

 

Board 스크립트

using System.Linq;
using UnityEngine;

public class Board : MonoBehaviour
{
    public int Card_length;

    public GameObject Prefabs_Card;

    void Start()
    {
        int[] arr = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
        arr = arr.OrderBy(x => Random.Range(0f, 7f)).ToArray();


        for (int i = 0; i < Card_length; i++)
        {
            GameObject obj = Instantiate(Prefabs_Card, transform);

            float x = (i % 4) * .4f - .6f;
            float y = (i / 4) * .4f - 1f;

            obj.transform.position = new Vector2 (x, y);
            obj.transform.GetComponent<Card>().Setting(arr[i]);
        }

        GameManager_FindRtanCard.Instance.cardCount = arr.Length;
    }
}

 

 

게임 매니저 스크립트

using TMPro;
using UnityEngine;

public class GameManager_FindRtanCard : MonoBehaviour
{
    public static GameManager_FindRtanCard Instance;

    public TextMeshProUGUI textMeshProUGUI_timer;
    public GameObject textMeshProUGUI_End;

    float time = 30f;

    public int cardCount = 0;

    public Card firstCard;
    public Card secondCard;

    private void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
        }
    }

    private void Start()
    {
        Time.timeScale = 1f;
    }

    void Update()
    {
        time -= Time.deltaTime;
        if(time <= 0f)
        {
            time = 0f;
            GameOver();
        }
        textMeshProUGUI_timer.text = time.ToString("F2");
    }

    public void Matched()
    {
        if (firstCard.idx == secondCard.idx)
        {
            firstCard.DestoryCard();
            secondCard.DestoryCard();
            cardCount -= 2;
            if (cardCount <= 0) { GameOver(); }
        }
        else
        {
            firstCard.CloseCard();
            secondCard.CloseCard();
        }
        firstCard = null;
        secondCard = null;
    }

    void GameOver()
    {
        Time.timeScale = 0f;
        textMeshProUGUI_End.SetActive(true);
    }
}

 

 

Card 스크립트

using UnityEngine;

public class Card : MonoBehaviour
{
    public int idx = 0;

    public GameObject g_Front;
    public GameObject g_Back;
    public Animator myAnim;

    public SpriteRenderer spriteRenderer;

    private void Start()
    {
        myAnim = GetComponent<Animator>();
    }

    public void Setting(int num)
    {
        idx = num;

        spriteRenderer.sprite = Resources.Load<Sprite>($"rtan{idx}");
    }

    public void OpenCard()
    {
        myAnim.SetBool("isFlip", true);
        g_Front.SetActive(true);
        g_Back.SetActive(false);


        if(GameManager_FindRtanCard.Instance.firstCard == null)
        {
            GameManager_FindRtanCard.Instance.firstCard = this;
        }
        else
        {
            GameManager_FindRtanCard.Instance.secondCard = this;
            GameManager_FindRtanCard.Instance.Matched();
        }
    }

    public void DestoryCard()
    {
        Invoke("DestoryCradInvoke", 1.0f);
    }

    void DestoryCradInvoke()
    {
        Destroy(gameObject);
    }

    public void CloseCard()
    {
        Invoke("CloseCardInvoke", 1.0f);
    }

    void CloseCardInvoke()
    {
        myAnim.SetBool("isFlip", false);
        g_Front.SetActive(false);
        g_Back.SetActive(true);
    }
}

 

 

 

RetryButton 스크립트

using UnityEngine;
using UnityEngine.SceneManagement;

public class RetryButton : MonoBehaviour
{
    public void RetryBtn()
    {
        SceneManager.LoadScene("FindRtanCard");
    }
}

Unity Play에 업로드된 제 게임을 아래 링크에서 플레이할 수 있습니다.

 

https://play.unity.com/en/games/66b66d82-f807-42a0-a2e4-20b979ff34fd/findrtancard

 

FindRtanCard on Unity Play

Flip the cards and find the same card

play.unity.com

 

 

728x90