泰课课程-别踩白块儿

下载泰课该课程的资源包->导入unity

首先有一个较明确的框架:1.StartUI  2.GameUI

StartUI如下图

泰课课程-别踩白块儿_第1张图片

GameUI如下图

泰课课程-别踩白块儿_第2张图片

注意:在应用图片素材是时要更改参数,否则无法apply(我用的是5.3.4)

泰课课程-别踩白块儿_第3张图片

开始按钮的浮动

public class Floating : MonoBehaviour {
    private float radian = 0;
    public float perRadian = 0.02f;
    public float radius = 0.2f;
    private Vector3 oldPos;
	// Use this for initialization
	void Start () {
        oldPos = transform.position;//记录当前位置
	
	}
	
	// Update is called once per frame
	void Update () {
        radian += perRadian;
        float dy = Mathf.Cos(radian)*radius;
        transform.position = oldPos + new Vector3(0, dy, 0);
    }
}


游戏开始和进行的UI完成后,我们需要完成开始和返回的交互,建立一个游戏控制器脚本

public class StartButton : MonoBehaviour {
    private GameController gameController;
	// Use this for initialization
	void Start () {
        gameController = GameObject.FindObjectOfType();
	}
	
	// Update is called once per frame
	void OnMouseDown () {
       gameController.EnterGameUI();

	}
}
public class ReturnButton : MonoBehaviour {
    private GameController gameController;
       
	// Use this for initialization
	void Start () {
        gameController = GameObject.FindObjectOfType();
        //print("StarButton gameController"+gameController);
	}
	
	// Update is called once per frame
	void OnMouseDown() {
        gameController.ReturnStartUI();
	}
}



  public void EnterGameUI() {
        startUI.SetActive(false);
        gameUI.SetActive(true);
        //开始游戏
        StartGame();
        //绘制GUI
        showScore = true;
    }
    //返回主界面
    public void ReturnStartUI() {
        startUI.SetActive(true);
        gameUI.SetActive(false);
        showScore = false;
        //清理游戏状态数据
        Clean();
        //重置分数
        score = 0;
    }
    private void StartGame() {
        blocks = new ArrayList();
        for (int rowIndex = 0; rowIndex < 4; rowIndex++) {
            AddBlock(rowIndex);
            //不绘制UI
    }
}

开始和游戏界面交互完成后开始在游戏界面指定行增加色块(放在游戏控制器下)
void AddBlock(int rowIndex) {
        float rnd = Random.Range(0f, 1f);
        GameObject prefabs = rnd > 0.2? yellowBlock :whiteBlock;
    GameObject o = Instantiate(whiteBlock) as GameObject;
    o.transform.parent = Container.transform;
    Block b = o.GetComponent();
    int columnIndex = Random.Range(0, 4);
    b.SetPosition(columnIndex, rowIndex);
        //添加到Blocks数组中
        blocks.Add(b);
  }

接下来是进行色块的移动

public class Block : MonoBehaviour
{
    //不能触摸
    public bool cantTouch = false;
    //设置坐标偏移量
    private float xOff = -3.3f;
    private float yOff = -6.85f;
    private GameController gameController;
    public int rowIndex;
    public int columnIndex;
    // Use this for initialization
    void Start()
    {
        gameController = GameObject.FindObjectOfType();
    }

    // Update is called once per frame
    void OnMouseDown()
    {
        print("Block OnMouseDown");
        gameController.Select(this);
        //gameController.EnterGameUI();
        //int columnIndex=Random.Range(0,3);
        //SetPOsition(columnIndex, 0);
    }
    public void SetPosition(int columnIndex, int rowIndex)
    {
        this.rowIndex = rowIndex;
        this.columnIndex = columnIndex;
        gameObject.transform.position = new Vector3(xOff + columnIndex * 2.0f, yOff + rowIndex * 4.5f, 0);
    }
    //色块往下移动一行
    public void MoveDown() {
        SetPosition(columnIndex, rowIndex - 1);
    }
}
色块的选择及随机生成

void AddBlock(int rowIndex) {
        float rnd = Random.Range(0f, 1f);
        GameObject prefabs = rnd > 0.2? yellowBlock :whiteBlock;
    GameObject o = Instantiate(whiteBlock) as GameObject;
    o.transform.parent = Container.transform;
    Block b = o.GetComponent();
    int columnIndex = Random.Range(0, 4);
    b.SetPosition(columnIndex, rowIndex);
        //添加到Blocks数组中
        blocks.Add(b);
  }

色块的删除

 public void Select(Block block) {

        GetComponent();//播放点击音效
        score += block.cantTouch ? -5 : 1;//计算分数
        for (int i = 0; i < blocks.Count; i++)
        {
            Block b = (Block)blocks[i];
            b.MoveDown();//下降色块
            if (b.rowIndex == -1) {
                blocks.RemoveAt(i);
                Destroy(b.gameObject);//从视图中删除
                i--;//for循环中为了使后来的索引不出错,还可在for循环时从i--开始
            }
        }
        //在最上面添加一个
        AddBlock(3);
    }
游戏计分

public class UI : MonoBehaviour {

    // Use this for initialization
    void OnGUI()
    {
        if (GameController.showScore)
        {
            GUIStyle style = new GUIStyle();
            style.normal.textColor = Color.black;
            style.fontSize = 50;
            style.fontStyle = FontStyle.Bold;
            GUI.Label(new Rect(150, 55, 90, 60), "score:" + GameController.score);
        }
    }
	
}
返回时清理游戏状态

public void Clean() {
        for (int i = 0; i < blocks.Count; i++)
        {
            Block b = (Block)blocks[i];
            blocks.RemoveAt(i);
            Destroy(b.gameObject);
            i--;
        }
    
 }

.....游戏还在改进中

     未完待续




你可能感兴趣的:(泰课课程-别踩白块儿)