In this demo project, we will put into practice the concepts we learned about it in part 1 and part 2 of the Java tutorials. We will declare and initialize a whole bunch of different types of variable and then we will see the different ways we can use operators on them and the effect it has.

patreon

 

 

About this project

Skill level 1
Time to complete 30 minutes

New concepts

  • Outputting with Log to test and debug games

As we are working within the Android environment we will be working with the code that is required by all Android apps and games. We will not explain each and every part of this environment; instead, we will slowly introduce it over the course of the projects. By the end of your first real, playable game you will be very comfortable with the Android system.

This means I will ask you to take a few concepts on faith and accept that the exact under-the-hood goings on of a few of the lines of code we will write will all become clear in later tutorials or projects. So let’s get started.

Create a new Android project and enter GameVariables in the Application Name field and enter VariablesActivity in the Activity Name field. When Android Studio has finished generating the project read on.

Your screen should look something like this.

here_is_VariablesActivity.java

We need to edit the file which will contain all our Java code. So near the top of the window as indicated in the previous diagram click the VariablesActivity.java tab. You will now be faced with what looks like a rather daunting wall of code.

The good news is we don’t need hardly any of it, so go ahead and delete it all except the line of code at the very top that begins like this.

Now what we will do is put in the code which we need to work within. If you read or implemented the previous 3 projects you might remember that all Android games and regular apps as well run in an Activity. An Activity is some code that will be executed when the player taps on the icon representing our game on their device. So we need to put this code in and then do all of our coding within it. Don’t bother typing it, just copy and paste this below the one remaining line of code in your  VariablesActivity.java file that starts package.... It is important that you don’t delete the line that starts with package….

If you lose track of where we are within the code then you can find the complete code listing for this project at the end of this page.

The full explanation for this code is unimportant right now and exactly what it does will become clearer over later projects and tutorials but a cursory explanation will be useful- so here it is.

The first two lines we copy & paste that begin with the import keyword, make available some code that was written by the Android development team. More specifically the code that will allow us to wrap up our own Java and make a game.

The next line that starts  public class VariablesActivity... is declaring the name of the class which will encompass our entire game. Classes as a topic are deep but do not have to be complicated. If you can accept that our game must be wrapped up in this way then read on. If it kind of makes your head itch then sneak ahead to tutorial number 7: Understanding OOP for coding games in Java, but be sure to come back here again afterward.

The next line of code is where the Android system does its magic and makes life easy for us.

This one line of code sets up our entire game ready for us to do what we want to do. We don’t need to know how it does it although we will learn more about it in later tutorials. Now, at last, we can play with what we learned about variables. Add the code below in between the two comments (repeated below for context).

The previous block of code simply declares a bunch of variables and initializes them in one step just as we saw in the Game variables tutorial.

Now we will print out these values. We can use some code already written for us to print out values to the Android console. Here we use the d method contained in the Log class. We then simply add a message and a variable into this method. When you read Organizing our game code with methods then methods will become clearer and when you read Understanding OOP for coding games in Java then classes will become clearer.

For now, let’s just go ahead and use this code to explore our own a little further. Enter the code after the last block of code but before the final comment.

The first thing you might notice is that the Log keyword is underlined and in red font. This indicates an error. We have this error because we need to add another line of code to import the Log class just like we imported those two other classes. No typing is required here; simply click Log to place the cursor on it and then press the Alt|Enter keyboard combination and a new line of code is added for you after the previous two import... lines of code.

This slightly strange-looking line of code simply prints a message as we will see in a minute. The first pair of quotation marks contain the message "playerScore is ". The next is where we put our variable to work. The empty quotation marks are required even though they cause no output. The + playerScore simply causes the value of playerScore to be output.

Let’s add a load more of these Log.d statements to examine our other variables. Immediately below the previous Log.d... line add these.

Now, before we run the code let’s use some operators on our variables to sample what we learned about in the tutorial Changing the value of game variables. Add the following lines of code which change our variables using various operators and then print them out so we can see if all is as expected. Add this code immediately after the previous block.

Now finally, attach an Android device and deploy/run the game. Note that although the output to the actual device is the same static display as our blank game project, it is the output in the Logcat window on the  Android tab that we are interested in. This window, pictured below should appear automatically. You will probably need to click on the image below to expand it in order to read the highlighted output.

variables_logcat_output

In the Logcat window, we can see that our variables hold the values we initialized them to and that the operators had the expected effect. We will use this technique of outputting to the Android Logcat console to help us debug our game projects.

Here is the entire code laid out with all the indentations. Note that the package... line at the top might be different from your code depending on how you built your project. It is important that this line of code remains the same way that Android Studio created it for you.

The logical next step would be to read the tutorial about checking for conditions in our game.

 

patreon