Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Java by Building Android  Games

You're reading from   Learning Java by Building Android Games Learn Java and Android from scratch by building six exciting games

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher
ISBN-13 9781788839150
Length 774 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John Horton John Horton
Author Profile Icon John Horton
John Horton
Arrow right icon
View More author details
Toc

Table of Contents (28) Chapters Close

Preface 1. Java, Android and Game Development FREE CHAPTER 2. Java: First Contact 3. Variables, Operators and Expressions 4. Structuring Code with Java Methods 5. The Android Canvas Class – Drawing to the Screen 6. Repeating Blocks of Code with Loops 7. Making Decisions with Java If, Else and Switch 8. Object-Oriented Programming 9. The Game Engine, Threads, and The Game Loop 10. Coding the Bat and Ball 11. Collisions, Sound Effects and Supporting Different Versions of Android 12. Handling Lots of Data with Arrays 13. Bitmap Graphics and Measuring Time 14. The Stack, the Heap, and the Garbage Collector 15. Android Localization -Hola! 16. Collections, Generics and Enumerations 17. Manipulating Bitmaps and Coding the Snake class 18. Introduction to Design Patterns and much more! 19. Listening with the Observer Pattern, Multitouch and Building a Particle System 20. More Patterns, a Scrolling Background and Building the Player's ship 21. Completing the Scrolling Shooter Game 22. Exploring More Patterns and Planning the Platformer Project 23. The Singleton Pattern, Java HashMap, Storing Bitmaps Efficiently and Designing Levels 24. Sprite-sheet animations, Controllable Player and Parallax Scrolling Backgrounds 25. Intelligent Platforms and Advanced Collision Detection 26. What next? 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.

    Tip

    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.
    Starting the first project: Sub' Hunter
  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.
    Starting the first project: Sub' Hunter
  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.
    Starting the first project: Sub' Hunter
  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.

    Starting the first project: Sub' Hunter
  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:
    Starting the first project: Sub' Hunter
  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

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:

Extra step 2

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.

You have been reading a chapter from
Learning Java by Building Android Games - Second Edition
Published in: Aug 2018
Publisher:
ISBN-13: 9781788839150
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image