Home Page Icon
Home Page
Table of Contents for
My other channels
Close
My other channels
by John Horton
Learning Java by Building Android Games - Second Edition
Learning Java by Building Android Games - Second Edition
Table of Contents
Learning Java by Building Android Games Second Edition
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is Searching for Authors Like You
Preface
About the book
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
1. Java, Android and Game Development
What's new in the second edition?
Why Java, Android and Games?
Java: The first stumbling block
The games you will build
Sub' Hunter
Pong
Bullet Hell
Snake Clone
Scrolling Shooter
Open-World Platformer
How Java and Android work
Setting up Android Studio
Final step
Starting the first project: Sub' Hunter
Extra step 1
Extra step 2
Android Studio and our project – A very brief guided tour
The Project panel
The Editor window
Locking the game to full-screen and landscape orientation
Deploying the game so far
Running the game on an Android emulator
Running the game on a real device
Summary
2. Java: First Contact
Planning the Sub' Hunter game
Actions flowchart/diagram
Code comments
Mapping out our code using comments
Introduction to Java methods
Overriding methods
Structuring Sub' Hunter with methods
Introduction to Object Oriented Programming
Classes and Objects
The important thing about OOP, Classes, and Objects
Classes, objects, and instances
Final word on OOP, Classes, and Objects – for now
Using Java packages
Adding classes by importing packages
Linking up our methods
Summary
3. Variables, Operators and Expressions
Handling syntax and jargon
Java Variables
Different types of variables
Primitive types
Reference variables
String references
Array references
Object/class references
How to use variables
Declaring variables
Initializing variables
Making variables useful with operators
Most used operators in this book
Casting
Concatenation
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: Initialising the variables
Errors, warnings, and bugs
Printing Debugging Information
Testing the game
Summary
4. Structuring Code with Java Methods
Methods
Methods revisited and explained further
The method signature
Modifier
Return type
A closer look at method names
Parameters
Doing things in the method body
Method Overloading by Example
Creating a new project
Coding the method overloading mini-app
Running the method overloading mini-app
How it works
Scope: Methods and Variables
Revisiting the code and methods we have used already
Generating random numbers to deploy a sub
The Random class and the nextInt method
Adding Random based code to newGame
Testing the game
Summary
5. The Android Canvas Class – Drawing to the Screen
Understanding the Canvas class
Getting started drawing with Bitmap, Canvas, and ImageView
Canvas and Bitmap
Paint
ImageView and Activity
Canvas, Bitmap, Paint and ImageView quick summary
Using the Canvas class
Preparing the objects of classes
Initializing the objects
Setting the Activity content
Canvas Demo app
Creating a new project
Coding the Canvas demo app
Exploring the Bitmap initialization
Drawing on the screen
Explaining Color.argb
Android Coordinate system
Plotting and drawing
Drawing the Sub' Hunter graphics and text
Preparing to draw
Initializing a Canvas, Paint, ImageView, and Bitmap
Drawing some grid lines
Drawing the HUD
Upgrading the printDebuggingText method
Summary
6. Repeating Blocks of Code with Loops
Decisions, decisions
Keeping things tidy
More operators
Loops
While loops
Breaking out of a loop
Do while loops
For loops
Nested loops
Using for loops to draw the Sub' Hunter grid
Summary
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
Continue...
Making sense of the 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
8. Object-Oriented Programming
Basic Object-Oriented Programming
Humans learn by doing
Introducing OOP
Encapsulation
Inheritance
Polymorphism
Why 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
Remember that encapsulation thing?
Controlling class use with access modifiers
Class access in a nutshell
Controlling variable use with access modifiers
Variable access summary
Methods have access modifiers too
Method access summary
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
Summary
9. The Game Engine, Threads, and The Game Loop
Coding the PongActivity class
Coding the PongGame class
Coding the PongGame class
Adding the member variables
Coding the PongGame constructor
Coding the startNewGame method
Coding the draw method
Adding the printDebuggingText method
Understanding the draw method and the SurfaceView class
The game loop
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
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
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
11. Collisions, Sound Effects and Supporting Different Versions of Android
Handling collisions
Collision detection options
Rectangle intersection
Radius overlapping
Crossing number algorithm
Optimizations
Multiple hitboxes
Neighbour checking
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
Java Method chaining explained
Back to initializing SoundPool (the new way)
Initializing SoundPool the old way
Loading sound files into memory
Playing a sound
Stopping a sound
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
12. Handling Lots of Data with Arrays
Planning the project
Starting the project
Change the AndroidManifest.xml to lock in landscape and full-screen
Creating the classes
Reusing the Pong engine
Coding the BulletHellActivity
Coding the BulletHellGame class
Coding the member variables
Coding the BulletHellGame constructor
Coding the BulletHellGame methods
Coding draw and onTouchEvent
Coding pause, resume, and printDebuggingText
Testing the Bullet Hell engine
Coding the Bullet class
Spawning a bullet
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
13. Bitmap Graphics and Measuring Time
The Bob (player's) class
Adding the Bob graphic to the project
Coding the Bob class
Using the Bob class
Adding Bob to the collision detection
Drawing Bob to the screen
Adding the sound effects
Activating Bob's teleport
Coding the printDebuggingText method
Coding the spawnBullet method (again)
Coding the startGame method
Running the game
Summary
14. The Stack, the Heap, and the Garbage Collector
Managing and understanding memory
Variables revisited
The stack and the heap
A quick break to throw out the trash
Stack and Heap quick summary
Introduction to the Snake game
Looking ahead to the Snake game
Getting started with the Snake game
Make full screen and landscape
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 onTouchEvent
Coding pause and resume
Running the game
Summary
15. Android Localization -Hola!
Making the snake game Spanish, English and German
Adding Spanish support
Adding German support
Add the string resources
Amending the code
Run the game in German or Spanish
Summary
16. Collections, Generics and Enumerations
Adding the graphics
Coding the apple
The Apple constructor
Using the apple
Running the game
Using Arrays in the snake game
ArrayLists
The enhanced for loop
Arrays and ArrayLists are polymorphic
Enumerations
Summary
17. Manipulating Bitmaps and Coding the Snake class
Rotating Bitmaps
What is a Bitmap exactly
The Matrix class
Inverting the head to face left
Rotating the head to face up and down
Add 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
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
Editing the manifest
Code 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
1. Coding the new interface
2. Implementing the interface
3. Passing a reference to the interface into the class that needs it
Coding the GameState class
Saving and loading the high score- forever
4. 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 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
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 the 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
Building a physics engine to get things moving
Running the game
Summary
20. More Patterns, a Scrolling Background and Building the Player's ship
Meet the game objects
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
AlienChaseSpec
AlienDiverSpec
AlienLaserSpec
AlienPatrolSpec
BackgroundSpec
PlayerLaserSpec
PlayerSpec
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
Completing the StdGraphicsComponent
Completing the PlayerMovementComponent
Completing the PlayerSpawnComponent
Completing the PlayerInputComponent
Completing the LaserMovementComponent
Completing the LaserSpawnComponent
Coding a scrolling background
Completing the BackgroundGraphicsComponent
Completing the BackgroundMovementComponent
Completing the BackgroundSpawnComponent
GameObject/Component reality check
Building the GameObjectFactory
Coding the Level class
Putting everything together
Updating GameEngine
Updating PhysicsEngine
Updating Renderer
Running the game
Summary
21. Completing the Scrolling Shooter Game
Adding the alien's components
AlienChaseMovementComponent
Code AlienLaserSpawner
Implement the Interface in GameEngine
AlienDiverMovementComponent
AlienHorizontalSpawnComponent
AlienPatrolMovementComponent
AlienVerticalSpawnComponent
Spawning the Aliens
Updating GameEngine
Updating Level
Updating GameObjectFactory
Running the game
Detecting collisions
Running the completed game
Summary
22. Exploring More Patterns and Planning the Platformer Project
Platform Game: Bob Was in A Hurry
How we will build the platformer
Level design files example
The graphics
Cameras and the real world
The slightly modified ObjectSpec
New improved transform hierarchy
Project patterns
BitmapStore
Levels and LevelManager
Getting started with Bob was in a hurry
Creating the project and adding the assets
Specifying all the game objects with GameObjectSpec classes
GameObjectSpec
BackgroundCitySpec
BackgroundMountainSpec
BackgroundUndergroundSpec
BrickTileSpec
CartTileSpec
CoalTileSpec
CollectibleObjectSpec
ConcreteTileSpec
DeadTreeTileSpec
FireTileSpec
GrassTileSpec
LamppostTileSpec
MoveablePlatformSpec
ObjectiveTileSpec
PlayerSpec
ScorchedTileSpec
SnowTileSpec
SnowyTreeTileSpec
StalactiteTileSpec
StalagmiteTileSpec
StonePileTileSpec
Summary of component classes
Coding the component interfaces
GraphicsComponent
UpdateComponent
Coding the other interfaces
EngineController
GameEngineBroadcaster
InputObserver
Summary
23. The Singleton Pattern, Java HashMap, Storing Bitmaps Efficiently and Designing Levels
The Singleton pattern
The Singleton code
More Java Collections – Meet Java Hashmap
The memory problem and the BitmapStore
Coding the BitmapStore class
Coding the basic transform
Coding the inanimate and decorative components
DecorativeBlockUpdateComponent
InanmiateBlockGraphicsComponent
InanimateBlockUpdateComponent
Create the levels
Level
LevelCity
LevelMountains
LevelUnderground
Coding a stripped down GameObjectFactory
Coding a slightly commented-out game object
Coding the GameState
Code the SoundEngine
Coding the physics engine (without collision)
Coding a Renderer
Coding the camera
Testing the formulas with hypothetical coordinates
Coding the Hud
Coding the UIController class
Code the Activity
Code the GameEngine
Coding the LevelManager class
Running the game
Summary
24. Sprite-sheet animations, Controllable Player and Parallax Scrolling Backgrounds
Animating sprite-sheets
Coding the Animator
Coding the player's components and transform
PlayerTransform
AnimatedGraphicsComponent
PlayerInputComponent
PlayerUpdateComponent
Coding a parallax background
Coding the BackgroundTransform
Coding the BackgroundGraphicsComponent
Coding the BackgroundUpdateComponent
Updating the levelManager, GameObjectFactory and GameObject
Running the game
Summary
25. Intelligent Platforms and Advanced Collision Detection
Coding the moving platform component class
MoveableBlockUpdateComponent
Update LevelManager and GameObjectFactory
Running the game
Coding the detectCollisions method: Part 1
Explaining part 1
Coding the detectCollisions method: Part 2
Explaining part 2
Running the game
Summary
26. What next?
Publishing
Using the assets from the book
Future learning
My other channels
Thanks
Index
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Future learning
Next
Next Chapter
Thanks
My other channels
Please keep in touch.
gamecodeschool.com
facebook.com/gamecodeschool
twitter.com/gamecodeschool
youtube.com/channel/UCY6pRQAXnwviO3dpmV258Ig/videos
plus.google.com/114785498572480147747/posts
linkedin.com/in/gamecodeschool
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset