The Beginner’s Guide to Android Game Development James S. Cho Glasnevin Publishing, 2nd Floor, 13 Upper Baggot Street, Dublin 4, Ireland www.glasnevinpublishing.com This edition published in 2014 by Glasnevin Publishing © James S. Cho 2014 All rights reserved. No part of this book may be reproduced or utilised in any form or by any means electronic or mechanical including photography, filming, recording, video recording, photocopying, or by information storage and retrieval system and shall not, by any way of trade or otherwise, be lent, resold or otherwise circulated in any form of binding or cover other than that in which it is published without prior permission in writing from the publisher. The moral rights of the author have been asserted. All trademarks that appear in the book are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. The author and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research and testing of the theories and computational models given in the book. The author and publisher make no warranty of any kind, expressed or implied, with regard to any text, models, program code or algorithms contained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of this text, models, program code or algorithms. ISBN: 978-1-908689-26-9 A CIP catalogue record for this book is available from the British Library Cover Design by Racheal Reeves: http://rachealreeves.com/ AndroidTM and Google PlayTM are trademarks of Google Inc. The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. JavaTM is a registered trademark of Oracle and/or its affiliates Chapter 1: The Fundamentals of Programming Maybe you’ve picked up this book because you have a passion for building things, and you want to try your hand at developing your own game. Or perhaps locked inside your brain are the ideas for a brilliant game that will change the world as we know it. In any case, you will not make it very far without becoming a programmer. This chapter will be dedicated to building much of the basic foundation you will need in order to grow into a thoughtful, successful Java programmer who can build great games while writing efficient code. As we will not be executing real programs until Chapter 2, you do not need your computer yet! What is Programming? At the basic level, to program is to instruct the computer to perform a series of tasks provided in the form of code. Let’s have a look at some example code and see what kind of instructions programmers can provide. Don’t worry about the meaning behind each symbol and line of code just yet. We will be revisiting these in detail throughout this book. For now, aim to understand the logic. Read the descriptive green text that precedes each line of code and try to follow the following code from top to bottom. ***** Listing 1.01 ***** // Instruct the computer to create two integer variables called a and // b, and assign values 5 and 6, respectively. int a = 5; int b = 6; // Create another integer variable called result using a + b. int result = a + b; // Print the result (Outputs the value of result to the Console). print("The value of a + b is " + result); Listing 1.01 shows what a programmer types into a text editor such as Notepad (Windows) or TextEdit (Mac). The output produced by the computer on the console display is shown below: The value of a + b is 11 Okay, we looked at a small example of some Java code. Here are some key points that you should remember before we proceed further. KEY POINTS Basic rule of code execution: Code is executed line by line from top to bottom. This is a simplification, but it will suit our purposes for now. We will be adding to this rule later. Comments ( // ) In Java, any line of code that follows two forward slashes is a comment. Comments are written specifically for humans (and in this case, as my way of describing code to you), and so comments will not be executed by the Java Virtual Machine (more on the JVM later). Line numbers We can refer to each line of code by its line number. You must count both comments and empty lines when determining line numbers! As an example, in listing 1.01, the following code appears on line 3: int a = 5; As demonstrated in listing 1.01, we can instruct the computer to store values (as variables), and we can perform arithmetic and concatenation (combining text with integers – see listing 1.01 line 08) operations with them. We can even display results of these operations to the console. This is just the tip of the iceberg. Before long, we will be drawing a video game character and animating its walk cycle as it moves across the screen, playing a footstep sound at each step. This may look something like this (remember that the following is ONLY an example. Several chapters into this book, you will be writing your own code like this): ***** Listing 1.02 ***** while (mainCharacter.isAlive()) { mainCharacter.updatePosition(); mainCharacter.animate(time); if (mainCharacter.getFoot().collidesWith(ground)) { footstepSound.play(volume); } screen.render(mainCharacter); } Types of Data Primitives In the previous examples, we saw examples of data types. For instance, in listing 1.01, we worked with integer values 5 and 6, both examples of numerical data. Let’s discuss some other types of data, starting with more numerical types. Integers can be represented using four types, each of which has a different size. In Java, we have the 8-bit byte, the 16-bit short, the 32- bit int, and the 64-bit long. Each of these four types can hold positive and negative integer values. Decimal values (such as 3.14159) can be represented in one of two types: the 32-bit float and the 64-bit double. We can represent a single character or symbol using char. To represent something being true or false, we use the boolean type. These are the most basic data types in Java, and we refer to them as primitive types, or primitives for short. We will be seeing many of these primitives in action in the upcoming chapters. Strings The word String refers to a string of characters. As the name suggests, we can use a String to hold multiple characters together (the primitive type char can only hold one): char firstInitial= 'J'; char lastInitial = 'C'; String name = "James"; Notice here that the word String is capitalized, while the primitive type char is not capitalized. This is because Strings belong to a category called objects, not primitives. We will spend a lot of time later discussing these objects, which play a huge role in Java programming. For the time being, however, we will treat Strings as if they were primitives. Declaring and Initializing Variables All primitives (and Strings) can be represented as variables. They are each declared (created) using the same basic syntax. When creating a new variable, we must always declare two things: the variable's data type and the variable name. In most cases, we will also assign a starting value for the variable using the assignment operator, the equal sign (=). There are two ways to do this. The first way is to assign a literal value, such as ‘J’ as shown in Figure 1-1. The second way is to assign an expression that evaluates to a value, such as 35 + 52 also shown in Figure 1-1 (the expression is evaluated before the value is assigned). Figure 1-1 Examples of Variable Declarations The assignment operator (=) does NOT declare equality. This is important. As the name suggests, we use the assignment operator to assign a value (on the right side of the equal sign) to a variable (on the left side of the equal sign). For example, consider the following two lines of code: int a = 5; a = a + 10; In this case, you are not stating a contradictory equality of a and a + 10. You are simply assigning the value of a + 10 to an existing variable a. A common practice to help make this distinction is to read the equal sign as “gets.” Figure 1- 1 would then read “int num gets the result of the expression 35 + 52.”
Description: