Search Unity

[Tutorial]Tetris game

Discussion in 'Community Learning & Teaching' started by Pakarat, Jun 2, 2014.

  1. Pakarat

    Pakarat

    Joined:
    Jan 13, 2014
    Posts:
    19
    Hello,

    By the Able_Elizalde request, I will start the tutorial about ,,How to create a simple tetris game".





    So far I've never tried to make a tetris game. And this may not be the best way to do tetris style game.
    You can test final result.

    In these tutorials I will show you how to:
    1. Generate board
    2. Create a shape
    3. Move shape down
    4. Collision between other blocks and edge
    5. User input
    6. Block rotation
    7. Check if there is full row, destroy and move blocks down

    The first part of this tutorial is here.
    The second part of this tutorial is here.
     
    Last edited: Jun 3, 2014
    Shadeless and Narattitude like this.
  2. Pakarat

    Pakarat

    Joined:
    Jan 13, 2014
    Posts:
    19
  3. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    That's awesome! Thank you so much! :D I'll start following it soon and I'll let you know how it feels to follow it!
     
  4. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    I followed it!! :D

    I imagine some parts are a bit confusing for total beginners, a few of the code snippets aren't closing {}, but if the reader knows what they're doing it's an easy fix ^^

    Well, I didn't quite make a Tetris with it, but I made something close enough xD and it's pretty cool

    https://dl.dropboxusercontent.com/u/10197361/BlobbyTetrisThingie/WebBuild.html

    Now I gotta add scoring and stuff :3 gonna be awesome

    thanks for the tutorial!
     
  5. Abel_Elizalde

    Abel_Elizalde

    Joined:
    May 10, 2014
    Posts:
    7
    Cool!, I'll look in to it thanks, for taking time to actually talk and share with the community, we need more people like you around here :p.
     
  6. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
    hey i was wondering how you got the blocks to look like that and also can you share the code for the random color generation?
     
  7. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    What do you mean look like that? o_O they're regular old unity primitive cubes.

    I don't have the full code with me right now, it's in my other machine, but I basically picked a random number from the amount of colors I had at the moment using Random.Range http://docs.unity3d.com/ScriptReference/Random.Range.html

    Then based on the number that came out I used iTween ColorTo http://itween.pixelplacement.com/documentation.php#ColorTo to change the color to the desired one.
     
  8. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
    oh ok thank you for the speedy reply! i see what you mean... worked like a charm, but in the instance of the primitives do how would i get the lighting like that?
     
  9. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    I think you might be talking about the Bloom effect I used. It's from Unity Pro Image Effects.

    Other than that, it's just directional light.
     
  10. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
    hmm i might be.... Im new to unity lol i know different programming languages but unity is still foreign territory lol.... thank you again 'oh great sensei'

    BTW great new game too.... i loved it
     
  11. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    Thanks! :D Do you mean the Coop Action Game Kit?
     
  12. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
  13. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
    ok i have basically everything but im trying to understand the creating of the blocks function SpawnShape(); i dont quite understand where or what the variable of GenBlock derived from.... i mean is it a function or Array? can someone plz help me understand.... btw im converting the script to js.

    //SShapeif(shape==0){ pivot.transform.position = new Vector3(xPos,height+1, 0); shapes.Add(GenBlock(new Vector3(xPos, height,0)));shapes.Add(GenBlock(new Vector3(xPos-1, height,0)));
    shapes.Add(GenBlock(new Vector3(xPos, height+1,0)));
    shapes.Add(GenBlock(new Vector3(xPos+1, height+1,0)));
    Debug.Log("Spawned SShape");
    }
     
  14. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    GenBlock is a function that spawns the block at the desired spot.

    Code (csharp):
    1.  
    2. //This is a function that returns a transform, it's given a Vector3 called "pos".
    3. Transform GenBlock(Vector3 pos){
    4.        //create a new transform called "obj" instantiating block in the position "pos".
    5.        Transform obj = (Transform)Instantiate(block.transform, pos, Quaternion.identity) as Transform;
    6.        //Our newly created object should be of tag "Block"
    7.         obj.tag = "Block";
    8.     //Return to whoever called this function the "obj" transform. (Our newly created transform).
    9.     return obj;
    10. }
     
  15. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
    hmmmmm do you know if their is a way to do the same thing with js and STILL have the operations work properly??
     
  16. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    Of course there is :D

    I just don't know how to. xD
     
  17. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
    lol oh well im sure i can figure it out lol... if you can help me lastly, i just need this code explained. I,m trying to understand how it works. mainly because i am trying to recreate it in js by using 3d arrays

    Code (CSharp):
    1. void GenBoard(){
    2.     for(int x=0; x<board.GetLength(0);x++){
    3.         for(int y=0; y<board.GetLength(1);y++){
    4.             if(x<11 && x>0){
    5.                 if(y>0 && y<board.GetLength(1)-2){
    6.                     //Board
    7.                     board[x,y]=0;
    8.                     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    9.                     cube.transform.position = new Vector3(x,y,1);
    10.                     Material material = new Material(Shader.Find("Diffuse"));
    11.                     material.color = Color.grey;
    12.                     cube.renderer.material = material;
    13.                     cube.transform.parent = transform;
    14.                 }
    15.                 else if(y<board.GetLength(1)-2){
    16.                     board[x,y]=1;
    17.                     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    18.                     cube.transform.position = new Vector3(x,y,0);
    19.                     Material material = new Material(Shader.Find("Diffuse"));
    20.                     material.color = Color.black;
    21.                     cube.renderer.material = material;
    22.                     cube.transform.parent = transform;
    23.                     cube.collider.isTrigger = true;
    24.                    
    25.                 }
    26.             }
    27.             else if((y<board.GetLength(1)-2)){
    28.                 //Left and right edge
    29.                 board[x,y]=1;
    30.                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    31.                 cube.transform.position = new Vector3(x,y,0);
    32.                 Material material = new Material(Shader.Find("Diffuse"));
    33.                 material.color = Color.black;
    34.                 cube.renderer.material = material;
    35.                 cube.transform.parent = transform;
    36.             }
    37.         }
    38.     }
    39. }
     
  18. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    I have no idea why you're recreating it in JS... C# is way more awesome ;o

    Code (csharp):
    1.  
    2. void GenBoard(){
    3.     //For each x = 0, while x is less than Length of the array Board 0. After running it increase x by 1
    4.     for(int x=0; x<board.GetLength(0);x++){
    5.         //For each y = 0, while y is less than Length of the array Board 1. After running it increase y by 1
    6.         for(int y=0; y<board.GetLength(1);y++){
    7.             //If X is a number between 11 and 0
    8.             if(x<11 && x>0){
    9.                 //And y is a number between 0 and the length of the board in y minus 2.
    10.                 if(y>0 && y<board.GetLength(1)-2){
    11.                     //Then this value on the board should be 0.
    12.                     board[x,y]=0;
    13.                     //Let's create a cube on this spot.
    14.                     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    15.                     //And position this newly created cube at the X position that's the current X of our foreach loop,
    16.                     //and at the Y position that's the current Y position of our foreach loop. Lastly, 1 at Z position.
    17.                     cube.transform.position = new Vector3(x,y,1);
    18.                     //Let's createa new material for our newly created cube
    19.                     Material material = new Material(Shader.Find("Diffuse"));
    20.                     //And make it's color to be grey.
    21.                     material.color = Color.grey;
    22.                     //Let's assign the material to our newly created cube.
    23.                     cube.renderer.material = material;
    24.                     //And make it our child.
    25.                     cube.transform.parent = transform;
    26.                 }
    27.                 //Else if y is less than board y length -2.
    28.                 else if(y<board.GetLength(1)-2){
    29.                     //Then this value on the board should be 1.
    30.                     board[x,y]=1;
    31.                     //Let's create a cube on this spot.
    32.                     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    33.                     //And position this newly created cube at the X position that's the current X of our foreach loop,
    34.                     //and at the Y position that's the current Y position of our foreach loop. Lastly, 0 at Z position.
    35.                     cube.transform.position = new Vector3(x,y,0);
    36.                     //Let's createa new material for our newly created cube
    37.                     Material material = new Material(Shader.Find("Diffuse"));
    38.                     //And make it's color to be black.
    39.                     material.color = Color.black;
    40.                     //Let's assign the material to our newly created cube.
    41.                     cube.renderer.material = material;
    42.                     //And make it our child.
    43.                     cube.transform.parent = transform;
    44.                     //Oh, and the collider of this one should actually be a trigger instead of a collider.
    45.                     cube.collider.isTrigger = true;
    46.                 }
    47.             }
    48.             //Else if y is less than board y length -2...
    49.             else if((y<board.GetLength(1)-2)){
    50.                 //Then this value on the board should be 1.
    51.                 board[x,y]=1;
    52.                 //Let's create a cube on this spot.
    53.                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    54.                 //And position this newly created cube at the X position that's the current X of our foreach loop,
    55.                 //and at the Y position that's the current Y position of our foreach loop. Lastly, 0 at Z position.
    56.                 cube.transform.position = new Vector3(x,y,0);
    57.                 //Let's createa new material for our newly created cube
    58.                 Material material = new Material(Shader.Find("Diffuse"));
    59.                 //And make it's color to be black.
    60.                 material.color = Color.black;
    61.                 //Let's assign the material to our newly created cube.
    62.                 cube.renderer.material = material;
    63.                 //And make it our child.
    64.                 cube.transform.parent = transform;
    65.             }
    66.         }
    67.     }
    68. }
    69.  
     
  19. Talintid

    Talintid

    Joined:
    Jul 25, 2014
    Posts:
    8
    lol i dont know c#... i know js from creating websites lls
     
  20. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    It's quite similar as you've might have noticed... So... As good a time as any to learn C# ;D
     
  21. louisch

    louisch

    Joined:
    Apr 19, 2014
    Posts:
    3
    I know that this is probably your first foray into scripting, or programming, so your code is bound to have several 'newbie mistakes', but still, I want to point out one thing at least, which is the redundancy in GenBoard.
    Often, if you find yourself seeing large patterns in your code, or basically, repetition, there's a fairly good chance that there's some way of factoring out the pattern, and reducing the overall length of the code, improving conciseness. This is also called the DRY principle, or 'Don't Repeat Yourself'.
    The pattern in GenBoard in this case, is the creation of each individual square (or in your case, cube) on the board. You've got three separate kinds of cubes that you create for the board, and in each case, you're going through the exact same process of creating the primitive, setting its material, setting its parent etc. You can easily factor out this pattern into a separate private method (or in javascript, a function). Actually, functions were originally created for this exact use-case.
    Just create a method that does that general process, but takes arguments to specify the parts that do vary:

    Code (csharp):
    1. private void GenCube(int x, int y, int boardValue, Vector3 position, Color materialColor, bool isTrigger) {
    2.     board[x,y] = boardValue;
    3.     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    4.     cube.transform.position = position;
    5.     Material material = new Material(Shader.Find("Diffuse"));
    6.     material.color = materialColor;
    7.     cube.renderer.material = material;
    8.     cube.transform.parent = transform;
    9.     cube.collider.isTrigger = isTrigger;
    10. }
    Also, if you want more, look up what magic constants are.
     
  22. Pakarat

    Pakarat

    Joined:
    Jan 13, 2014
    Posts:
    19
    I never improved this code(I am lazy), I just tried to create tetris game and that's what I shared with everyone. Also I don't have any algorithm so I was just writing with many 'newbie mistakes'...
    Code (CSharp):
    1. Instantiate(object, position, rotation);
     
    Last edited: Aug 30, 2014
  23. artpologabriel

    artpologabriel

    Joined:
    Aug 7, 2013
    Posts:
    6

    wow can u share the unity project file? or can i buy it? thanks
     
  24. sventar

    sventar

    Joined:
    Feb 13, 2015
    Posts:
    3
    Could you please share your code? You have added some stuff and i like what i see :) i would like to inspect your code and work to improve my own
     
  25. julien-b

    julien-b

    Joined:
    May 8, 2015
    Posts:
    31
    Too bad we can't access demo or tutorials
     
  26. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
  27. SatDeViLxDS

    SatDeViLxDS

    Joined:
    Apr 24, 2016
    Posts:
    1
     
  28. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Thank you so much for sharing. Sharing is caring. :)
     
  29. LAFI

    LAFI

    Joined:
    Sep 5, 2014
    Posts:
    47
    Hello,
    i cant' access to the blog
    Seuls les lecteurs invités ont accès à ce blog
    This my email addressee Lafiraed@gmail.com
     
  30. akshay1993

    akshay1993

    Joined:
    Apr 24, 2018
    Posts:
    1
    -------------------------------------------------------------------------------

    Unable to access the tutorials.. Is it still available?
     
  31. naj223

    naj223

    Joined:
    Sep 25, 2018
    Posts:
    7
    I do not have access to read this tutorial
     
    Wrimor likes this.