Learning Java by Building Android Games

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Get ready to learn Java the fun way by developing games for the Android platform 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 learning all the concepts 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 other Android books, this book doesn’t assume you have any prior knowledge of Java, and 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 shows you how to start building Android games from scratch. After getting 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, including up-to-date code along with helpful examples. At each stage, you will put what you’ve learned into practice by developing a game. As you advance, you’ll build games such as Minesweeper-style, Retro Pong, Bullet Hell, Classic Snake and Scrolling Shooter games. By the end of this Java book, you will not only have a solid understanding of Java and Android basics but also 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, animating sprite sheets, 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 and singleton, strategy, and entity-component patterns Use the Android API, detecting version number, SoundPool API, Paint, Canvas, and Bitmap classes 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 also acts as a refresher for those who already have experience of using Java on Android or any other platform without game development experience.

Author(s): John Horton
Edition: 3
Publisher: Packt Publishing
Year: 2021

Language: English
Pages: 634
Tags: java, android, games

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