Above, you can see the set up of our bread board in order to create the buttons for the game controls. Below is a screenshot of the Tetris gameplay in Unity.
This code (above) spawns random Tetris pieces at the top center of the screen. The "groups" consist of each prefab for individual Tetris shapes (I, J, L, O, S, Z, T).
This code (above) does the following:
Check if all the blocks are between the borders
Check if all the blocks are above y=0
Check if a group can be moved towards a certain position
Check if a row is full of blocks
Delete a row
This Code demonstrates how our buttons move the Tetris pieces:
void Update()
{
if (sp.IsOpen)
{
try
{
// MOVE LEFT
if (sp.ReadByte() == 1)
{
// Modify position
transform.position += new Vector3(-1, 0, 0);
// See if valid
if (isValidGridPos())
// Its valid. Update grid.
updateGrid();
else
// Its not valid. revert.
transform.position += new Vector3(1, 0, 0);
}
//MOVE RIGHT
else if (sp.ReadByte() == 2)
{
// Modify position
transform.position += new Vector3(1, 0, 0);
// See if valid
if (isValidGridPos())
// It's valid. Update grid.
updateGrid();
else
// It's not valid. revert.
transform.position += new Vector3(-1, 0, 0);
}
//ROTATE
else if (sp.ReadByte() == 3)
{
transform.Rotate(0, 0, -90);
// See if valid
if (isValidGridPos())
// It's valid. Update grid.
updateGrid();
else
// It's not valid. revert.
transform.Rotate(0, 0, 90);
}
//FALL
else if (sp.ReadByte() == 4)
{
// Modify position
transform.position += new Vector3(0, -1, 0);
// See if valid
if (isValidGridPos())
{
// It's valid. Update grid.
updateGrid();
}
else
{
// It's not valid. revert.
transform.position += new Vector3(0, 1, 0);
// Clear filled horizontal lines
GameGrid.deleteFullRows();
// Spawn next Group
FindObjectOfType<Spawner>().spawnNext();
// Disable script
enabled = false;
}
}
}
catch (System.Exception)
{
}
}
This video shows the usage of the buttons:
Challenge:
One of the challenges of this assignment seems to be the hardware, as seen in the video, the second button from the left on the arduino board only functions occasionally, and it needs to be pressed very hard in order to work, all other buttons work, but their inputs seems to be very delayed/laggy.
Comments