Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Adding extra functionality to a simple Tetris game

Discussion in 'Scripting' started by OliverWilliams, Apr 3, 2016.

  1. OliverWilliams

    OliverWilliams

    Joined:
    Nov 16, 2013
    Posts:
    3
    Hi there all,

    I've been playing with Unity recently - and I've just followed this tutorial on how to make a Tetris clone - and it all went really well - I'm going to fiddle around with it and add some more functionality to it learn a little more about developing in Unity :)

    What I'd like to do is -

    • When a Tetris piece has been placed (as in, it can't go down any more and its position is locked)
      • I'd like to change it's color and the layer it is on
      • I'm pretty sure I can get the exact gameObject I want with - this.gameObject (as it is being placed)
      • I don't know however where exactly the game is registering the piece as 'placed', whether it's on the grid, or the piece

    What is below is the 'abridged' version of the code, as I don't think that all of it will be relevant/important - and it's kinda gross to read a code dump anyways :) Pastebin to the full code - just incase

    Thanks
    Ollie

    P.S. - Sorry if this is in the wrong place, I think the forums have changed since the last time I posted anything :)


    Grid.cs (Global)

    Code (CSharp):
    1.  
    2. ...
    3. public class Grid : MonoBehaviour {
    4.     // The Grid itself
    5.     public static int w = 10;
    6.     public static int h = 20;
    7.     public static Transform[,] grid = new Transform[w, h];
    8.  
    9.     public static Vector2 roundVec2(Vector2 v) {
    10.         return new Vector2(Mathf.Round(v.x),
    11.                            Mathf.Round(v.y));
    12.     }
    13.  
    14.     public static bool insideBorder(Vector2 pos) {
    15.         return ((int)pos.x >= 0 &&
    16.                 (int)pos.x < w &&
    17.                 (int)pos.y >= 0);
    18.     }
    19.    
    20.     ...
    21. }
    22.  

    Group.cs (Attached to each Tetrinemo)
    Code (CSharp):
    1.  
    2. ...
    3. [FONT=Courier New]public class Group : MonoBehaviour {
    4.     // Time since last gravity tick
    5.     float lastFall = 0;
    6.  
    7.     void Start() {
    8.         // Default position not valid? Then it's game over
    9.         if (!isValidGridPos()) {
    10.             Debug.Log("GAME OVER");
    11.             Destroy(gameObject);
    12.         }
    13.     }
    14.  
    15.     void Update() {
    16.         // Move Left
    17.         if (Input.GetKeyDown(KeyCode.LeftArrow)) {
    18.             // Modify position
    19.             transform.position += new Vector3(-1, 0, 0);
    20.  
    21.             // See if valid
    22.             if (isValidGridPos())
    23.                 // It's valid. Update grid.
    24.                 updateGrid();
    25.             else
    26.                 // It's not valid. revert.
    27.                 transform.position += new Vector3(1, 0, 0);
    28.         }
    29.  
    30.         // Move Right
    31.         else if (Input.GetKeyDown(KeyCode.RightArrow)) {
    32.             // Modify position
    33.             transform.position += new Vector3(1, 0, 0);
    34.  
    35.             // See if valid
    36.             if (isValidGridPos())
    37.                 // It's valid. Update grid.
    38.                 updateGrid();
    39.             else
    40.                 // It's not valid. revert.
    41.                 transform.position += new Vector3(-1, 0, 0);
    42.         }
    43.  
    44.         // Rotate
    45.         else if (Input.GetKeyDown(KeyCode.UpArrow)) {
    46.             transform.Rotate(0, 0, -90);
    47.  
    48.             // See if valid
    49.             if (isValidGridPos())
    50.                 // It's valid. Update grid.
    51.                 updateGrid();
    52.             else
    53.                 // It's not valid. revert.
    54.                 transform.Rotate(0, 0, 90);
    55.         }
    56.  
    57.         // Move Downwards and Fall
    58.         else if (Input.GetKeyDown(KeyCode.DownArrow) ||
    59.                  Time.time - lastFall >= 1) {
    60.             // Modify position
    61.             transform.position += new Vector3(0, -1, 0);
    62.  
    63.             // See if valid
    64.             if (isValidGridPos()) {
    65.                 // It's valid. Update grid.
    66.                 updateGrid();
    67.             } else {
    68.                 // It's not valid. revert.
    69.                 transform.position += new Vector3(0, 1, 0);
    70.  
    71.                 // Clear filled horizontal lines
    72.                 Grid.deleteFullRows();
    73.  
    74.                 // Spawn next Group
    75.                 FindObjectOfType<Spawner>().spawnNext();
    76.  
    77.                 // Disable script
    78.                 enabled = false;
    79.             }
    80.  
    81.             lastFall = Time.time;
    82.         }
    83.     }
    84.  
    85.     bool isValidGridPos() {
    86.         foreach (Transform child in transform) {
    87.             Vector2 v = Grid.roundVec2(child.position);
    88.  
    89.             // Not inside Border?
    90.             if (!Grid.insideBorder(v))
    91.                 return false;
    92.  
    93.             // Block in grid cell (and not part of same group)?
    94.             if (Grid.grid[(int)v.x, (int)v.y] != null &&
    95.                 Grid.grid[(int)v.x, (int)v.y].parent != transform)
    96.                 return false;
    97.         }
    98.         return true;
    99.     }
    100.  
    101.     void updateGrid() {
    102.         // Remove old children from grid
    103.         for (int y = 0; y < Grid.h; ++y)
    104.             for (int x = 0; x < Grid.w; ++x)
    105.                 if (Grid.grid[x, y] != null)
    106.                     if (Grid.grid[x, y].parent == transform)
    107.                         Grid.grid[x, y] = null;
    108.  
    109.         // Add new children to grid
    110.         foreach (Transform child in transform) {
    111.             Vector2 v = Grid.roundVec2(child.position);
    112.             Grid.grid[(int)v.x, (int)v.y] = child;
    113.         }
    114.     }
    115. }
    116.  
     

    Attached Files:

    • Grid.cs
      File size:
      1.6 KB
      Views:
      884
    • Group.cs
      File size:
      3.4 KB
      Views:
      819
    Last edited: Apr 3, 2016
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    You should at least format the post better with code tags and tab/space indents
    Code (CSharp):
    1. [code=CSharp]
    2. This is C# code.
    3.  
    [/code]
     
  3. OliverWilliams

    OliverWilliams

    Joined:
    Nov 16, 2013
    Posts:
    3
    Sorry - will do, as I said, I haven't posted here in its current incarnation :)
    Thanks!
     
  4. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    • I'd like to change it's color and the layer it is on
    From just code i can't tell how your objects are colored, that comes from the components you use on objects. If i assume you have SpriteRenderer, then you can
    Code (CSharp):
    1. grid[x, y].GetComponent<SpriteRenderer>().color = new Color(1, 0, 0); // Change to red
    • I don't know however where exactly the game is registering the piece as 'placed', whether it's on the grid, or the piece
    Code (CSharp):
    1. if (grid[x, y]) // <- piece is not null in that position so it is placed
     
  5. OliverWilliams

    OliverWilliams

    Joined:
    Nov 16, 2013
    Posts:
    3
    Sorted it out :) Line 73 in Group.cs is the area where any code should be added that interferes with the placed Tetrinemo
    Thanks for your help all :)