Learning Java by Building Android Games
eBook - ePub

Learning Java by Building Android Games

John Horton

Share book
  1. 392 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning Java by Building Android Games

John Horton

Book details
Book preview
Table of contents
Citations

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learning Java by Building Android Games an online PDF/ePUB?
Yes, you can access Learning Java by Building Android Games by John Horton in PDF and/or ePUB format, as well as other popular books in Computer Science & Object Oriented Programming. We have over one million books available in our catalogue for you to explore.

Information

Learning Java by Building Android Games


Table of Contents

Learning Java by Building Android Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Why Java, Android, and Games?
Is this book for me?
If you just want to learn pure Java
If Android is your focus
Where this book will take you
Why build games to learn to program?
Why Android and Java?
Android is the fastest evolving and growing OS ever
Android developers have great prospects
Android is open source
Java is here to stay
Java is not just for Android
Java is fast and easy to use
A summary of Java and Android
Setting up our development environment
Installing the JDK
Android Studio
Installing Android Studio
Summary
2. Getting Started with Android
Our first game project
Preparing Android Studio
Building the project
Exploring Android Studio
Using the Android Studio visual designer
Android UI types
TextView
Layout elements
ImageView widgets
ButtonView
Using the sample code
Making our game menu
Structuring our code for Android
Life cycle phases – what we need to know
Life cycle phases – what we need to do
Dividing our game into activities
Our first look at Java
Cleaning up our code
Building and installing our game
Emulators and devices
Creating an emulator
Running the emulator
Running our game on the emulator
Building our game
Installing the setup to a device
Future projects
Self-test questions
Summary
3. Speaking Java – Your First Game
Java syntax
The compiler
Making code clear with comments
Storing data and using it with variables
Types of variables
Reference types
Declaration
Initialization
Changing variables with operators
Expressing yourself in Java
Math game – asking a question
Creating the new game activity
Laying out the game screen UI
Coding a question in Java
Linking our game from the main menu
Decisions in Java
More operators
Decision 1 – If they come over the bridge, shoot them
Decision 2 – Else, do this
Switching to make decisions
Math game – getting and checking the answer
Self-test questions
Summary
4. Discovering Loops and Methods
Looping with loops
While loops
Breaking out of a loop
The continue keyword
Do-while loops
For loops
Random numbers in Java
Methods
The structure of a method
Modifier
Return type
Name of a method
Parameters
Getting things done in the method body
Using methods
A working method
Exploring method overloading
Enhancing our math game
Enhancing the UI
The new Java code
Amending GameActivity
The methods
The setQuestion method
The updateScoreAndLevel method
The isCorrect method
Calling our new methods
Finishing touches
Going full screen and locking orientation
Adding a custom image (instead of the Android icon)
Self-test questions
Summary
5. Gaming and Java Essentials
Java arrays – an array of variables
Arrays are objects
A simple example of an array
Getting dynamic with arrays
Dynamic array example
Entering the nth dimension with arrays
An example of a multidimensional array
Array-out-of-bounds exceptions
Timing with threads
A simple thread timer example with the Handler class
Beeps n buzzes – Android sound
Creating sound FX
Playing sounds in Android
Life after destruction – persistence
An example of persistence
The memory game
Phase 1 – the UI and the basics
Phase 2 – preparing our variables and presenting the pattern
Phase 3 – the player's response
Phase 4 – preserving the high score
Animating our game
UI animation in Android
Phase 5 – animating the UI
Self-test questions
Summary
6. OOP – Using Other People's Hard Work
What is OOP?
Why do it like this?
Our first class and first object
Basic classes
More things we can do with our first class
Encapsulation
Controlling the use of classes with access modifiers
Class access in a nutshell
Controlling the use of variables with access modifiers
Variable access in a nutshell
Methods have access modifiers too
Method access in a nutshell
Accessing private variables with the getter and setter methods
Setting up our objects with constructors
Variables revisited
The stack and the heap
A quick break to throw out the trash
Access, scope, this, static, and constructors demo
A quick summary on stack and heap
Inheritance
An example of inheritance
Polymorphism
Abstract classes
Interfaces
More about OOP and classes
Inner classes
Self-test questions
Summary
7. Retro Squash Game
Drawing with Android Canvas
The Android coordinate system
Animating our pixels
Getting started with Canvas and Paint
Android Canvas demo app
Detecting touches on the screen
Preparing to make the retro squash game
The design of the game
The UI
Physics
The structure of the code
The four implementation phases in detail
Phase 1 – MainActivity and onCreate
Phase 1 code explained
Phase 2 – SquashCourtView part 1
Phase 2 code explained
Phase 3 – SquashCourtView part 2
Phase 3 code explained
Phase 4 – Remaining lifecycle methods
Good object-oriented design
Self-test questions
Summary
8. The Snake Game
Game design
The coordinate system
Keeping track of the snake segments
Detecting collisions
Drawing the snake
The code structure
MainActivity
GameActivity
Tidying up onCreate
Animation, sprite sheets, and the Snake home screen
Animating with sprite sheets
Implementing the Snake home screen
Implementing the Snake game activity
Enhancing the game
Self-test questions
Summary
9. Making Your Game the Next Big Thing
How to publish your app
Marketing your app
Adding leaderboards and achievements
Planning the Snake achievements
Step-by-step leaderboards and achievements
Installing the Google Play Services API on your PC
Configuring the Google Play developer console
Implementing the leaderboard in the Google Play developer console
Implementing the achievements in the Google Play developer console
Setting up the Snake project ready for implementation
Implementing the player's sign-in, achievements, and leaderboard buttons
Implementing the leaderboards in code
Implementing the achievements in code
Uploading the updated Snake game to Google Play
What next?
Getting a programmer's job
Building bigger and better games
Self-test questions
Summary
A. Self-test Questions and Answers
Chapter ...

Table of contents