• umutefiloglu@outlook.com
  • Ankara, Turkey

Slappy Bird 2D Tower-Defence Style Game [Updated in GitHub on 22 Dec 2018]

Summary

…While following this tutorial, I also completed the first release of my 2D game and I gave the name of “Slappy Bird” to my game. This game is 2D tower-defence style game. Your character is an orc that defends our little bird which looks like a flappy bird. In this game, enemies spawn randomly in a specified range of the scene. Game has 2 enemies which differences each other from their attributes. These enemies’ abilities change after each stage. At the different scores, game stage changes automatically. At the beginning, our Slappy Bird has 5 health and whenever enemy monster touches bird, it loses one health. If bird’s health reaches 0, then we lost the game and if our score is in top 3, we can insert our name to the scoreboard. Orc is moved with the “WASD” keys. With the left button of mouse, we throw axe towards the direction that orc looks. We have ammo in our game and if it reaches 0, then we cannot throw axe anymore. When we pressed the left button of the mouse, we lose 1 ammo and throw axe. If throwed axe hits the enemy, we gain our lost ammo back. In every 10 seconds, we gain 1 ammo, too. I used ready sprites to build up my game. I have created 4 scenes for my entire game up to now. These are credits, start screen, high score, game scene. You can see different screenshots from my game, below:

