using UnityEngine;
using System.Collections;
/// <summary>
/// This script controls movement of ball
/// </summary>
public class BallMove : MonoBehaviour {
//speed at which ball moves
int speed;
//speed at which ball bounces
int BounceSpeed;
void Start(){
//getting value of speed and BounceSpeed from the GameController
GameObject go = GameObject.Find ("GameController");
GameController speedController = go.GetComponent <GameController> ();
speed = speedController.speed;
BounceSpeed = speedController.BounceSpeed;
}
// Update is called once according to physics
void FixedUpdate ()
{
//moving the ball
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x,speed);
}
void BounceStart(){
//after bouncing from racket ball bounces in opposite direction
speed = BounceSpeed;
}
}
     Scripts 
BallMove.cs                                                                              
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
/// <summary>
/// This script controls hitting of ball, bouncing it back & scores
/// </summary>
public class BounceBack : MonoBehaviour {
public Text scoretext;
int score =0;
void OnTriggerEnter2D(Collider2D other) {
//on hitting by racket bouncing command should start of the BallMove script
other.gameObject.SendMessage ("BounceStart");
//add score by 1
score++;
//set the score to ScoreText
scoretext.text = "" + score.ToString ();
//play the audio for bouncing
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
//run the command score of GameController
GameObject.FindGameObjectWithTag("GameController").SendMessage("Score");
}
}
     Scripts 
BounceBack.cs                                                                              
using UnityEngine;
using System.Collections;
/// <summary>
/// This script destroys the ball game object when it leaves game area
/// </summary>
public class DestroyByBoundry : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other) {
//On entering the boundries outside the game area the ball prefab will destroy
Destroy(other.gameObject);
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
/// <summary>
/// This script controls the creation of ball gameobject, handling all canvases and the gameover screen
/// </summary>
public class GameController : MonoBehaviour
{
public GameObject GameObjectBall;
public Vector3 spawnValues;
public int ballCount;
public float spawnWait;
public float startWait;
public float waveWait;
public int speed;
public int BounceSpeed;
public int ballCountIncrease;
float TimeSpeed =1;
public float TimeSpeedIncrease;
public Text speedText;
int speedMultiplier=1;
int score=0;
int highscore;
public GameObject GameOverCanvas;
public GameObject StartCanvas;
public Text ScoreText;
public Text HighScoreText;
public GameObject pausecanvas;
void Start ()
{   //start the balls
StartCoroutine (SpawnWaves ());
//set start canvas activated
StartCanvas.SetActive (true);
//set GameOver canvas deactivated
GameOverCanvas.SetActive (false);
//set puase canvas deactivated
pausecanvas.SetActive (false);
//set timescale to 1
Time.timeScale = 1;
//get the value of highscore
highscore = PlayerPrefs.GetInt ("HighScore",0);
}

IEnumerator SpawnWaves ()
{
//wait of startWait(int) seconds
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < ballCount; i++)
{
//Setting spawn Position randomly between spawnValues
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
//set rotation to the gameObjects identity
Quaternion spawnRotation = Quaternion.identity;
//create ball gameobject at spawnPosition & spawnRotation
Instantiate (GameObjectBall, spawnPosition, spawnRotation);
//wait between every ball drop
yield return new WaitForSeconds (spawnWait);
}
//wait between waves of balls
yield return new WaitForSeconds (waveWait);
//increase time speed & set to time scale
TimeSpeed+=TimeSpeedIncrease;
Time.timeScale=TimeSpeed;
//increase speed multiplier by 1 & showing its value on speedText
speedMultiplier++;
speedText.text = "Speed:"+ speedMultiplier.ToString() +"x";
//LevelUp audio is played
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
//ballCount is increased by ballCountIncrease(int)
ballCount+=ballCountIncrease;
}
}

public void Score(){
//it adds score by 1 when receives message from BounceBack script
score++;
}
public void GameOver(){
//Pause the time
Time.timeScale = 0;
//activate the gameover canvas
GameOverCanvas.SetActive (true);
//fading of canvas is started
StartCoroutine (DoFade());
//start canvas is deactivated
StartCanvas.SetActive (false);
//show the score value on ScoreText
ScoreText.text = "Score:" + score.ToString();
//if score is greater than highscore change the stored value of highscore
if (score > highscore) {
PlayerPrefs.SetInt ("HighScore", score);
}
//show highscore value on HighScoreText
highscore = PlayerPrefs.GetInt ("HighScore",0);
HighScoreText.text = "Highscore:" + highscore.ToString ();
}
public void restart(){
//restart the level
Application.LoadLevel (0);
}
public void exit(){
//exit the game
Application.Quit();
}
public void Pause(){
//pausing the time & activating pause canvas
Time.timeScale = 0;
pausecanvas.SetActive (true);
}
public void Unpause(){
//unpausing the time & deactivating pause canvas
Time.timeScale = TimeSpeed;
pausecanvas.SetActive (false);
}
IEnumerator DoFade(){
//get the canvas to canavsGroup gameobject
CanvasGroup canvasGroup =GameOverCanvas.GetComponent<CanvasGroup> ();
//set visibility to 0
canvasGroup.alpha = 0;
while(canvasGroup.alpha<1){
//increse visibility by 0.05 per frame
canvasGroup.alpha += 0.05f;
yield return null;
}
//set the game object interactable
canvasGroup.interactable = true;
yield return null;
}
}
       Scripts 
GameController.cs                                                                              
             Scripts 
DestroyByBoundry.cs                                                                              
using UnityEngine;
using System.Collections;
/// <summary>
/// This script controls the reduction of lives
/// </summary>
public class LifeMinus : MonoBehaviour {
int lives;
public GameObject life1;
public GameObject life2;
public GameObject life3;
void Start () {
//on game start all 3 lives should be active
life1.SetActive(true);
life2.SetActive(true);
life3.SetActive(true);
lives = 3;
}

void OnTriggerEnter2D(Collider2D other) {
// on missing a ball reducing a life
lives -= 1;
//play sound for reducing life
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
switch (lives) {
//if player has 3 lives all 3 lives gameobject would be active
case 3:
life1.SetActive(true);
life2.SetActive(true);
life3.SetActive(true);
break;
case 2:
//if player has 2 lives only 2 lives gameobject would be active
life1.SetActive(true);
life2.SetActive(true);
life3.SetActive(false);
break;
case 1:
//if player has 1 life only 1 life gameobject would be active
life1.SetActive(true);
life2.SetActive(false);
life3.SetActive(false);
break;
case 0:
//if player has no life no life gameobject would be active
life1.SetActive(false);
life2.SetActive(false);
life3.SetActive(false);
//run the GameOver command in the GameController
GameObject.FindGameObjectWithTag("GameController").SendMessage("GameOver");
break;
default:
//at default none should be visible
life1.SetActive(false);
life2.SetActive(false);
life3.SetActive(false);
break;
}
}
}
     Scripts 
LifeMinus.cs                                                                              
using UnityEngine;
using System.Collections;
/// <summary>
/// This script controls the movement of racket
/// </summary>
public class PlayerController : MonoBehaviour {

// Update is called once per frame
void Update ()
{
if (Input.GetMouseButton (0) && Time.timeScale!=0) {
//Change targetPos to mouse position
Vector3 targetPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
//move the racket's x positon to targerPos
transform.position = Vector3.MoveTowards (transform.position, new Vector3(targetPos.x, -4.5f, 0),10 );
}
}
}
             Scripts 
   PlayerController.cs