How to Move Game Pieces Through Code

So far we have created an awesome looking user interface (UI) for our Tetris clone, so now it’s time to take the plunge and write our very first script. To do this, we are going to be creating a C# script and then writing code using Monodevelop. However, if you prefer, you can also use Microsoft Visual Basic to achieve the same purpose.

If you have never coded before, don’t worry, everything will be explained along the way.

Before we actually start coding however, let’s think about what our goals are for how to move game pieces through code. For this type of game, there are six immediate ones which come to mind.

Movement Goals:

  1. Pieces should fall at a constant speed, which increases as levels get higher.
  2. The player can move the piece left and right within the confines of the game.
  3. The piece will stop when it hits the bottom or another piece.
  4. The player can rotate the pieces clockwise and counter-clockwise in 90 degree increments.
  5. There will be a small grace period when a piece lands where the player can still rotate it.
  6. Pieces will never leave the confines of the game space.

This sounds a lot like a real Tetris game. Now let’s get into our bit of how to move game pieces through code. The very first thing you need to do is create a new C# script and attach it to your Tetris game object. Once you have done that,  take a look at the screenshot of a new script below and become familiar with what these parts are.

New Script

  • The “using” statements at the top give us access to certain bits of code. Don’t worry about these for now.
  • “Public class GamePiece” is essentially the name and description of our script. Think of it like the “My Documents” folder on your computer.
  • MonoBehaviour gives us access to a lot of code. Don’t worry about this for now, just accept that you need it there.
  • These squiggly brackets {} are like the file folders on a computer, but in this case they contain code.
  • The grey text with // behind it are comments. We can use this to write comments in plain English and help us remember why certain things are there.
  • void Start() and void Update() are methods. These are where we write our code. Whatever is written within Start() gets called when your scene loads and whatever is within Update() gets called every frame while the script is active within our game scene.
How to Move Game Pieces Through Code

Now that you know what these things are, let’s talk about how to actually write our first bit of code. We are going to write a method called MakeBlockFall() and within it change the transform.position of the game object our script is attached to. As long as you correctly attached your C# script to a Tetris piece in the game scene, this should work.

The code we want to write looks like this:

transform.position -= new Vector3 (0, 1, 0);

transform.position contains the x, y and z coordinate of our piece. We write -= to subtract the value on the right side from the value on the left. Because we are dealing with 3 coordinates (x, y, z) we need to create and subtract a new Vector3() variable which also contains 3 coordinates. As we want to make our pieces move downwards vertically, this means we leave the x and z alone, and subtract 1 from the Y.

Next, we need to call our MakeBlockFall() method from the Start() method. To do this, we simply write the name of the method with the () at the end, followed by a semicolon.

By now, your code should look like this:

TetrisPiece 2

InvokeRepeating()

Go to Unity and press the play button to test your game out. When the game starts, your piece should go down by one world unit. It will happen instantly so you won’t see it, but trust that it has. Of course, we need it to move down more than one world unit. We need it to fall at a constant pace.

To achieve this, we will use InvokeRepeating(). This will allow us to call a method over and over for as long as we want. First, we will declare a new float variable that will be our fallSpeed. This is how often InvokeRepeating will get called in seconds. To do this, write at the top of your script, just underneath “public class GamePiece”:

public float fallSpeed = 1f;

  • public determines what access level our variable has. The other option is to write private. By setting it to public we can change it from within the Unity inspector, without even entering Monodevelop.
  • float is the type of variable we are assigning. Float variables use decimal places.
  • fallSpeed is the name of variable. We can name this anything we want, but notice that the second word begins with an uppercase letter and the first word doesn’t.
  • = 1f; is the default value we are assigning it.

Now, instead of writing MakePieceFall() in the Start() method, we will write the following:

InvokeRepeating (“MakeBlockFall”, 0f, fallSpeed);

This will allow us to call MakeBlockFall() every 1 second. Of course, we want the piece to stop when it gets to the bottom so under update we will write an if statement:

if (transform.position.y == 0) {
     CancelInvoke (“MakeBlockFall”);
}

Your code should now look like this:

TetrisPiece 3

Try it out and you should have a Tetris piece that falls and stops when it gets to the bottom of the level! This is how to move game pieces through code… or at least it’s the first step.

Continue
By | 2017-07-23T18:21:01+00:00 July 21st, 2017|Tetris Clone, Tutorials|Comments Off on How to Move Game Pieces Through Code

About the Author:

Jonathan is the Executive Producer at Wild Cockatiel Games. He's an avid gamer and currently loves playing Overwatch. He's also an author seeking publication for his YA fantasy novel, Elementalists: The Fires of Canicus.