I started learning Unity & C# and decided to develop a Tetris game as a practice.
I have most functions working except Instantiating preview pieces which are shown in the right side of the screen.
I have my codes separated into two: one for Tetris block spawner and one for Tetris block.
Tetris block code is applied to each block.
I have a few problems right now.
-
(Whenever a Tetris block is placed, the first block in the preview is then played but the previews that have been instantiated before do not get destroyed, resulting in previews being instantiated on top of one another.) ** Edit : This is fixed**
-
(I also get the “Index was outside the bounds of the array” error although I don’t see why index goes out of bounds.) ** Edit : This is fixed**
-
The “DeleteLine” function does not work now. It was working fine before I added preview of Tetris blocks.
I’m sorry if I’m asking a lot of question at once.
I appreciate any help or hints.
Spawner Code:
public List<GameObject> Sevenbag;
public GameObject[] Baglist; // this is where i store prefabs
private int randomizer;
public GameObject[] CopyPrefab;
private static Vector2[] ColOneNext = new[] { new Vector2(13f, 16f),
new Vector2(13f, 12.5f),
new Vector2(13f, 9f),
new Vector2(13f, 5.5f),
new Vector2(13f, 2f) };
public GameObject CurrentTetrimino;
public bool PreviewUpdate = true;
// Start is called before the first frame update
void Start()
{
BagRandomizer();
NewTetrimino();
}
// Update is called once per frame
void Update()
{
PreviewInitializer();
BagRandomizer();
}
public void NewTetrimino()
{
CurrentTetrimino = Instantiate(Sevenbag[0], transform.position, Quaternion.identity);
CurrentTetrimino.GetComponent<TetrisBlock>().enabled = true;
Sevenbag.RemoveAt(0);
Destroy(GameObject.FindWithTag("PreviewTetrimino"));
PreviewUpdate = true;
}
public void PreviewInitializer()
{
if (Sevenbag.Count() > 10)
{
CopyPrefab = new GameObject[Sevenbag.Count()];
for (int i = 0; i <= 5; i++)
{
CopyPrefab <em>= Instantiate(Sevenbag_, ColOneNext*, Quaternion.identity) as GameObject; //this is where I get the Index outside the bounds of the array error*_</em>
CopyPrefab*.GetComponent().enabled = false;*
CopyPrefab*.tag = “PreviewTetrimino”;*
PreviewUpdate = false;
}
}
else
{
BagRandomizer();
}
}
public void BagRandomizer()
{
while (Sevenbag.Count() <= 14)
{
List randomint = new List();
int randomizer;
for (int i = 0; i < 7; i++)
{
do
{
System.Random r = new System.Random();
randomizer = r.Next(0, 7);
} while (randomint.Contains(randomizer));
randomint.Add(randomizer);
Sevenbag.Add(Baglist[randomint*]);*
Console.Write(Sevenbag*);*
}
}
}
Tetris Block Code:
public static int height = 20;
public static int width = 10;
public static float fallTime = 0.8f;
private float previousTime;
private static Transform[,] grid = new Transform[width, height];
public Vector3 rotationPosition;
public float das_rate = 0.1f;
public float das_check = 0;
private bool CheckHold = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//codes of control of tetris blocks are deleted because they don’t have any bug.
}
bool ValidMove()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
if (roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height)
{
return false;
}
if (grid[roundedX, roundedY] != null)
{
return false;
}
}
return true;
}
void AddToGrid()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
grid[roundedX, roundedY] = children;
}
}
bool HasLine(int i)
{
for (int j = 0; j < width; j++)
{
if (grid[j, i] == null)
return false;
}
return true;
}
void DeleteLine(int i)
{
for (int j = 0; j < width; j++)
{
Destroy(grid[j, i].gameObject);
grid[j, i] = null;
}
}
void RowDown(int i)
{
for (int y = i; y < height; y++)
{
for (int j = 0; j < width; j++)
{
if (grid[j, y] != null)
{
grid[j, y - 1] = grid[j, y];
grid[j, y] = null;
grid[j, y - 1].transform.position += new Vector3(0, -1, 0);
}
}
}
}
void CheckForLines()
{
for (int i = height; i >= 0; i–)
{
if (HasLine(i))
{
DeleteLine(i);
RowDown(i);
}
}
}