Starting the first project: Sub' Hunter
Now we can get started on the first game. I will go into a lot more detail about what exactly Sub' Hunter does and how it is played but for now, let's just build something and see our first Android game start to take shape. Then we can run it on an emulator and a real device.
Note
The complete code as it stands at the end of this chapter is in the download bundle in the Chapter 1
folder. Note however that you still need to go through the project creation phase explained in this chapter (and at the beginning of all projects) as Android Studio does lots of work that we can't see.
Follow these steps to start the project:
- Run Android Studio in the same way you run any other app. On Windows 10, for example, the launch icon appears in the start menu.
Tip
If you are prompted to Import Studio settings from:, choose Do not import settings.
- You will be greeted with the Android Studio welcome screen as shown in the next image. Locate the Start a new Android Studio project option and left-click it.
- After this, Android Studio will bring up the New Project window. This is where we will:
- Name the new project
- Choose where on our computer the project files should go
- Provide a Company domain to distinguish our project from any others in case we should ever decide to publish the game on the Play store
The name of our project is going to be
Sub Hunter
and the location for the files will be yourAndroidProjects
folder that we created in the Setting up Android Studio section.The company domain can be almost anything you like. If you have a website, you could use the format.
Yourdomain.com
. If not, feel free to usegamecodeschool.com
or something that you just make up yourself. It is only important when you come to publish. - To be clear in case you can't see the details in the next image clearly, here are the values I used. Remember that yours might vary depending upon your choices for company domain and project location.
Option
Value entered
Application name:
Sub Hunter
Company domain:
gamecodeschool.com
Include C++ support
Leave this option unchecked (see the next information box if you want to know more)
Project location:
D:\AndroidProjects\SubHunter
- The next picture shows the New Project screen once you have entered all the information.
- In the previous image, you can see that Android Studio has auto-generated a Package name: based on the information entered. Mine is com.gamecodeschool.subhunter. Yours might be the same or not, it doesn't matter.
Note
You can write Android apps and games in a few different languages including C++ and Kotlin. There are various advantages and disadvantages to each compared to using Java. Learning Java will be a great introduction to other languages and Java is also the official language of Android. Most top apps and games on the Play store are written in Java.
- Click the Next button and we will continue to configure the Sub Hunter project. The following set of options is the Target Android Devices window. We can leave the default options selected as we are only making games for Phone and Tablet. The Minimum SDK option can be left as it is because it means our game will run on most (nearly all) Android devices from Android 4 to the latest version.
Note
If you are reading this some years in the future, then the Minimum SDK option will probably default to something different but the code in this book will still work.
- This next picture shows the Target Android Devices window we have just discussed, mainly just for your reference.
- Click the Next button and we will move on.
- The window that follows has the slightly obscure-sounding title, Add an Activity to Mobile. These are some useful project templates that Android Studio can generate for you depending on the type of app you are going to develop.
We will learn about Android Activity as the book progresses, but we are making games and want to control every pixel, beep and player input. For this reason, we will use the Empty Activity option. Android Studio will auto-generate a small amount of code to get our project started. Here is a picture of the Add an Activity to Mobile screen with the Empty Activity option selected.
- Make sure Empty Activity is selected and click Next.
- On the Customize Activity screen which you should now be looking at, we have a few changes to make. We could leave the defaults but then Android would generate more files than we need. In addition, we want to change the Activity Name to something more appropriate than MainActivity. Follow this short list of changes to configure the Customize Activity screen:
- Rename the Activity Name: to
SubHunter
- Uncheck the Generate Layout File option – we do not want a layout generated as we will be laying out every pixel ourselves
- Uncheck Backwards Compatibility (AppCompat) we won't need it for our games and they will still run on almost every phone and tablet
- Rename the Activity Name: to
- This is what the Customize the Activity screen should look like when you're done:
- Finally, for this section, you can click the Finish button and we will explore a little of what we (and Android Studio) have just done.
At this stage you might be ready to proceed but depending on the install process you might need to click a couple of extra buttons. This is why I mentioned that we are "probably" finished installing and setting up. Look in the bottom window of Android Studio and see if you have the following message.
Note
Note that if you do not see a horizontal window at the bottom of Android Studio like the one shown below you can skip these two extra steps.
Extra step 1
If you do, click Install missing platform(s) and sync project, accept the license agreement and click Next followed by Finish.
Extra step 2
If you get another message like this:
Click Install Build tools…. Then click Finish.
Tip
You can tidy up a bit and close this bottom horizontal window by clicking the Messages tab on the very bottom of Android Studio, but this isn't required.