Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SlyyGuyy · Feb 14, 2014 at 08:00 PM · c#blocksmatching

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]);    
                     }
                 }
             }
         }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SlyyGuyy · Feb 15, 2014 at 08:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges