FreeShare

Lập trình game flappy bird sử dụng unity

Chủ nhật, 18/02/2024
image
– Đầu tiên ta cần cài đặt unity bằng cách vào trang https://unity3d.com/get-unity/download download unity hub về máy và tiến hành cài đặt – Mở unity hub lên và vào phần installs cài 1 phiên bản unity về máy. ở đây mình cài đặt phiên bản unity 2021.3.6f1 – Tạo 1 dự án mới

- Đầu tiên ta cần cài đặt unity bằng cách vào trang https://unity3d.com/get-unity/download download unity hub về máy và tiến hành cài đặt

- Mở unity hub lên và vào phần installs cài 1 phiên bản unity về máy. ở đây mình cài đặt phiên bản unity 2021.3.6f1

- Tạo 1 dự án mới chọn templates 2d core. Đặt tên dự án là flappy-bird

- Download các bộ ảnh cần thiết bằng đường link

- Trong thư mục Assets tạo 1 mục đặt tên là Texture và copy ảnh trên vào mục này

- Chọn ảnh và chọn sprite mode là multiple

- Bấm vào sprite editor và chọn slice ->automatic và bấm apply, và ta đã có bộ ảnh rồi

- Kéo thả cái ảnh nền đầu tiên vào đối tượng của game (dưới cái SampleScene), đổi tên Là BackGround, kéo cho fit full hết với cái Main Camera

- Kéo cái ảnh nền đất vào mục đối tượng của game, Đổi tên thành Platform, bấm add Components và search: Box collider 2D , cái Box collider 2D để xác định là con chim sẽ xảy ra va chạm nếu chạm đến cái background này

- Trở lại code c# ta tạo luôn 4 File c# như sau

Assets/Scripts/GameManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject startButton;
    public Player player;

    public Text gameOverCountdown;
    public float countTimer = 5;

    // Start is called before the first frame update
    void Start()
    {
        gameOverCountdown.gameObject.SetActive(false);
        Time.timeScale = 0;
    }

    private void Update()
    {
        if( player.isDead )
        {
            gameOverCountdown.gameObject.SetActive(true);
            countTimer -= Time.unscaledDeltaTime;
        }

        gameOverCountdown.text = "Restarting in " + (countTimer).ToString("0");

        if(countTimer < 0)
        {
            RestartGame();
        }
    }

    public void StartGame()
    {
        startButton.SetActive(false);
        Time.timeScale = 1;
    }

    public void GameOver()
    {
        Time.timeScale = 0;
    }


    public void RestartGame()
    {
        EditorSceneManager.LoadScene(0);
    }
}

Assets/Scripts/Obstacle.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Obstacle : MonoBehaviour
{
    public float speed;

    // Update is called once per frame
    void Update()
    {
        transform.position += ((Vector3.left * speed) * Time.deltaTime);
    }
}

Assets/Scripts/Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float velocity = 2.4f;
    private Rigidbody2D rigidbody;
    public GameManager gameManager;
    public bool isDead = false;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            rigidbody.velocity = Vector2.up * velocity;
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        isDead = true;
        gameManager.GameOver();
    }
}

Assets/Scripts/Spawner.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    public float queueTime = 1.5f;
    private float time = 0f;
    public GameObject obstacle;

    public float height;

    // Update is called once per frame
    void Update()
    {
        if(time > queueTime)
        {
            GameObject go = Instantiate(obstacle);
            go.transform.position = transform.position + new Vector3(0, Random.Range(-height, height), 0);

            time = 0;

            Destroy(go, 10);
        }

        time += Time.deltaTime;
    }
}

- GameObject -> Create Empty tạo 1 đối tượng đặt tên là Obstacle pipes và kéo thả file obstacle.cs vào mục này

- Kéo thả 2 ảnh của 2 cái ống vào dưới mục này, ở mỗi ống thêm components: Box collider 2D

- Tạo 1 đối tượng rỗng đặt tên là Obstacle Spawner và kéo thả file Spawner.cs vào phần này, ở biến obstacle kéo thả Obstacle pipes vào phần này

- Tạo đối tượng Rỗng đặt tên là Player, kéo thả Animation của con chim vào (cái tạo animation tự tìm hiểu nhé), add thêm components Rigidbody 2D, Capsule Collider 2D sửa cái này chỗ Direction chọn horinzontal, kéo thả script Player.cs vào

- Tạo đối tượng rỗng đặt tên là GameManager kéo file GameManager.cs vào phần này

Dưới đối tượng Scripts mình tạo 1 nút đặt tên là Restart kéo ảnh start vào và 1 đối tượng Text (UI->Text)

kéo thả nút restart và text, Player vào cái GameManager này

Hình ảnh demo

 

Hình ảnh bird hơi nhỏ cần nhờ design lại

Bạn có thể download source code tại đây