Flappy Bird Style

This examples based on Flappy Bird Style Example Game.

This example was made by using CSharpParser to parse the following script:

  • Bird.cs
  • Column.cs
  • ColumnPool.cs
  • GameControl.cs
  • RepeatingBackground.cs
  • ScrollingObject.cs

Original Script:

using UnityEngine;
using System.Collections;

public class Bird : MonoBehaviour 
{
  public float upForce;					//Upward force of the "flap".
  private bool isDead = false;			//Has the player collided with a wall?

  private Animator anim;					//Reference to the Animator component.
  private Rigidbody2D rb2d;				//Holds a reference to the Rigidbody2D component of the bird.

  void Start()
  {
    //Get reference to the Animator component attached to this GameObject.
    anim = GetComponent<Animator> ();
    //Get and store a reference to the Rigidbody2D attached to this GameObject.
    rb2d = GetComponent<Rigidbody2D>();
  }

  void Update()
  {
    //Don't allow control if the bird has died.
    if (isDead == false) 
    {
      //Look for input to trigger a "flap".
      if (Input.GetMouseButtonDown(0)) 
      {
        //...tell the animator about it and then...
        anim.SetTrigger("Flap");
        //...zero out the birds current y velocity before...
        rb2d.velocity = Vector2.zero;
        //	new Vector2(rb2d.velocity.x, 0);
        //..giving the bird some upward force.
        rb2d.AddForce(new Vector2(0, upForce));
      }
    }
  }

  void OnCollisionEnter2D(Collision2D other)
  {
    // Zero out the bird's velocity
    rb2d.velocity = Vector2.zero;
    // If the bird collides with something set it to dead...
    isDead = true;
    //...tell the Animator about it...
    anim.SetTrigger ("Die");
    //...and tell the game control about it.
    GameControl.instance.BirdDied ();
  }
}
using UnityEngine;
using System.Collections;

public class Column : MonoBehaviour 
{
  void OnTriggerEnter2D(Collider2D other)
  {
    if(other.GetComponent<Bird>() != null)
    {
      //If the bird hits the trigger collider in between the columns then
      //tell the game control that the bird scored.
      GameControl.instance.BirdScored();
    }
  }
}
using UnityEngine;
using System.Collections;

public class ColumnPool : MonoBehaviour 
{
  public GameObject columnPrefab;									//The column game object.
  public int columnPoolSize = 5;									//How many columns to keep on standby.
  public float spawnRate = 3f;									//How quickly columns spawn.
  public float columnMin = -1f;									//Minimum y value of the column position.
  public float columnMax = 3.5f;									//Maximum y value of the column position.

  private GameObject[] columns;									//Collection of pooled columns.
  private int currentColumn = 0;									//Index of the current column in the collection.

  private Vector2 objectPoolPosition = new Vector2 (-15,-25);		//A holding position for our unused columns offscreen.
  private float spawnXPosition = 10f;

  private float timeSinceLastSpawned;


