본문 바로가기
개발/C#

유니티 C# 슬롯머신 간단 구현 Unity Slot machine

by SPNK 2023. 8. 17.
반응형
  • 코드 작성
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class SlotMachine : MonoBehaviour
{
    public Text resultText;
    public Button spinButton;

    public string[] symbols;
    public int initialCredits = 100;

    private int currentCredits;

    private void Start()
    {
        currentCredits = initialCredits;
        UpdateCreditsText();

        spinButton.onClick.AddListener(Spin);
    }

    private void Spin()
    {
        if (currentCredits <= 0)
        {
            resultText.text = "Out of credits!";
            return;
        }

        currentCredits--;

        int symbol1Index = Random.Range(0, symbols.Length);
        int symbol2Index = Random.Range(0, symbols.Length);
        int symbol3Index = Random.Range(0, symbols.Length);

        string symbol1 = symbols[symbol1Index];
        string symbol2 = symbols[symbol2Index];
        string symbol3 = symbols[symbol3Index];

        if (symbol1 == symbol2 && symbol2 == symbol3)
        {
            resultText.text = "Jackpot! You won!";
            currentCredits += 10;
        }
        else
        {
            resultText.text = "Try again!";
        }

        UpdateCreditsText();
    }

    private void UpdateCreditsText()
    {
        resultText.text = "Credits: " + currentCredits;
    }
}
반응형

댓글