스네이크 머리가 벽에 닿이거나 꼬리에 닿인 경우,
게임 오버 판정이 되도록 코드를 추가하였다.
게임 오버가 되었을 경우,
게임을 재시작하거나 메인 메뉴로 돌아갈 수 있도록 구성하였고
그에 대한 코드를 GameManager에 작성하여 관리하였다.
"재시도" 버튼을 클릭하면 게임이 초기화된 상태로 다시 시작하고
"메인으로" 버튼을 클릭하면 메인 화면으로 이동한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
// for Setting
public GameObject inSetting;
public GameObject mainButton;
// Snake Var
public GameObject snake;
// Score Var
public Text scoreText;
// Pause Var
bool isPause = false;
public Text pauseText;
// for Game Over
public GameObject gameOver;
void Update()
{
// Run in GameScene
if (SceneManager.GetActiveScene().name == "GameScene")
{
// Load Score from Snake Script
Snake snakeLogic = snake.GetComponent<Snake>();
scoreText.text = string.Format("{0:n0}", snakeLogic.score); // {0:n0}: 세자리마다 쉼표로 나눠주는 숫자 양식
}
}
// Start Button
public void GameStart()
{
// Change Scene
SceneManager.LoadScene(1);
}
// Setting Button
public void GameSetting()
{
// On or Off the Object
inSetting.SetActive(true);
mainButton.SetActive(false);
}
// Back Button in Setting
public void SettingBack()
{
// On or Off the Object
inSetting.SetActive(false);
mainButton.SetActive(true);
}
// Pause Game
public void TimePause()
{
if (isPause)
{
// Resume
Time.timeScale = 1;
pauseText.text = "일시정지";
isPause = false;
}
else
{
// Pause
Time.timeScale = 0;
pauseText.text = "다시시작";
isPause = true;
}
}
// Game Over
public void GameOver()
{
Time.timeScale = 0;
gameOver.SetActive(true);
}
// Game Retry
public void GameRetry()
{
SceneManager.LoadScene(1);
Time.timeScale = 1;
}
// to Main Menu
public void GameToMain()
{
// Change Scene
SceneManager.LoadScene(0);
Time.timeScale = 1;
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Snake : MonoBehaviour
{
// Game Manager
public GameManager manager;
// Snake Movement Var
Vector2 dir = 1 * Vector2.right;
Vector2 dirTemp = 1 * Vector2.right;
private float size = 1;
// Snake Direction Var
float h;
float v;
bool isHorizonMove;
// Mobile Key Var
int up_Value;
int down_Value;
int left_Value;
int right_Value;
bool up_Down;
bool down_Down;
bool left_Down;
bool right_Down;
bool up_Up;
bool down_Up;
bool left_Up;
bool right_Up;
// Snake Tail Var & Prefab
List<Transform> tail = new List<Transform>();
public GameObject tailPrefab;
// for Check that Snake Eat
bool ate = false;
// Score Var
public int score = 0;
void Start()
{
InvokeRepeating("Move", 1f, 0.05f);
}
void Update()
{
// Move Value (PC + Mobile)
h = Input.GetAxisRaw("Horizontal") + (right_Value + left_Value);
v = Input.GetAxisRaw("Vertical") + (up_Value + down_Value);
// Check Button Down & Up (PC || Mobile)
bool hDown = Input.GetButtonDown("Horizontal") || right_Down || left_Down;
bool vDown = Input.GetButtonDown("Vertical") || up_Down || down_Down;
bool hUp = Input.GetButtonUp("Horizontal") || right_Up || left_Up;
bool vUp = Input.GetButtonUp("Vertical") || up_Up || down_Up;
// Check Horizontal Move
if (hDown)
isHorizonMove = true;
else if (vDown)
isHorizonMove = false;
else if (hUp || vUp)
isHorizonMove = (h != 0);
// Determine Direction
if (h != 0 || v != 0)
{
dirTemp = dir;
dir = (isHorizonMove ? new Vector2(h, 0) : new Vector2(0, v));
// Check Snake Direction (Reverse)
if (dir == -dirTemp)
{
dir = dirTemp;
}
}
// Mobile Var Init
up_Down = false;
down_Down = false;
left_Down = false;
right_Down = false;
up_Up = false;
down_Up = false;
left_Up = false;
right_Up = false;
}
void Move()
{
// Store Present Position
Vector2 v = transform.position;
// Move
transform.Translate(size * dir);
// Check whether Snake Eats Food or Not
if (ate)
{
// Copy the Tail in Present Position
GameObject g = (GameObject)Instantiate(tailPrefab, v, Quaternion.identity);
// Store the Tail
tail.Insert(0, g.transform);
ate = false;
}
// Move the Tail to the Head
else if (tail.Count > 0)
{
tail.Last().position = v;
tail.Insert(0, tail.Last());
tail.RemoveAt(tail.Count - 1);
}
}
// Button Down (Mobile)
public void ButtonDown(string type)
{
switch (type)
{
case "U":
up_Value = 1;
up_Down = true;
break;
case "D":
down_Value = -1;
down_Down = true;
break;
case "L":
left_Value = -1;
left_Down = true;
break;
case "R":
right_Value = 1;
right_Down = true;
break;
}
}
// Button Up (Mobile)
public void ButtonUp(string type)
{
switch (type)
{
case "U":
up_Value = 0;
up_Up = true;
break;
case "D":
down_Value = 0;
down_Up = true;
break;
case "L":
left_Value = 0;
left_Up = true;
break;
case "R":
right_Value = 0;
right_Up = true;
break;
}
}
void OnTriggerEnter2D(Collider2D col)
{
// Eat Food
if (col.gameObject.tag == "Food")
{
ate = true;
Destroy(col.gameObject);
// Add Score
Food foodLogic = col.gameObject.GetComponent<Food>();
score += foodLogic.point;
}
else
{
}
// Touch Border or Tail
if (col.gameObject.tag == "Border" || col.gameObject.tag == "Snake")
{
manager.GameOver();
}
}
}
궁금하신 점은 밑에 댓글 바랍니다
'메모장 > 스네이크 게임' 카테고리의 다른 글
스네이크 게임 - 해상도 변경 (0) | 2021.01.21 |
---|---|
스네이크 게임 - 게임 클리어 판정 (0) | 2021.01.20 |
스네이크 게임 - 메인 메뉴와 일시 정지 (0) | 2021.01.18 |
스네이크 게임 - 점수 카운트 (0) | 2021.01.17 |
스네이크 게임 - 스네이크 길이 증가 (0) | 2021.01.16 |