Book Image

Learning Java by Building Android Games - Third Edition

By : John Horton
5 (1)
Book Image

Learning Java by Building Android Games - Third Edition

5 (1)
By: John Horton

Overview of this book

Android is one of the most popular mobile operating systems today. It uses the most popular programming language, Java, as one of the primary languages for building apps of all types. Unlike most other Android books, this book doesn’t assume that you have any prior knowledge of Java programming, instead helps you get started with building Android games as a beginner. This new, improved, and updated third edition of Learning Java by Building Android Games helps you to build Android games from scratch. Once you've got to grips with the fundamentals, the difficulty level increases steadily as you explore key Java topics, such as variables, loops, methods, object-oriented programming (OOP), and design patterns while working with up-to-date code and supporting examples. At each stage, you'll be able to test your understanding by implementing the concepts that you’ve learned to develop a game. Toward the end, you’ll build games such as Sub Hunter, Retro Pong, Bullet Hell, Classic Snake, and Scrolling Shooter. By the end of this Java book, you'll not only have a solid understanding of Java and Android basics but will also have developed five cool games for the Android platform.
Table of Contents (24 chapters)

What this book covers

Chapter 1, Java, Android, and Game Development, helps you discover what is so great about Android, what exactly Android and Java are, and how they work and complement each other. Moving quickly on, we will set up the required software and then build and deploy the outline for the first game.

Chapter 2, Java – First Contact, looks in detail at exactly how Sub' Hunter will be played and the steps/flow that our completed code will need to take to implement it. We will also learn about Java code comments for documenting the code, take a brief initial glimpse at methods to structure our code, and take an even briefer first glimpse at OOP that will begin to reveal the power of Java and the Android API.

Chapter 3, Variables, Operators, and Expressions, teaches you about Java variables that allow us to give our game the data it needs. Things such as the sub's location and whether it has been hit will soon be possible to code. Furthermore, the use of operators and expressions will enable us to change and mathematically manipulate this data as the game is executing.

Chapter 4, Structuring Code with Java Methods, takes a closer look at methods because although we know that we can call them to make them execute their code, there is more to them that hasn't been discussed so far.

Chapter 5, The Android Canvas Class – Drawing to the Screen, is entirely about the Android Canvas class and some related classes, such as Paint and Color. These classes combined bring great power when it comes to drawing to the screen. Learning about Canvas will also teach us the basics of using any class.

Chapter 6, Repeating Blocks of Code with Loops, teaches you about Java loops, which enable us to repeat sections of our code in a controlled manner. Loops in Java take a few different forms and we will learn how to use them all, and then throughout the rest of the book, we will put each of them to good use.

Chapter 7, Making Decisions with Java If, Else, and Switch, helps you learn more about controlling the flow of the game's execution, and we will also put the finishing touches to the Sub' Hunter game to make it playable.

Chapter 8, Object-Oriented Programming, gets you to grips with OOP as well as how to use it. We will start to write our own reusable classes and look at some more advanced OOP, such as abstract classes and interfaces. Topics such as encapsulation, inheritance, and polymorphism will be looked at. By the end of the chapter, we will know enough about OOP to get started on the Pong game.

Chapter 9, The Game Engine, Threads, and the Game Loop, helps you understand how the game engine comes together. By the end of the chapter, we will have an exciting blank screen that draws debugging text at 60 frames per second on a real device, although probably less on an emulator. Furthermore, this game engine code will be used as an approximate template (we will improve it with each project) for future projects, making the realization of future games faster and easier.

Chapter 10, Coding the Bat and Ball, gets you to add code to the Bat and the Ball classes. By the end of the chapter, we will have a moving ball and a player-moveable bat.

Chapter 11, Collisions, Sound Effects, and Supporting Different Versions of Android, gets you to have a fully working and beeping implementation of the Pong game. We will start the chapter off by looking at some collision detection theory, which will be put into practice near the end of the chapter. At this point, we can then put everything we have learned into producing some more code to get the Pong ball bouncing and beeping, as well as putting the finishing touches on the game.

Chapter 12, Handling Lots of Data with Arrays, teaches you about Java arrays, which allow us to manipulate a potentially huge amount of data in an organized and efficient manner. Once we are comfortable with handling arrays of data, we will see how we can spawn hundreds or thousands of our new Bullet class instances, without breaking a sweat.

Chapter 13, Bitmap Graphics and Measuring Time, helps you understand how we can use the Canvas class to draw bitmap graphics – after all, our character Bob is so much more than just a block or a line. We will also code Bob and implement his teleportation feature, shield, and collision detection. To finish the game off, we will add a HUD, measure the time, and code a solution to remember the longest (best) time.

Chapter 14, Java Collections, the Stack, the Heap, and the Garbage Collector, makes a good start on creating an authentic-looking clone of the classic Snake game. It will also be about time that we understood a little better what is going on underneath the Android hood. We constantly refer to references, but what exactly is a reference and how does it affect how we build games?

Chapter 15, Android Localization – Hola!, teaches you how to make your game accessible to millions more potential gamers. We will see how to add additional languages. We will also see the correct way to add text to our games and use this feature to make the Snake game multilingual. This includes using string resources instead of hardcoding strings in our code directly as we have done so far throughout the book.

Chapter 16, Collections and Enumerations, will be part practical and part theory. First, we will code and use the Apple class and get our apple spawning ready for dinner. Afterward, we will spend a little time getting to know new Java concepts such as ArrayList and enumerations (enums for short). These two new topics will give us the extra knowledge we will need to finish the game (mainly the Snake class) in the next chapter.

Chapter 17, Manipulating Bitmaps and Coding the Snake Class, will finish the Snake game and make it fully playable. We will put what we have learned about ArrayList and enums to good use and we will properly see the benefits of encapsulating all the object-specific drawing code into the object itself. Furthermore, we will learn how to manipulate bitmaps so that we can rotate and invert them to face any way that we need them to.

Chapter 18, Introduction to Design Patterns and Much More!, starts the Scrolling Shooter project. This project and the next will explore many ways that we can structure our Java code to make our code efficient, reusable, and less buggy. When we write code to a specific, previously devised solution/structure, we are using a design pattern.

Chapter 19, Listening with the Observer Pattern, Multitouch, and Building a Particle System, gets you to code and use our first design pattern. The Observer pattern is exactly what it sounds like. We will code some classes that will indeed observe another class. We will use this pattern to allow the GameEngine class to inform other classes when they need to handle user input. This way, individual classes can handle different aspects of user input. In addition, we will code a particle system. A particle system comprises hundreds or even thousands of graphical objects that are used to create an effect. Our particle system will look like an explosion.

Chapter 20, More Patterns, a Scrolling Background, and Building the Player's Ship, is one of the longest chapters and we have quite a bit of work as well as theory to get through before we can see the results on our device/emulator. However, what you will learn about and then implement will give you the techniques to dramatically increase the complexity of the games you are able to build. Once you understand what an entity-component system is and how to construct game objects using the Factory pattern, you will be able to add almost any game object you can imagine to your games. If you bought this book not just to learn Java but because you want to design and develop your own video games, then this part of the book is for you.

Chapter 21, Completing the Scrolling Shooter Game, will complete the Scrolling Shooter game. We will achieve this by coding the remaining component classes, which represent the three different types of aliens and the lasers that they can shoot at the player. Once we have completed the component classes, we will make minor modifications to the GameEngine, Level, and GameObjectFactory classes to accommodate these newly completed entities. The final step to complete the game is the collision detection that we will add to the PhysicsEngine class.

Chapter 22, What Next?, just covers a few ideas and pointers that you might like to look at before rushing off and making your own games.