Some Codes From The Project

Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    Animator animator;
    public float moveSpeed = 0.1f;
    public int jumpSpeed = 7;
    public bool isGrounded;
    private bool isPaused;
    private bool isLoose;
    //public bool attack = false;
    public bool turnedRight = true;

    private void Awake()
    {
        animator = gameObject.GetComponent<Animator>();
    }

    // Use this for initialization
    void Start () {
        isPaused = false;
        isLoose = false;
    }

	// Update is called once per frame
	void Update () {
        isLoose = GameObject.Find("little_bird").GetComponent<bird>().isLoose;
        isPaused = GameObject.Find("Canvas/ContinueGameButton").GetComponent<PauseButtonShift>().isPaused;

        if (isLoose == false && isPaused == false)
        {
            if (Input.GetKey("a") && transform.position.x > -18.6)
            {
                //animator.enabled = true;
                transform.localScale = new Vector3(-1, 1, 1);
                animator.SetBool("walking", true);
                gameObject.transform.Translate(Vector3.left * moveSpeed);
                turnedRight = false;
            }

            if (Input.GetKey("d") && transform.position.x < 17.1)
            {
                //animator.enabled = true;
                transform.localScale = new Vector3(1, 1, 1);
                animator.SetBool("walking", true);
                gameObject.transform.Translate(Vector3.right * moveSpeed);
                turnedRight = true;
            }

            if ((Input.GetKey("w")) && (isGrounded == true))
            {
                Rigidbody2D mybody = gameObject.GetComponent<Rigidbody2D>();
                mybody.velocity = new Vector2(0, jumpSpeed);
                isGrounded = false;
            }

            if (Input.GetKeyUp("a") || Input.GetKeyUp("d"))
            {
                animator.SetBool("walking", false);
            }

            if (Input.GetMouseButton(0) && GameObject.Find("AxeSpawner").GetComponent<axeSpawn>().ammo > 0)
            {
                GameObject.Find("holdingAxe").GetComponent<Animator>().SetBool("isMouseHeld", true);
            }

            if (GameObject.Find("AxeSpawner").GetComponent<axeSpawn>().ammo > 0 && Input.GetMouseButtonUp(0) && GameObject.Find("Time").GetComponent<TimeCounter>().split_sec != GameObject.Find("ContinueGame").GetComponent<continueButton>().TimeActivated)
            {
                GameObject.Find("holdingAxe").GetComponent<Animator>().SetBool("isMouseHeld", false);
                GameObject.Find("AxeSpawner").GetComponent<axeSpawn>().isFired = true;
            }
        }
        else if (isLoose == true)
        {
            Destroy(gameObject.GetComponent<Animator>());
        }
    }
}
Stage.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Stage : MonoBehaviour {
    private bool isLoose;
    private bool isPaused;
    public AudioClip gainHealth;
    public int callOnce = 0;
    public int bird_health;
    public bool call1stMonster;
    public bool call2ndMonster;
    public bool holdOn;
    public bool holdOnLower;
    Color holdColor;

	// Use this for initialization
	void Start () {
        holdColor = GameObject.Find("Sky").GetComponent<SpriteRenderer>().color;
        isPaused = GameObject.Find("Canvas/ContinueGameButton").GetComponent<PauseButtonShift>().isPaused;
        isLoose = GameObject.Find("little_bird").GetComponent<bird>().isLoose;
        GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 1";
        StartCoroutine(removeStageNumber());
        bird_health = GameObject.Find("little_bird").GetComponent<bird>().health;
        call1stMonster = true;
        call2ndMonster = false;
        holdOn = false;
        holdOnLower = true;
    }
	
	// Update is called once per frame
	void Update () {
        isLoose = GameObject.Find("little_bird").GetComponent<bird>().isLoose;
        isPaused = GameObject.Find("Canvas/ContinueGameButton").GetComponent<PauseButtonShift>().isPaused;

        if (isLoose == false && isPaused == false)
        {
            isLoose = GameObject.Find("little_bird").GetComponent<bird>().isLoose;
            if (holdOn == true)
            {
                if (holdOnLower == true)
                {
                    holdColor.g -= 0.02f;
                    holdColor.b -= 0.02f;
                    GameObject.Find("Sky").GetComponent<SpriteRenderer>().color = holdColor;
                }
                else if (holdOnLower == false)
                {
                    holdColor.g += 0.02f;
                    holdColor.b += 0.02f;
                    GameObject.Find("Sky").GetComponent<SpriteRenderer>().color = holdColor;
                }

                if (Mathf.Round(holdColor.g * 100f) / 100f == 1)
                {
                    holdOnLower = true;
                }
                else if (Mathf.Round(holdColor.g * 100f) / 100f == 0)
                {
                    holdOnLower = false;
                }
            }

            if (GameObject.Find("Score").GetComponent<Score>().score == 1000 && callOnce == 0) //STAGE 2
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 2";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 3;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 3;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score == 1100)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score == 2500 && callOnce == 0) //STAGE 3
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 3";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 2;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 2;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score == 2600)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score == 5000 && callOnce == 0) //STAGE 4
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 4";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 1;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score == 5100)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score == 7500 && callOnce == 0) //STAGE 5
            {
                call1stMonster = false;
                call2ndMonster = true;
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 5";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 2;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 5;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 2;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 5;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 7501 && GameObject.Find("Score").GetComponent<Score>().score <= 8000)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score >= 10000 && GameObject.Find("Score").GetComponent<Score>().score <= 10500 && callOnce == 0) //STAGE 6
            {

                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 6";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 3;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 3;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 10501 && GameObject.Find("Score").GetComponent<Score>().score <= 11000)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score >= 12500 && GameObject.Find("Score").GetComponent<Score>().score <= 13000 && callOnce == 0) //STAGE 7
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 7";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 2;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 2;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 13001 && GameObject.Find("Score").GetComponent<Score>().score <= 13500)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score >= 15000 && GameObject.Find("Score").GetComponent<Score>().score <= 15500 && callOnce == 0) //STAGE 8
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 8";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 1;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 15501 && GameObject.Find("Score").GetComponent<Score>().score <= 16000)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score >= 17500 && GameObject.Find("Score").GetComponent<Score>().score <= 18000 && callOnce == 0) //STAGE 9
            {
                call1stMonster = true;
                call2ndMonster = true;
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 9";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 2;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 5;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 2;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 5;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 18001 && GameObject.Find("Score").GetComponent<Score>().score <= 18500)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score >= 20000 && GameObject.Find("Score").GetComponent<Score>().score <= 20500 && callOnce == 0) //STAGE 10
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 10";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 3;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 3;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 20501 && GameObject.Find("Score").GetComponent<Score>().score <= 21000)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score >= 22500 && GameObject.Find("Score").GetComponent<Score>().score <= 23000 && callOnce == 0) //STAGE 11
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "STAGE 11";
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 2;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 2;
                callOnce++;
                healthUp();
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 23001 && GameObject.Find("Score").GetComponent<Score>().score <= 23500)
            {
                callOnce = 0;
            }

            if (GameObject.Find("Score").GetComponent<Score>().score >= 25000 && GameObject.Find("Score").GetComponent<Score>().score <= 25500 && callOnce == 0) //STAGE 12
            {
                GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "HOLD ON!";
                holdOn = true;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMin = 1;
                GameObject.Find("LeftSpawner").GetComponent<LeftSpawner>().randomTimeMax = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMin = 1;
                GameObject.Find("RightSpawner").GetComponent<RightSpawner>().randomTimeMax = 1;
                callOnce++;
                healthUp();
                StartCoroutine(pauseAndPlayBackgroundMusic());
                StartCoroutine(removeStageNumber());
            }
            else if (GameObject.Find("Score").GetComponent<Score>().score >= 25501 && GameObject.Find("Score").GetComponent<Score>().score <= 26000)
            {
                callOnce = 0;
            }
        }
    }

    IEnumerator removeStageNumber()
    {
        yield return new WaitForSeconds(3);
        GameObject.Find("Canvas/Stage").GetComponent<Text>().text = "";
    }

    IEnumerator pauseAndPlayBackgroundMusic()
    {
        gameObject.GetComponent<AudioSource>().Play();
        GameObject.Find("Ambiance").GetComponent<AudioSource>().Pause();
        yield return new WaitForSeconds(60);
        GameObject.Find("Ambiance").GetComponent<AudioSource>().Play();
    }

    public void healthUp()
    {
        if (GameObject.Find("little_bird").GetComponent<bird>().health < 5)
        {
            bird_health = ++GameObject.Find("little_bird").GetComponent<bird>().health;
            GameObject.Find("Canvas/hp" + bird_health).GetComponent<Image>().enabled = true;
            GameObject.Find("Canvas/hp" + bird_health).GetComponent<Animator>().SetBool("isGain", true);
            GameObject.Find("Canvas/hp" + bird_health).GetComponent<Animator>().SetBool("isHit", false);
            gameObject.GetComponent<AudioSource>().PlayOneShot(gainHealth);
        }
    }
}

GitHub Link of The Project is here.

Some Screenshots From The Project

Figure 1
Figure 2
Figure 3
Figure 4
Figure 5