Learning Java by Building Android Games

Book description

Get ready for a fun-filled experience of learning Java by developing games for the Android platform

In Detail

Android is the fastest growing operating system (OS) with one of the largest installed bases of any mobile OS. Android uses one of the most popular programming languages, Java, as the primary language for building apps of all types. So, you should first obtain a solid grasp of the Java language and its foundation APIs to improve the chances of succeeding as an Android app developer.

This book will show you how to get your Android development environment set up and you will soon have your first working game. The difficulty level grows steadily with the introduction of key Java topics such as loops, methods, and OOP. You'll then use them in the development of games. You will learn how to build a math test game, a Simon-like memory game, a retro pong-style game, and for the grand finale, a Snake-style, retro arcade game with real Google Play leaderboards and achievements. The book has a hands-on approach and is packed with screenshots.

What You Will Learn

  • Set up an efficient, professional game development environment in Android Studio

  • Build your very own Android UI using easy to-use tools in Android Studio

  • Add real-time interaction with Java threads and implement locking/handling screen rotation, pixel graphics, clicks, animation, sound FX, and many other features in your games

  • Explore object-oriented programming (OOP) and design scalable, reliable, and well-written Java games or apps on almost any Android device

  • Build and deploy a graphical pong-style game using advanced OOP concepts

  • Explore APIs and implement advanced features such as online leaderboards and achievements using Google game services

  • Make your game compelling to be the next big hit on Google Play market with a content update strategy and in-game marketing

  • Table of contents

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

    Product information

    • Title: Learning Java by Building Android Games
    • Author(s): John Horton
    • Release date: January 2015
    • Publisher(s): Packt Publishing
    • ISBN: 9781784398859