Sirtet

THE ASSIGNMENT:

For this project, my assignment was to create a retro game with a twist. So i decided to recreate the classic tetris game, and turn the rewards around. For this project we where going to learn about singletons, events and listeners.

HOW IT WORKS:

The game is very simple. Each block spawns at the absolute top, and drops down at a specified speed. Each block consists of multiple small 1x1x1 blocks, that constantly check if there are any walls or other placed blocks nearby. If there are they’re not allowed to move. And each time the block hits the ground, the game makes a quick check of all the rows to see if it’s filled or not. If it is, then the game will replace the camera, and increase the playing field in correlation to how many rows have been filled.

CODE:

The hardest part in creating the tetris gameplay for me was to recreate the basic row checking. What i thought first was to create colliders for each row that check the amount of blocks that are inside it. But sadly this didn’t work good enough, so i tried to think of something else. Then i thought of a system where the code checks the width and height of the screen, and checks each row with one block. If there are blocks, the checker skips over the rest and goes on with the next horizontal row.

TETRIS CHECK CODE:

public class TetrisCheck : MonoBehaviour {
	private float width = 20;
	private float height = 20;
	private Vector3 origin;
	private Vector3 newPos;
	private int howManyPoints;


	void Start(){
		origin = this.transform.position;
		newPos = origin;
	}


	void OnEnable(){
		EventManager.CheckTetris += Check;
		EventManager.GotTetris += IncreaseSize;
	}


	void OnDisable(){
		EventManager.CheckTetris -= Check;
		EventManager.GotTetris -= IncreaseSize;	
	}

	//Check for Tetris
	public void Check(){
		while(newPos.y < height){
			while(newPos.x < width){		
				Collider[] hitColliders = Physics.OverlapBox(transform.position,new Vector3(0.1f,0.1f,0.1f),Quaternion.identity);
				if(hitColliders.Length > 0){
					//There is a block in this line
					newPos.x += 2;
					transform.position = newPos;
				} else { 
					//No Blocks = No Tetris = One line up
					newPos.x = origin.x;
					newPos.y += 2;
					transform.position = newPos;
					break;
				}
			}

			if(newPos.x >= width){
				//If there is a tetris
				howManyPoints ++;
				newPos.x = origin.x;
				newPos.y += 2;
				transform.position = newPos;
			}	
			
		}

		if(newPos.y >= height){
			for(int i = 0; i < howManyPoints; i++){
				//Ativate Tetris as much as the aquired points
				EventManager.GoTetris();
			}	
			howManyPoints = 0;
			newPos.y = origin.y;
			newPos.x = origin.x;
			transform.position = origin;		
		}	
	}


	public void IncreaseSize(){
		width += 2;
		height += 2;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *