본문 바로가기
개발/C#

유니티 C# 지렁이 키우기 게임 만들기 간단 구현

by SPNK 2023. 4. 1.
반응형
  • 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WormGame : MonoBehaviour
{
    public Text wormsText;
    public Text clicksText;
    public Text timeElapsedText;
    public int worms;
    public int clicks;
    public float timeElapsed;

    void Start()
    {
        worms = 0;
        clicks = 0;
        timeElapsed = 0f;
    }

    void Update()
    {
        wormsText.text = "Worms: " + worms.ToString();
        clicksText.text = "Clicks: " + clicks.ToString();
        timeElapsedText.text = "Time Elapsed: " + timeElapsed.ToString("F1") + " seconds";

        if (worms >= 100)
        {
            Debug.Log("You win!");
            enabled = false;
        }
    }

    public void CollectWorms()
    {
        worms++;
        clicks++;
    }

    public void WaitForReproduction()
    {
        worms += Random.Range(1, 4);
        clicks++;
        StartCoroutine(WaitForOneSecond());
    }

    IEnumerator WaitForOneSecond()
    {
        yield return new WaitForSeconds(1f);
        timeElapsed += 1f;
    }

    public void WormDeath()
    {
        if (Random.Range(0f, 1f) < 0.05f)
        {
            worms--;
            Debug.Log("Oh no! A worm has died.");
        }
    }
}
반응형

댓글