
그림과 같이 생선가게 주인 강아지가 몰려오는 고양이에게 밥을 던져 배를 채워서 내쫓는 게임을 만들어보았다.
public class Dog : MonoBehaviour
{
public GameObject food;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("MakeFood", 0f, 0.2f);
}
private void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float x = mousePos.x;
if (x > 9.2f)
{
x = 9.2f;
}
if (x < -9.2f)
{
x = -9.2f;
}
transform.position = new Vector2(x, transform.position.y);
}
void MakeFood()
{
float x1 = transform.position.x + 2.65f;
float x2 = transform.position.x - 2.8f;
float y = transform.position.y + 0.5f;
Instantiate(food, new Vector2(x1, y), Quaternion.identity);
Instantiate(food, new Vector2(x2, y), Quaternion.identity);
}
}
public class Food : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position += Vector3.up * 0.5f;
if (transform.position.y > 27.0f)
{
Destroy(gameObject);
}
}
}
public class Cat : MonoBehaviour
{
public GameObject hungryCat;
public GameObject fullCat;
public RectTransform front;
public int type;
float full = 5.0f;
float energy = 0.0f;
float speed = 0.05f;
bool isFull = false;
// Start is called before the first frame update
void Start()
{
float x = Random.Range(-9.0f, 9.0f);
float y = 30.0f;
transform.position = new Vector2(x, y);
if (type == 1)
{
speed = 0.05f;
full = 5f;
}
else if (type == 2)
{
speed = 0.02f;
full = 10f;
}
else if (type == 3)
{
transform.localScale = new Vector2(0.8f, 0.8f);
speed = 0.1f;
full = 5f;
}
}
// Update is called once per frame
void Update()
{
if (energy < full)
{
transform.position += Vector3.down * speed;
if (transform.position.y < -16.0f)
{
GameManager.Instance.GameOver();
}
}
else
{
if (transform.position.x > 0)
{
transform.position += Vector3.right * 0.05f;
}
else
{
transform.position += Vector3.left * 0.05f;
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Food"))
{
if (energy < full)
{
energy += 1.0f;
front.localScale = new Vector3(energy / full, 1.0f, 1.0f);
Destroy(collision.gameObject);
if (energy == full)
{
if (!isFull)
{
isFull = true;
hungryCat.SetActive(false);
fullCat.SetActive(true);
Destroy(gameObject);
GameManager.Instance.AddScore();
}
}
}
}
}
}
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public GameObject normalCat;
public GameObject fatCat;
public GameObject pirateCat;
public GameObject retryBtn;
public RectTransform levelFront;
public Text levelTxt;
int level = 0;
int score = 0;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
Application.targetFrameRate = 60;
}
// Start is called before the first frame update
void Start()
{
Time.timeScale = 1.0f;
InvokeRepeating("MakeCat", 0f, 1f);
}
void MakeCat()
{
Instantiate(normalCat);
if (level == 1)
{
int p = Random.Range(0, 10);
if (p < 2) Instantiate(normalCat);
}
else if (level == 2)
{
int p = Random.Range(0, 10);
if (p < 5) Instantiate(normalCat);
}
else if (level == 3)
{
Instantiate(fatCat);
}
else if (level == 4)
{
Instantiate(pirateCat);
}
else if (level >= 5)
{
Instantiate(fatCat);
Instantiate(pirateCat);
}
}
public void GameOver()
{
retryBtn.SetActive(true);
Time.timeScale = 0.0f;
}
public void AddScore()
{
score++;
level = score / 5;
levelTxt.text = level.ToString();
levelFront.localScale = new Vector3((score - level * 5) / 5.0f, 1f, 1f);
}
}