Get ready to learn Java the fun way by developing games for the Android platform with this new and updated third edition

Key Features

  • Learn Java, Android, and object-oriented programming from scratch
  • Find out how to build games including Sub Hunter, Retro Pong, Bullet Hell, Classic Snake, and Scrolling Shooters
  • Create and design your own games by learning all the concepts that a game developer must know

Book Description

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.

What you will learn

  • Set up a game development environment in Android Studio
  • Respond to a player's touch and program intelligent enemies who can challenge the player in different ways
  • Explore collision detection, sprite sheets animation, simple tracking and following, AI, parallax backgrounds, and particle explosions
  • Animate objects at 60 FPS and manage multiple independent objects using OOP
  • Work with design patterns such as OOP, singleton, strategy, and entity-component
  • Work with the Android API, the SoundPool API, Paint, Canvas, Bitmap classes, and detect version numbers

Who this book is for

Learning Java by Building Android Games is for anyone who is new to Java, Android, or game programming and wants to develop Android games. The book will also serve as a refresher for those who already have experience using Java on Android or any other platform but are new to game development.

Table of Contents

  1. Learning Java by Building Android Games
  2. Third Edition
  3. Contributors
  4. About the author
  5. About the reviewer
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Reviews
  7. Chapter 1: Java, Android, and Game Development
    1. Technical requirements
    2. Windows
    3. Mac
    4. Linux
    5. What's new in the third edition?
    6. Why Java, Android, and games?
    7. The Java stumbling block
    8. The games we will build
    9. Sub' Hunter
    10. Pong
    11. Bullet Hell
    12. Snake Clone
    13. Scrolling Shooter
    14. How Java and Android work together
    15. Run that by me again – what, exactly, is Android?
    16. Setting up Android Studio
    17. Starting the first project – Sub' Hunter
    18. Android Studio and our project – a very brief guided tour
    19. The Project panel
    20. The Editor window
    21. Refactoring MainActivity to SubHunter
    22. Locking the game to full screen and landscape orientation
    23. Amending the code to use the full screen and the best Android class
    24. Deploying the game so far
    25. Running the game on an Android emulator
    26. Running the game on a real device
    27. Summary
  8. Chapter 2: Java – First Contact
    1. Planning the Sub' Hunter game
    2. The actions flowchart/diagram
    3. Mapping out our code using comments
    4. Introducing Java methods
    5. Overriding methods
    6. Structuring Sub' Hunter with methods
    7. Introducing OOP
    8. Classes and objects
    9. Classes, objects, and instances
    10. A final word on OOP, classes, and objects – for now
    11. Using Java packages
    12. Adding classes by importing packages
    13. Linking up our methods
    14. Summary
  9. Chapter 3: Variables, Operators, and Expressions
    1. Handling syntax and jargon
    2. Java variables
    3. Different types of variables
    4. How to use variables
    5. Declaring variables
    6. Initializing variables
    7. Making variables useful with operators
    8. Most used operators in this book
    9. Declaring and initializing the Sub' Hunter variables
    10. Planning the variables
    11. Declaring the variables
    12. Handling different screen sizes and resolutions
    13. Handling different screen resolutions, part 1 – initializing the variables
    14. Errors, warnings, and bugs
    15. Printing debugging information
    16. Testing the game
    17. Summary
  10. Chapter 4: Structuring Code with Java Methods
    1. Methods
    2. Methods revisited and explained further
    3. Method overloading by example
    4. Creating a new project
    5. Coding the method overloading mini-app
    6. Running the method overloading mini-app
    7. Scope – methods and variables
    8. Method recursion
    9. Revisiting the code and methods we have used already
    10. Generating random numbers to deploy a sub
    11. The Random class and the nextInt method
    12. Testing the game
    13. Summary
  11. Chapter 5: The Android Canvas Class – Drawing to the Screen
    1. Understanding the Canvas class
    2. Getting started drawing with Bitmap, Canvas, and ImageView
    3. Using the Canvas class
    4. Preparing the objects of classes
    5. Initializing the objects
    6. Setting the Activity content
    7. Canvas Demo app
    8. Creating a new project
    9. Android coordinate system
    10. Plotting and drawing
    11. Drawing the Sub' Hunter graphics and text
    12. Preparing to draw
    13. Initializing Canvas, Paint, ImageView, and Bitmap objects
    14. Drawing some gridlines
    15. Drawing the HUD
    16. Upgrading the printDebuggingText method
    17. Summary
  12. Chapter 6: Repeating Blocks of Code with Loops
    1. Making decisions with Java
    2. Keeping things tidy
    3. More operators
    4. Java loops
    5. While loops
    6. Do while loops
    7. For loops
    8. Using for loops to draw the Sub' Hunter grid
    9. Summary
  13. Chapter 7: Making Decisions with Java If, Else, and Switch
    1. If they come over the bridge, shoot them
    2. Else do this instead
    3. Switching to make decisions
    4. Switch example
    5. Combining different control flow blocks
    6. Using the continue keyword
    7. Making sense of screen touches
    8. Coding the onTouchEvent method
    9. Final tasks
    10. Coding the takeShot method
    11. Explaining the takeShot method
    12. Coding the boom method
    13. Drawing the shot on the grid
    14. Running the game
    15. Summary
  14. Chapter 8: Object-Oriented Programming
    1. Basic object-oriented programming
    2. Humans learn by doing
    3. Introducing OOP
    4. Why do we do it like this?
    5. Class recap
    6. Looking at the code for a class
    7. Class implementation
    8. Declaring, initializing, and using an object of the class
    9. Basic classes mini-app
    10. Creating your first class
    11. More things we can do with our first class
    12. Encapsulation
    13. Controlling class use with access modifiers
    14. Controlling variable use with access modifiers
    15. Methods have access modifiers too
    16. Accessing private variables with getters and setters
    17. Setting up our objects with constructors
    18. Using "this"
    19. Static methods
    20. Encapsulation and static methods mini-app
    21. OOP and inheritance
    22. Inheritance mini-app
    23. Polymorphism
    24. Abstract classes
    25. Interfaces
    26. Starting the Pong game
    27. Planning the Pong game
    28. Setting up the Pong project
    29. Refactoring MainActivity to PongActivity
    30. Locking the game to fullscreen and landscape orientation
    31. Amending the code to use the full screen and the best Android class
    32. Summary
  15. Chapter 9: The Game Engine, Threads, and the Game Loop
    1. Coding the PongActivity class
    2. Coding the PongGame class
    3. Thinking ahead about the PongGame class
    4. Adding the member variables
    5. Coding the PongGame constructor
    6. Coding the startNewGame method
    7. Coding the draw method
    8. Understanding the draw method and the SurfaceView class
    9. The game loop
    10. Getting familiar with threads
    11. Problems with threads
    12. Java try-catch exception handling
    13. Implementing the game loop with a thread
    14. Implementing Runnable and providing the run method
    15. Coding the thread
    16. Starting and stopping the thread
    17. The activity lifecycle
    18. A simplified explanation of the Android lifecycle
    19. Lifecycle phases – what we need to know
    20. Lifecycle phases – what we need to do
    21. Using the activity lifecycle to start and stop the thread
    22. Coding the run method
    23. Running the game
    24. Summary
  16. Chapter 10: Coding the Bat and Ball
    1. The Ball class
    2. Communicating with the game engine
    3. Representing rectangles and squares with RectF
    4. Coding the variables
    5. Coding the Ball constructor
    6. Coding the RectF getter method
    7. Coding the Ball update method
    8. Coding the Ball helper methods
    9. Coding a realistic-ish bounce
    10. Using the Ball class
    11. The Bat class
    12. Coding the Bat variables
    13. Coding the Bat constructor
    14. Coding the Bat helper methods
    15. Coding the Bat's update method
    16. Using the Bat class
    17. Coding the Bat input handling
    18. Running the game
    19. Summary
  17. Chapter 11: Collisions, Sound Effects, and Supporting Different Versions of Android
    1. Handling collisions
    2. Collision detection options
    3. Optimizing the detection methods
    4. Best options for Pong
    5. The RectF intersects method
    6. Handling different versions of Android
    7. Detecting the current Android version
    8. The SoundPool class
    9. Initializing SoundPool the new way
    10. Generating sound effects
    11. Adding sound to the Pong game
    12. Adding the sound variables
    13. Initializing the SoundPool
    14. Coding the collision detection and playing sounds
    15. The bat and the ball
    16. The four walls
    17. Playing the game
    18. Summary
  18. Chapter 12: Handling Lots of Data with Arrays
    1. Planning the project
    2. Starting the project
    3. Refactoring MainActivity to BulletHellActivity
    4. Locking the game to full-screen and landscape orientation
    5. Amending the code to use the full screen and the best Android class
    6. Creating the classes
    7. Reusing the Pong engine
    8. Coding the BulletHellActivity class
    9. Coding the BulletHellGame class
    10. Testing the Bullet Hell engine
    11. Coding the Bullet class
    12. Spawning a bullet
    13. Getting started with Java arrays
    14. Arrays are objects
    15. Simple array example mini-app
    16. Getting dynamic with arrays
    17. Dynamic array example
    18. Entering the nth dimension with arrays
    19. Multidimensional array mini app
    20. Array out of bounds exceptions
    21. Spawning an array of bullets
    22. Running the game
    23. Summary
  19. Chapter 13: Bitmap Graphics and Measuring Time
    1. The Bob (player's) class
    2. Add the Bob graphic to the project
    3. Coding the Bob class
    4. Using the Bob class
    5. Coding the spawnBullet method (again)
    6. Running the game
    7. The Android Studio Profiler tool
    8. Summary
  20. Chapter 14: Java Collections, the Stack, the Heap, and the Garbage Collector
    1. Managing and understanding memory
    2. Variables revisited
    3. Introduction to the Snake game
    4. Looking ahead to the Snake game
    5. Getting started with the Snake game
    6. Refactoring MainActivity to SnakeActivity
    7. Locking the game to fullscreen and landscape orientation
    8. Adding some empty classes
    9. Coding SnakeActivity
    10. Adding the sound effects
    11. Coding the game engine
    12. Coding the members
    13. Coding the constructor
    14. Coding the newGame method
    15. Coding the run method
    16. Coding the updateRequired method
    17. Coding the update method
    18. Coding the draw method
    19. Coding the OnTouchEvent method
    20. Coding pause and resume
    21. Running the game
    22. Summary
  21. Chapter 15: Android Localization – Hola!
    1. Making the Snake game Spanish, English, or German
    2. Adding Spanish support
    3. Adding German support
    4. Adding the string resources
    5. Amending the Java code
    6. Running the game in German or Spanish
    7. Summary
  22. Chapter 16: Collections and Enumerations
    1. Adding the graphics
    2. Coding the Apple class
    3. The Apple constructor
    4. Using the Apple class
    5. Running the game
    6. Using arrays in the Snake game
    7. Understanding ArrayList class
    8. The enhanced for loop
    9. Arrays and ArrayLists are polymorphic
    10. Introducing enumerations
    11. Summary
  23. Chapter 17: Manipulating Bitmaps and Coding the Snake Class
    1. Rotating Bitmaps
    2. What is a Bitmap exactly?
    3. The Matrix class
    4. Adding the sound to the project
    5. Coding the Snake class
    6. Coding the constructor
    7. Coding the reset method
    8. Coding the move method
    9. Coding the detectDeath method
    10. Coding the checkDinner method
    11. Coding the draw method
    12. Coding the switchHeading method
    13. Using the snake class and finishing the game
    14. Running the completed game
    15. Summary
  24. Chapter 18: Introduction to Design Patterns and Much More!
    1. Introducing the Scrolling Shooter project
    2. Game programming patterns and the structure of the Scrolling Shooter project
    3. Starting the project
    4. Refactoring MainActivity to GameActivity
    5. Locking the game to fullscreen and landscape orientation
    6. Coding the GameActivity class
    7. Getting started on the GameEngine class
    8. Controlling the game with a GameState class
    9. Passing GameState from GameEngine to other classes
    10. Communicating from GameState to GameEngine
    11. Giving partial access to a class using an interface
    12. Interface refresher
    13. What we will do to implement the interface solution
    14. Coding the GameState class
    15. Saving and loading the high score forever
    16. Pressing the "special button" – calling the method of the interface
    17. Finishing off the GameState class
    18. Using the GameState class
    19. Building a sound engine
    20. Adding the sound files to the project
    21. Coding the SoundEngine class
    22. Using the SoundEngine class
    23. Testing the game so far
    24. Building a HUD class to display the player's control buttons and text
    25. Coding the prepareControls method
    26. Coding the draw method of the HUD class
    27. Coding drawControls and getControls
    28. Building a Renderer class to handle the drawing
    29. Using the HUD and Renderer classes
    30. Running the game
    31. Summary
  25. Chapter 19: Listening with the Observer Pattern, Multitouch, and Building a Particle System
    1. The Observer pattern
    2. The Observer pattern in the Scrolling Shooter project
    3. Coding the Observer pattern in Scrolling Shooter
    4. Coding the Broadcaster interface
    5. Coding the InputObserver interface
    6. Making GameEngine a broadcaster
    7. Coding a multitouch UI controller and making it a listener
    8. Coding the required handleInput method
    9. Using UIController
    10. Running the game
    11. Implementing a particle system explosion
    12. Coding the Particle class
    13. Coding the ParticleSystem class
    14. Adding a particle system to the game engine and drawing it with the Renderer class
    15. Building a physics engine to get things moving
    16. Running the game
    17. Summary
  26. Chapter 20: More Patterns, a Scrolling Background, and Building the Player's Ship
    1. Meeting the game objects
    2. A reminder of how all these objects will behave
    3. The Entity-Component pattern
    4. Why lots of diverse object types are hard to manage
    5. The first coding nightmare
    6. Using a generic GameObject for better code structure
    7. Composition over inheritance
    8. The Simple Factory pattern
    9. At last, some good news
    10. Summary so far
    11. The object specifications
    12. Coding the ObjectSpec parent class
    13. Coding all the specific object specifications
    14. Coding the component interfaces
    15. GraphicsComponent
    16. InputComponent
    17. MovementComponent
    18. SpawnComponent
    19. Coding the player's and the background's empty component classes
    20. StdGraphicsComponent
    21. PlayerMovementComponent
    22. PlayerSpawnComponent
    23. PlayerInputComponent and the PlayerLaserSpawner interface
    24. LaserMovementComponent
    25. LaserSpawnComponent
    26. BackgroundGraphicsComponent
    27. BackgroundMovementComponent
    28. BackgroundSpawnComponent
    29. Every GameObject has a transform
    30. Every object is a GameObject
    31. Completing the player's and the background's components
    32. The player's components
    33. Coding a scrolling background
    34. GameObject/Component reality check
    35. Building the GameObjectFactory class
    36. Coding the Level class
    37. Putting everything together
    38. Updating GameEngine
    39. Updating PhysicsEngine
    40. Updating the renderer
    41. Running the game
    42. Summary
  27. Chapter 21: Completing the Scrolling Shooter Game
    1. Adding the alien's components
    2. AlienChaseMovementComponent
    3. AlienDiverMovementComponent
    4. AlienHorizontalSpawnComponent
    5. AlienPatrolMovementComponent
    6. AlienVerticalSpawnComponent
    7. Spawning the aliens
    8. Updating the GameEngine class
    9. Updating the Level class
    10. Updating the GameObjectFactory class
    11. Running the game
    12. Detecting collisions
    13. Running the completed game
    14. Summary
  28. Chapter 22: What Next?
    1. Publishing
    2. Using the assets from the book
    3. Future learning
    4. My other channels
    5. Thanks
    6. Why subscribe?
  29. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think