using UnityEngine; using System.Collections; using System.Collections.Generic; public class Tetris : MonoBehaviour { //Board public int[,] board; //Block public Transform block; //Spawn boolean public bool spawn; //Seconds before next block spawn public float nextBlockSpawnTime = 0.5f; //Block fall speed public float blockFallSpeed = 0.5f; //Game over level public int gameOverHeight = 22; //20 board + 2 edge //Current spawned shapes private List shapes = new List(); //Set true if game over private bool gameOver; //Current rotation of an object private int currentRot = 0; //Current pivot of the shape private GameObject pivot; void Start(){ //Deafult board is 10x16 //1+10+1 - Side edge //+2 - Space for spawning //+1 - Top edge //20 - Height //+1 - Down edge board = new int[12,24];//Set board width and height GenBoard();//Generate board InvokeRepeating("moveDown",blockFallSpeed,blockFallSpeed); //move block down } void Update(){ if(spawn && shapes.Count == 4){ //If there is block //Get spawned blocks positions Vector3 a = shapes[0].transform.position; Vector3 b = shapes[1].transform.position; Vector3 d = shapes[2].transform.position; Vector3 c = shapes[3].transform.position; if(Input.GetKeyDown(KeyCode.LeftArrow)){//Move left if(CheckUserMove(a,b,c,d,true)){//Check if we can move it left a.x-=1; b.x-=1; c.x-=1; d.x-=1; pivot.transform.position = new Vector3(pivot.transform.position.x-1, pivot.transform.position.y, pivot.transform.position.z); shapes[0].transform.position = a; shapes[1].transform.position = b; shapes[2].transform.position = c; shapes[3].transform.position = d; } } if(Input.GetKeyDown(KeyCode.RightArrow)){//Move right if(CheckUserMove(a,b,c,d,false)){//Check if we can move it right a.x+=1; b.x+=1; c.x+=1; d.x+=1; pivot.transform.position = new Vector3(pivot.transform.position.x+1, pivot.transform.position.y, pivot.transform.position.z); shapes[0].transform.position = a; shapes[1].transform.position = b; shapes[2].transform.position = c; shapes[3].transform.position = d; } } if(Input.GetKey(KeyCode.DownArrow)){ //Move down fast moveDown(); } if(Input.GetKeyDown(KeyCode.Space)){ //Rotate Rotate(shapes[0].transform,shapes[1].transform,shapes[2].transform,shapes[3].transform); } } if(!spawn && !gameOver){//If nothing spawned, if game over = false, then spawn StartCoroutine("Wait"); spawn = true; //Reset rotation currentRot = 0; } } void moveDown(){ //Spawned blocks positions if(shapes.Count!=4){ return; } Vector3 a = shapes[0].transform.position; Vector3 b = shapes[1].transform.position; Vector3 c = shapes[2].transform.position; Vector3 d = shapes[3].transform.position; if(CheckMove(a,b,c,d)==true){ // Will we hit anything if we move block down(true = we can move) //Move block down by 1 a = new Vector3(Mathf.RoundToInt(a.x),Mathf.RoundToInt(a.y-1.0f),a.z); b = new Vector3(Mathf.RoundToInt(b.x),Mathf.RoundToInt(b.y-1.0f),b.z); c = new Vector3(Mathf.RoundToInt(c.x),Mathf.RoundToInt(c.y-1.0f),c.z); d = new Vector3(Mathf.RoundToInt(d.x),Mathf.RoundToInt(d.y-1.0f),d.z); pivot.transform.position = new Vector3(pivot.transform.position.x, pivot.transform.position.y-1, pivot.transform.position.z); shapes[0].transform.position = a; shapes[1].transform.position = b; shapes[2].transform.position = c; shapes[3].transform.position = d; } else{ //We hit something. Stop and mark current shape location as filled in board, also destroy last pivot gameobject Destroy(pivot.gameObject); //Destroy pivot //Set ID in board board[Mathf.RoundToInt(a.x),Mathf.RoundToInt(a.y)]=1; board[Mathf.RoundToInt(b.x),Mathf.RoundToInt(b.y)]=1; board[Mathf.RoundToInt(c.x),Mathf.RoundToInt(c.y)]=1; board[Mathf.RoundToInt(d.x),Mathf.RoundToInt(d.y)]=1; //**************************************************** checkRow(1); //Check for any match checkRow(gameOverHeight); //Check for game over //**************************************************** shapes.Clear(); //Clear spawned blocks from array spawn = false; //Spawn a new block } } //Wait time before next block spawn IEnumerator Wait(){ yield return new WaitForSeconds(nextBlockSpawnTime); SpawnShape(); } bool CheckMove(Vector3 a, Vector3 b, Vector3 c, Vector3 d){ //Check, if we move a block down will it hit something if(board[Mathf.RoundToInt(a.x),Mathf.RoundToInt(a.y-1)]==1){ return false; } if(board[Mathf.RoundToInt(b.x),Mathf.RoundToInt(b.y-1)]==1){ return false; } if(board[Mathf.RoundToInt(c.x),Mathf.RoundToInt(c.y-1)]==1){ return false; } if(board[Mathf.RoundToInt(d.x),Mathf.RoundToInt(d.y-1)]==1){ return false; } return true; } bool CheckUserMove(Vector3 a, Vector3 b, Vector3 c, Vector3 d, bool dir){ //Check, if we move a block left/right will it hit something if(dir){//Left if(board[Mathf.RoundToInt(a.x-1),Mathf.RoundToInt(a.y)]==1 || board[Mathf.RoundToInt(b.x-1),Mathf.RoundToInt(b.y)]==1 || board[Mathf.RoundToInt(c.x-1),Mathf.RoundToInt(c.y)]==1 || board[Mathf.RoundToInt(d.x-1),Mathf.RoundToInt(d.y)]==1){ return false; } } else{//Right if(board[Mathf.RoundToInt(a.x+1),Mathf.RoundToInt(a.y)]==1 || board[Mathf.RoundToInt(b.x+1),Mathf.RoundToInt(b.y)]==1 || board[Mathf.RoundToInt(c.x+1),Mathf.RoundToInt(c.y)]==1 || board[Mathf.RoundToInt(d.x+1),Mathf.RoundToInt(d.y)]==1){ return false; } } return true; } void GenBoard(){ for(int x=0; x0){ if(y>0 && y0){//If the current height is game over height, and there is more than 0 block, then game over Debug.LogWarning("Game over"); gameOver = true; } if(count==10){//The row is full //Start from bottom of the board(withouth edge and block spawn space) for(int cy=y; cy y){ board[xPos,height] = 0;//Set old position to empty board[xPos,height-1] = 1;//Set new position go.transform.position = new Vector3(xPos, height-1, go.transform.position.z);//Move block down } } } } } checkRow(y); //We moved blocks down, check again this row } else if(y+1