Book Image

Learning Java by Building Android Games

By : John Horton
Book Image

Learning Java by Building Android Games

By: John Horton

Overview of this book

Table of Contents (17 chapters)
Learning Java by Building Android Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

If you are completely new to any one of Java, Android, or game programming, and are aiming at publishing Android games for fun or for a business purpose, but you have no clue as to where to start, then this book is for you. This book also acts as a refresher for those who already have experience in Java on other platforms.

Android is the fastest growing operating system, and Android devices can empower, entertain, and educate the planet. Android uses one of the most popular programming languages—Java, which is a high-performance, secure, and object-oriented language.

In each chapter, we will build on what you learned from the previous chapter, steadily gaining an understanding of more advanced Java concepts and putting them to work by building Android games.

What this book covers

Chapter 1, Why Java, Android, and Games?, tells us why we might choose Java, Android, and games over other languages, platforms, and types of apps. Then we quickly prepare our Android and Java development environment by installing the Java Development Kit and Android Studio.

Chapter 2, Getting Started with Android, shows us how to design, build, and run a game menu UI on a real Android device. This is the first part of our math game project.

Chapter 3, Speaking Java – Your First Game, covers the Java fundamentals and their use in an Android environment to make a simple math quiz game.

Chapter 4, Discovering Loops and Methods, contains more Java fundamentals such as decision-making and looping. Then we use them to add features to our math game. This chapter also covers some basic Android game essentials such as locking and handling screen rotation, and introduces the device sensors.

Chapter 5, Gaming and Java Essentials, takes us on a quick ride through some vital essentials for any game. We also make the UI more exciting with animation and by storing player scores. Demonstrated with a Simon-style memory game, storing the sequence with Java arrays and a for loop.

Chapter 6, OOP – Using Other People's Hard Work, is the chapter where everything you have learned so far is tied up with a pretty ribbon. You should find yourself muttering phrases like, "Oh, I see" and "So that's how it works." We explore object-oriented programming (OOP) in this chapter.

Chapter 7, Retro Squash Game, teaches us how to draw graphics pixel by pixel with the Android Canvas class. We can then combine these new skills with what we already know and make a retro, pong-style squash game.

Chapter 8, The Snake Game, covers the beginning of our most advanced game project—an animated Snake-style arcade game. We will also be able to practice more of the important Java fundamentals that you learned in the previous chapters.

Chapter 9, Making Your Game the Next Big Thing, puts the icing on the cake by teaching you how to publish your game and add online leaderboards and achievements. This will also let you examine the concept of Java libraries.

Appendix, Self-test Questions and Answers, contains all the answers to the questions to test your understanding of the topic.

What you need for this book

You will need a modest version of Windows (XP, Vista, 7, or 8), and a Mac, Linux PC, or laptop. Both 32-bit and 64-bit systems are compatible. All of the software needed in excess of this is free and acquiring it is fully explained in the book.

Who this book is for

This book is for you if you are completely new to any one of Java, Android, or game programming. As the book assumes no previous knowledge, it is also for you if you are new to all three (Java, Android, and game programming).

If you want to publish Android games for fun or for business and are not sure where to start, then this book will show you what to do, step by step from the start.

If you have experience in Java on another platform (perhaps PC or Mac), then this book will be a good Java refresher, and will also show you how to apply your existing skills in an Android context.

If you have previously programmed in another language but want to learn Java for any platform from the start, then you will be able to learn from this book and move through the practical examples more quickly.

The book does assume that you are fairly confident in using your operating system of choice. The step-by-step tutorials are in Windows, but if you have a decent understanding of Mac or Linux, you can probably follow along quite easily with them too.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and explanations of their meanings.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "In the Android Studio Project Explorer, double-click on the layout folder to reveal the activity_main.xml file within it."

A block of code is set as follows:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

int partA = 9;
int partB = 9;
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer - 1;
int wrongAnswer2 = correctAnswer + 1;

Any command-line input or output is written as follows:

info﹕ a = 10
info﹕ b = Alan Turing
info﹕ c = true

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "When prompted to import another class, click on OK."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: http://www.packtpub.com/sites/default/files/downloads/8859OS_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.