2D打砖块小游戏脚本ss

主角脚本

using UnityEngine;
using System.Collections;

public delegate void VoidDelegate();//声明委托
public class BoadControll : MonoBehaviour//主角脚本
{
    bool gameStart = false;
    int score;

    float max = 0;//假设最大的距离等于0
    float speed = 10;//定义速度
    Transform ball;
    UIController uiControl;//声明UI
    Rigidbody2D ballRigid;//声明2D刚体
    public static VoidDelegate scoreChange;//实例化静态委托
    public static VoidDelegate gameWin;

    void Awake()
    {
        scoreChange += AddScore;//出生时绑定加分方法
        gameWin += GameWin;
    }
    void OnDestroy()
    {
        scoreChange -= AddScore;//死亡时解绑加分方法
       gameWin -= GameWin;
    }
    // Use this for initialization
    void Start()
    {
        //计算左右两侧最大和最小距离
        float screenMax = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height)).x;
        float boradHalf = GetComponent<SpriteRenderer>().bounds.extents.x;
        max = screenMax - boradHalf;

        ball = transform.GetChild(0);//获取木板的子节点(球)
        ballRigid = ball.GetComponent<Rigidbody2D>();//获取子节点球上的刚体
        uiControl = GameObject.FindObjectOfType<UIController>();//获取别的脚本中的UI
    }
    // Update is called once per frame
    void Update()
    {
        Move();
        BallControl();
    }

    void Move()//木板的移动
    {
        Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        pos.z = 0;
        pos.y = -3.6f;

        if (pos.x < -max)
        {
            pos.x = -max;
        }
        if (pos.x >= max)
        {
            pos.x = max;
        }
        transform.position = pos;
    }

    void BallControl()//球的弹跳
    {
        if (gameStart == false)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                ball.parent = null;//父节点置为null
                ballRigid.velocity = Vector3.up * speed;//为球刚体施加向上的速度
                gameStart = true;
            }
        }
    }

    void OnCollisionEnter2D(Collision2D collision)//进入2D碰撞器
    {
        if (gameStart)//游戏开始时进入判断
        {
            if (collision.gameObject.CompareTag("ball"))//在碰撞器中获取球的标签
            {
                //球的向量减去木板的向量计算球弹回的方向
                Vector3 dir = Vector3.Normalize(ball.position - transform.position);//Normalize(归一化):当归一化的,一个向量保持相同的方向,但它的长度为1.0。
                ballRigid.velocity = dir * speed;
            }
        }
    }

    public void AgGame()
    {
        gameStart = false;
    }
    public void AddScore()//加分方法
    {
        score++;
        uiControl.SetScore(score);
    }

    void GameWin()
    {
        uiControl.ShowWin();
    }


}

目标脚本

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour//目标脚本
{
    public static int count = 0;
    int hp;
    SpriteRenderer render;
    // Use this for initialization
    void Start()
    {

        count++;
        render = this.GetComponent<SpriteRenderer>();//SpriteRenderer:获取精灵渲染器 

        hp = Random.Range(1, 4);//为物体赋随机生命值
        SetColor(hp);//为不同生命值的球赋初始化颜色
        //Debug.Log("start:"+count);
    }
    void SetColor(int value)//随机给方块儿三种颜色
    {
        switch (value)
        {
            case 1:
                render.color = Color.white;
                break;
            case 2:
                render.color = Color.blue;
                break;
            case 3:
                render.color = Color.red;
                break;
            default:
                break;
        }
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("ball"))
        {
            OnHit();
        }
    }
    public void OnHit()
    {
        //Debug.Log(count);
        hp--;
        if (hp <= 0)
        {
            count--;
            if (count <= 0)
            {
                Time.timeScale = 0;
                BoadControll.gameWin();
            }
            Destroy(this.gameObject);
            //Debug.Log(count);
            BoadControll.scoreChange();//调用板的静态委托相当于调用委托绑定的方法
                                       //Debug.Log("end:" + count);
        }
        else
        {
            SetColor(hp);
        }
    }
}

球脚本

using UnityEngine;
using System.Collections;

public class GameOver : MonoBehaviour//球脚本
{
    Rigidbody2D rigid;//声明球刚体
    UIController uiOver;//声明UI
    BoadControll bo;
    int hp = 3;
    // Use this for initialization
    void Start()
    {
        rigid = GetComponent<Rigidbody2D>();//获取球刚体
        uiOver = GameObject.FindObjectOfType<UIController>();//获取别的脚本
        bo = FindObjectOfType<BoadControll>();
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Over"))
        {
            hp--;
            //rigid.Sleep();
            Again();
            uiOver.SetChance(hp);
            if (hp <= 0)
            {
                Time.timeScale = 0;
                uiOver.ShowOver();
            }
        }
    }

    public void Again()
    {
        transform.position = bo.transform.position;
        Vector3 v = bo.transform.position;
        v.y = -3f;
        transform.position = v;
        this.transform.SetParent(bo.transform, true);
        bo.AgGame();
        rigid.Sleep();
    }
}

UI脚本

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UIController : MonoBehaviour//UI脚本
{

    public Text _scoreText;

    public Text _chance;

    public GameObject winPanel;

    public GameObject _over;
    // Use this for initialization
    void Start()
    {

    }
    public void SetScore(int value)
    {
        _scoreText.text = "分数:" + value;
    }

    public void SetChance(int value)
    {
        _chance.text = "机会:" + value;
    }

    public void ShowWin()
    {
        winPanel.SetActive(true);//设置激活
    }
    public void OnRestartClick()
    {
        Time.timeScale = 1;
        Application.LoadLevel(0);
    }
    public void ShowOver()
    {
        _over.SetActive(true);
    }
    public void OnOverClick()
    {
        Time.timeScale = 1;
        Application.LoadLevel(0);

    }
    public void OnDestroy()
    {
        Brick.count = 0;
    }
}




猜你喜欢

转载自blog.csdn.net/qq_41996509/article/details/80199716