Book Image

Learning Java by Building Android Games - Second Edition

By : John Horton
Book Image

Learning Java by Building Android Games - Second Edition

By: John Horton

Overview of this book

Android is one of the most popular mobile operating systems presently. It uses the most popular programming language, Java, as the primary language for building apps of all types. However, this book is unlike other Android books in that it doesn’t assume that you already have Java proficiency. This new and expanded second edition of Learning Java by Building Android Games shows you how to start building Android games from scratch. The difficulty level will grow steadily as you explore key Java topics, such as variables, loops, methods, object oriented programming, and design patterns, including code and examples that are written for Java 9 and Android P. At each stage, you will put what you’ve learned into practice by developing a game. You will build games such as Minesweeper, Retro Pong, Bullet Hell, and Classic Snake and Scrolling Shooter games. In the later chapters, you will create a time-trial, open-world platform game. By the end of the book, you will not only have grasped Java and Android but will also have developed six cool games for the Android platform.
Table of Contents (30 chapters)
Learning Java by Building Android Games Second Edition
Contributors
Preface
Index

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:

  1. 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.

    Note

    If you are prompted to Import Studio settings from:, choose Do not import settings.

  2. 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.

  3. 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 your AndroidProjects 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 use gamecodeschool.com or something that you just make up yourself. It is only important when you come to publish.

  4. 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

  5. The next picture shows the New Project screen once you have entered all the information.

  6. 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.

  7. 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.

  8. This next picture shows the Target Android Devices window we have just discussed, mainly just for your reference.

  9. Click the Next button and we will move on.

  10. 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.

  11. Make sure Empty Activity is selected and click Next.

  12. 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

  13. This is what the Customize the Activity screen should look like when you're done:

  14. 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.

Note

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.