- Home /
Having trouble adding points to my block matching game C#
In the block matching game that I'm currently working on, I have the ability for the game to destroy multiple blocks at a given time, but what I want after the blocks are clicked and destroyed is I want the game to recognize how many blocks have been destroyed and then add points based off of that number. For example: - if 2 blocks - 4 points - if 3 blocks - 9 points - if 4 blocks - 16 points - etc
The code below is where I'm destroying groups of blocks. Would I need to add the points here? Or call it later in an OnDestroy function? Help is much appreciated!
public void DestroyBlock(GameObject clickedBlock) {
Vector2 blockPos = GetBlockPos(clickedBlock);
if (blockPos != new Vector2(-1,-1)) {
Color texType = clickedBlock.renderer.material.color;
clickedBlock.renderer.material.color = new Color(1f,1f,1f,0f);
// Destroy blocks moving up
if (blockPos.y > 0) {
if (grid[(int)blockPos.x,(int)blockPos.y-1].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x, (int) blockPos.y - 1]);
}
}
// Destroy blocks going right
if (blockPos.x > 0) {
if (grid[(int)blockPos.x-1,(int)blockPos.y].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x-1, (int) blockPos.y]);
}
}
// Destroy blocks going down
if (blockPos.y < grid.GetLength(1)-1) {
if (grid[(int)blockPos.x,(int)blockPos.y+1].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x, (int) blockPos.y + 1]);
}
}
// Destroy blocks going left
if (blockPos.x < grid.GetLength (1)-1) {
if (grid[(int)blockPos.x+1,(int)blockPos.y].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x+1, (int) blockPos.y]);
}
}
}
}
Answer by RudyTheDev · Feb 15, 2014 at 11:43 AM
One way doing it with pure recursion (no outside variables) would be:
public int DestroyBlock(GameObject clickedBlock)
{
int blocksDestroyed = 0; // keep a counter of how many blocks this function has destroyed
Vector2 blockPos = GetBlockPos(clickedBlock);
if (blockPos != new Vector2(-1, -1))
{
Color texType = clickedBlock.renderer.material.color;
clickedBlock.renderer.material.color = new Color(1f, 1f, 1f, 0f);
blocksDestroyed++; // count this as 1 block destroyed
// Destroy blocks moving up
if (blockPos.y > 0)
{
if (grid[(int)blockPos.x, (int)blockPos.y - 1].renderer.material.color == texType)
{
blocksDestroyed += DestroyBlock(grid[(int)blockPos.x, (int)blockPos.y - 1]);
}
}
// Destroy blocks going right
if (blockPos.x > 0)
{
if (grid[(int)blockPos.x - 1, (int)blockPos.y].renderer.material.color == texType)
{
blocksDestroyed += DestroyBlock(grid[(int)blockPos.x - 1, (int)blockPos.y]);
}
}
// Destroy blocks going down
if (blockPos.y < grid.GetLength(1) - 1)
{
if (grid[(int)blockPos.x, (int)blockPos.y + 1].renderer.material.color == texType)
{
blocksDestroyed += DestroyBlock(grid[(int)blockPos.x, (int)blockPos.y + 1]);
}
}
// Destroy blocks going left
if (blockPos.x < grid.GetLength(1) - 1)
{
if (grid[(int)blockPos.x + 1, (int)blockPos.y].renderer.material.color == texType)
{
blocksDestroyed += DestroyBlock(grid[(int)blockPos.x + 1, (int)blockPos.y]);
}
}
}
return blocksDestroyed; // return how many blocks we have destroyed including the total between the recursion branches
}
DestroyBlock()
returns how may blocks it destroyed directly plus the total of how many block each of the recursive branches destroyed. The outmost call to DestroyBlock()
will then return the total sum of blocks destroyed between all the DestroyBlock()
s. Then do your score calculation with whatever value DestroyBlock()
returns.
Thank you for your quick and accurate response. This works perfectly in my game and it returns the exact amount of blocks destroyed. Thanks again!
Your answer
Follow this Question
Related Questions
Shifting blocks to the right to remove empty spaces - C# 0 Answers
Trouble with Round Generating 0 Answers
Finding an object's neighbor C# 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers