Flappy Bird
https://github.com/devgame2020/unity/tree/main/FlappyBird
unity/FlappyBird at main · devgame2020/unity
Contribute to devgame2020/unity development by creating an account on GitHub.
github.com
backgroundScrolling.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class backgroundScrolling : MonoBehaviour
{
public float speed = 1;
// Update is called once per frame
void Update()
{
if (Manager.Instance.isPlay)
Move();
}
void Move()
{
Vector3 curPos = transform.position;
Vector3 nextPos = Vector3.left * speed * Time.deltaTime;
transform.position = nextPos + curPos;
if (transform.position.x < -16)
{
curPos = transform.position;
nextPos = Vector3.right * 32;
transform.position = nextPos + curPos;
}
}
}
Bird.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bird : MonoBehaviour, IGameObject
{
[SerializeField]
private Rigidbody2D _rigidbody = null;
[SerializeField]
private float _jumpValue = 1.0f;
// Start is called before the first frame update
void Start()
{
_rigidbody.constraints = RigidbodyConstraints2D.FreezePositionY;
}
public void FreezePositionY( bool value )
{
_rigidbody.constraints = value ? RigidbodyConstraints2D.FreezePositionY : RigidbodyConstraints2D.None;
}
public void GameUpdate()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
_rigidbody.velocity = Vector2.up * _jumpValue;
// _rigidbody.AddForce(new Vector2(0, _jumpValue));
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//Debug.Log(collision.gameObject.tag);
switch(collision.gameObject.tag)
{
case "Enemy":
Manager.Instance.isPlay = false;
break;
}
}
}
UIManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : Singleton<UIManager>
{
[SerializeField]
private GameObject _title = null;
[SerializeField]
private Button _startButton = null;
[SerializeField]
private Button _tipButton = null;
[SerializeField]
private GameObject _tipButton2 = null;
[SerializeField]
private NumbersRenderer _numbersRenderer = null;
[SerializeField]
private GameOverPopup _gameOverPopup = null;
public int Score { set {
_numbersRenderer.value = value;
} }
private void Start()
{
Init();
ShowTitle();
}
public void Init()
{
_title.gameObject.SetActive(false);
_startButton.gameObject.SetActive(false);
_tipButton.gameObject.SetActive(false);
_tipButton2.SetActive(false);
_gameOverPopup.gameObject.SetActive(false);
_numbersRenderer.gameObject.SetActive(false);
}
public void ShowTitle()
{
_title.gameObject.SetActive(true);
_startButton.gameObject.SetActive(true);
}
public void StartButton()
{
ShowTipButton();
_title.SetActive(false);
_startButton.gameObject.SetActive(false);
}
public void TipButton()
{
ShowScore();
Manager.Instance.isPlay = true;
_tipButton.gameObject.SetActive(false);
_tipButton2.SetActive(false);
}
private void ShowTipButton()
{
_tipButton.gameObject.SetActive(true);
_tipButton2.SetActive(true);
}
public void ShowScore()
{
_numbersRenderer.value = 0;
_numbersRenderer.gameObject.SetActive(true);
}
public void InvokeGameOver()
{
_gameOverPopup.Show();
_numbersRenderer.gameObject.SetActive(false);
}
}
'Unity' 카테고리의 다른 글
블랜더 (0) | 2024.12.24 |
---|---|
2025년2월2주 (0) | 2024.12.24 |
2025년2월1주(2/3~2/9) 무료 서버 구축하기 (0) | 2024.12.24 |
2025년1월5주(1/27~2/2) (0) | 2024.12.24 |
2025년1월 2주 (0) | 2024.12.24 |