使用Unity来制作俄罗斯方块游戏

1. 操作环境

Unity3D 4.1.0版本、Win 7

备注:该方法并非本人原创,我也是根据别人的代码来学习的。

2. 思路分析

该方法中,只有2个脚本,一个是控制方块的(Block.cs),另外一个是控制游戏场景的(Manager.cs)。游戏完成后效果如下图:
使用Unity来制作俄罗斯方块游戏_第1张图片

2.1 方块的构造(Block.cs)

俄罗斯方块一共有7种不同的方块。每个种类有4个小方块组成的。我们使用一个string[ ]数组来记录方块的形状。
使用Unity来制作俄罗斯方块游戏_第2张图片
从上图我们可以看出,1表示有方块,0表示没有方块。我们就可以根据该数组来实例化了,而且该数组的长度和宽度是相同的。这样方便我们对他进行旋转、移动等操作。

2.2 游戏场景(Manager.cs)

我们通过自己的喜欢来设计场景,但是要保证宽度必须是小方块的整数倍。不然就会产生无法填满的结果了。在这里我设计了领域宽度为18单位(FieldWidth = 18),领域高度为19单位(FieldHeight = 19)。我们定义个bool值数组来表示领域中的状态,0表示该处为空(即无方块),1表示有方块存在。我们对方块的操作都需要来监测场景中的状态,当满足其状态要求时,就可以执行相应的操作了。

3. 代码

Block.CS
using UnityEngine;
using System.Collections;

public class Block : MonoBehaviour {
      
	//根据字符串来定义方块的形状//
	public string[] block;
	
	private bool[,] blockMatrix;//方块的矩阵/
	private float fallSpeed;//下降速度/
	private int halfSize;
	private float halfSizeFloat;
	private int xPosition;
	private int yPosition;
	private bool dropped=false;//是否下降/
	private int size;
	
	private float pressInterval=0;//按键时间间隔/
	
	void Start () {	
	
		size=block.Length;
		int width=block[0].Length;		
		//不符合条件的方块弹出错误信息//
		if(size<2){
		
			Debug.LogError("Block must have at lest two lines!");
			return;
		}
		if(width != size){
		
			Debug.LogError("Block width and height must be the same!");
			return;
		}
		if(size > Manager.user.maxBlockSize){
		
			Debug.LogError("Block must not be larger than "+Manager.user.maxBlockSize);
			return;
		}
		for(int i=1;i
	/// Delay the specified time.
	/// 	
	IEnumerator Delay(float time){
	
		float t=0.0f;
		while(t

Manager.cs
using UnityEngine;
using System.Collections;

public class Manager : MonoBehaviour {        
	
	public int FieldWidth = 18;//领域宽度/
	public int FieldHeight = 19;//领域高度/
	public int maxBlockSize = 4;//最大方块尺寸/
	public float blockNormalSpeed=2.0f;//方块正常下降速度//
	public int blockDropSpeed=30;//方块下降速度/
	public float blockMoveDelay=0.1f;//方块移动延迟/
	public int rowsClearedToSpeedup=10;//行清加速/
	public float speedupAmount=0.5f;//加速数量/
	public GameObject[] blocks;//块//
	public Transform cube;//盒子//	
	
	public Transform leftWall;//左边墙壁
	public Transform rightWall;//右边墙壁
	
	private int fieldWidth;
	private int fieldHeight;
	private bool[,] field;//场景区域的bool值/
	private Transform[] cubeReferences;
	private  int[] cubePositions;
	private int rowsCleared =0;
	public static Manager user;
	
	void Start () {
	
		if(!user){
		
			user=this;
		}else{
		
			Debug.LogError("在这个脚本中只允许存在一个实例!");
			return;
		}
		//场景区域宽度增加2*最大方块尺寸(左右各一个)/
		fieldWidth = FieldWidth + maxBlockSize*2;
		//场景区域高度增加1*最大方块尺寸(顶端)/
		fieldHeight = FieldHeight + maxBlockSize;
		field = new bool[fieldWidth,fieldHeight];
		
		//将墙壁放入到field数组中,true=block,false=open
		//0=bottom,fieldHeight-1=top
		for(int i=0;i
	/// 监测blockMatrix是否已经存在block
	/// 我们从bottom向top监测
	/// 	
	public bool CheckBlock(bool[,] blockMatrix,int xPos,int yPos){
	
		//GetLength(0)获取第一维元素的长度//
		int size=blockMatrix.GetLength(0);
		//监测顺序为bottom-top,left-right
		for(int y=size-1;y>=0;y--){
		
			for(int x=0;x
	/// 监测屏幕上停止的方块
	/// 仅仅使用孩子物体是不行的,因为孩子方块的方向是不同的
	/// 使用Y轴会使他们位置混乱的,所以我们要使用一致的CollapseRow
	/// 在领域中,我们将blockMatrix写入到相应的位置
	/// 	
	public void SetBlock(bool[,] blockMatrix, int xPos,int yPos){
	
		int size=blockMatrix.GetLength(0);
		for(int y=0;y
	/// 监测领域中的每一行/
	/// 	
	public IEnumerator CheckRows(int yStart,int size){
	
		//等待一帧//
		yield return 0;
		if(yStart<1)
			yStart=1;//确保从bottom开始//
		for(int y=yStart;y
	/// 消除一行
	/// 	
	public IEnumerator CollapseRows(int yStart){
	
		//将数组中的行下移,最有效的就是删除当前行(yStart)/
		for(int y=yStart;yyStart){
			
				cubePositions[cubeToMove]=(int)c.transform.position.y;
				cubeReferences[cubeToMove++]=c.transform;
			}else if((int)(c.transform.position.y)==yStart){
			
				//销毁/
				Destroy(c);
			}
		}
		
		//将靠近的方块下一个立方/
		//Mathf.Lerp的第三个参数固定在1.0,这样使transform.position.y更加的精确。/
		float t=0.0f;
		while(t<=1.0f){
		
			t += Time.deltaTime*5.0f;
			for(int i=0;i
	/// Games the over.
	/// 
	public void GameOver(){
	
		Debug.Log("Game Over!");
	}
	
	/// 
	/// Prints the field.用于Debug
	/// 
	void PrintField(){
		
		string fieldChars="";
		for(int y=fieldHeight-1;y>=0;y--){
		
			for(int x=0;x

4. 项目下载和讨论

大家可以到我的CSDN资源中下载~
如果有什么问题,大家可以给我留言,我尽快给大家回复~


你可能感兴趣的:(Unity3D,unity,俄罗斯方块)