  void Start()
  {
    timeSinceLastSpawned = 0f;

    //Initialize the columns collection.
    columns = new GameObject[columnPoolSize];
    //Loop through the collection... 
    for(int i = 0; i < columnPoolSize; i++)
    {
      //...and create the individual columns.
      columns[i] = (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
    }
  }


  //This spawns columns as long as the game is not over.
  void Update()
  {
    timeSinceLastSpawned += Time.deltaTime;

    if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate) 
    {	
      timeSinceLastSpawned = 0f;

      //Set a random y position for the column
      float spawnYPosition = Random.Range(columnMin, columnMax);

      //...then set the current column to that position.
      columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);

      //Increase the value of currentColumn. If the new size is too big, set it back to zero
      currentColumn ++;

      if (currentColumn >= columnPoolSize) 
      {
        currentColumn = 0;
      }
    }
  }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameControl : MonoBehaviour 
{
  public static GameControl instance;			//A reference to our game control script so we can access it statically.
  public Text scoreText;						//A reference to the UI text component that displays the player's score.
  public GameObject gameOvertext;				//A reference to the object that displays the text which appears when the player dies.

  private int score = 0;						//The player's score.
  public bool gameOver = false;				//Is the game over?
  public float scrollSpeed = -1.5f;


  void Awake()
  {
    //If we don't currently have a game control...
    if (instance == null)
      //...set this one to be it...
      instance = this;
    //...otherwise...
    else if(instance != this)
      //...destroy this one because it is a duplicate.
      Destroy (gameObject);
  }

  void Update()
  {
    //If the game is over and the player has pressed some input...
    if (gameOver && Input.GetMouseButtonDown(0)) 
    {
      //...reload the current scene.
      SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
  }

  public void BirdScored()
  {
    //The bird can't score if the game is over.
    if (gameOver)	
      return;
    //If the game is not over, increase the score...
    score++;
    //...and adjust the score text.
    scoreText.text = "Score: " + score.ToString();
  }

  public void BirdDied()
  {
    //Activate the game over text.
    gameOvertext.SetActive (true);
    //Set the game to be over.
    gameOver = true;
  }
}
using UnityEngine;
using System.Collections;

public class RepeatingBackground : MonoBehaviour 
{
  
  private BoxCollider2D groundCollider;		//This stores a reference to the collider attached to the Ground.
  private float groundHorizontalLength;		//A float to store the x-axis length of the collider2D attached to the Ground GameObject.

  //Awake is called before Start.
  private void Awake ()
  {
    //Get and store a reference to the collider2D attached to Ground.
    groundCollider = GetComponent<BoxCollider2D> ();
    //Store the size of the collider along the x axis (its length in units).
    groundHorizontalLength = groundCollider.size.x;
  }

  //Update runs once per frame
  private void Update()
  {
    //Check if the difference along the x axis between the main Camera and the position of the object this is attached to is greater than groundHorizontalLength.
    if (transform.position.x < -groundHorizontalLength)
    {
      //If true, this means this object is no longer visible and we can safely move it forward to be re-used.
      RepositionBackground ();
    }
  }

  //Moves the object this script is attached to right in order to create our looping background effect.
  private void RepositionBackground()
  {
    //This is how far to the right we will move our background object, in this case, twice its length. This will position it directly to the right of the currently visible background object.
    Vector2 groundOffSet = new Vector2(groundHorizontalLength * 2f, 0);

    //Move this object from it's position offscreen, behind the player, to the new position off-camera in front of the player.
    transform.position = (Vector2) transform.position + groundOffSet;
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScrollingObject : MonoBehaviour 
{
  private Rigidbody2D rb2d;

  // Use this for initialization
  void Start () 
  {
    //Get and store a reference to the Rigidbody2D attached to this GameObject.
    rb2d = GetComponent<Rigidbody2D>();

    //Start the object moving.
    rb2d.velocity = new Vector2 (GameControl.instance.scrollSpeed, 0);
  }

  void Update()
  {
    // If the game is over, stop scrolling.
    if(GameControl.instance.gameOver == true)
    {
      rb2d.velocity = Vector2.zero;
    }
  }
}

Generated Script:

using UnityEngine;
using System.Collections;

public class Bird : MonoBehaviour {
  public float upForce = 0F;
  private bool isDead = false;
  private Animator anim;
  private Rigidbody2D rb2d;

  void Start() {
    anim = base.GetComponent<Animator>();
    rb2d = base.GetComponent<Rigidbody2D>();
  }

  void Update() {
    if((isDead == false)) {
      if(Input.GetMouseButtonDown(0)) {
        anim.SetTrigger("Flap");
        rb2d.velocity = Vector2.zero;
        rb2d.AddForce(new Vector2(0, upForce));
      }
    }
  }

  void OnCollisionEnter2D(Collision2D other) {
    rb2d.velocity = Vector2.zero;
    isDead = true;
    anim.SetTrigger("Die");
    GameControl.instance.BirdDied();
  }
}
using UnityEngine;
using System.Collections;

public class Column : MonoBehaviour {

  void OnTriggerEnter2D(Collider2D other) {
    if((other.GetComponent<Bird>() != null)) {
      GameControl.instance.BirdScored();
    }
  }
}
using UnityEngine;
using System.Collections;

public class ColumnPool : MonoBehaviour {
  public GameObject columnPrefab;
  public int columnPoolSize = 5;
  public float spawnRate = 3F;
  public float columnMin = -1F;
  public float columnMax = 3.5F;
  private GameObject[] columns;
  private int currentColumn = 0;
  private Vector2 objectPoolPosition = new Vector2(-15f, -25f);
  private float spawnXPosition = 10F;
  private float timeSinceLastSpawned;

  void Start() {
    timeSinceLastSpawned = 0F;
    columns = new GameObject[columnPoolSize];
    for(int index = 0; index < columnPoolSize; index += 1) {
      columns[index] = (GameObject)Object.Instantiate<GameObject>(columnPrefab, objectPoolPosition, Quaternion.identity);
    }
  }

  void Update() {
    float spawnYPosition;
    timeSinceLastSpawned += Time.deltaTime;
    if(((GameControl.instance.gameOver == false) && (timeSinceLastSpawned >= spawnRate))) {
      timeSinceLastSpawned = 0F;
      spawnYPosition = Random.Range(columnMin, columnMax);
      columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
      currentColumn += 1;
      if((currentColumn >= columnPoolSize)) {
        currentColumn = 0;
      }
    }
  }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameControl : MonoBehaviour {
  public static GameControl instance;
  public Text scoreText;
  public GameObject gameOvertext;
  private int score = 0;
  public bool gameOver = false;
  public float scrollSpeed = -1.5F;

  void Awake() {
    if((instance == null)) {
      instance = this as GameControl;
    } else if((instance != this)) {
      Object.Destroy(base.gameObject);
    }
  }

  void Update() {
    if((gameOver && Input.GetMouseButtonDown(0))) {
      SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
  }

  public void BirdScored() {
    if(gameOver) {
      return;
    }
    score += 1;
    scoreText.text = ("Score: " + score.ToString());
  }

  public void BirdDied() {
    gameOvertext.SetActive(true);
    gameOver = true;
  }
}
using UnityEngine;
using System.Collections;

public class RepeatingBackground : MonoBehaviour {
  private BoxCollider2D groundCollider;
  private float groundHorizontalLength;

  private void Awake() {
    groundCollider = base.GetComponent<BoxCollider2D>();
    groundHorizontalLength = groundCollider.size.x;
  }

  private void Update() {
    if((base.transform.position.x < -(groundHorizontalLength))) {
      RepositionBackground();
    }
  }

  private void RepositionBackground() {
    Vector2 groundOffSet;
    groundOffSet = new Vector2((groundHorizontalLength * 2F), 0);
    base.transform.position = ((Vector2)base.transform.position + groundOffSet);
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScrollingObject : MonoBehaviour {
  private Rigidbody2D rb2d;

  void Start() {
    rb2d = base.GetComponent<Rigidbody2D>();
    rb2d.velocity = new Vector2(GameControl.instance.scrollSpeed, 0);
  }

  void Update() {
    if((GameControl.instance.gameOver == true)) {
      rb2d.velocity = Vector2.zero;
    }
  }
}

 

Graphs preview:

 


Requirement

Unity 3D Version 5.6 or higher.
uNode – Visual Scripting Version 1.5.1 or higher, this requirement is optional.

 

 

Download

Was this helpful to you?

Leave a Reply

Your email address will not be published. Required fields are marked *