[JAVA][Beginning Java 8 Games Development]
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />
Let’s start with characteristics, which are things about an object that will not change and which are thus<br />
represented using constants, variables that will not (cannot) change. An important bagel characteristic is the type<br />
(flavor). We all have our favorites; mine are plain, egg, rye, onion, and pumpernickel. Another characteristic is the size<br />
of bagel; as we all know, there are minibagels, normal-size bagels, and giant bagels.<br />
private static final String FLAVOR_OF_BAGEL = "Pumpernickel";<br />
private static final String SIZE_OF_BAGEL = "Mini Bagel";<br />
Thus, constants are used to define the characteristics, or attributes, of an object. If you are defining a car, boat,<br />
or plane, the color (paint), engine (type), and transmission (type) are attributes (constants), as they generally do not<br />
change, unless you are a mechanic or own a body shop!<br />
Things about an object that will change, such as its location, orientation, how it is traveling (flying, driving,<br />
walking, running), and so on are called states and are defined using variables, which can constantly change in real<br />
time, based on what is happening in real life. These variables will allow any <strong>Java</strong> object to mimic, or virtualize, the<br />
real-world object that they are creating in your <strong>Java</strong> universe’s virtual reality. This is, of course, especially true in<br />
games, which is why the topic of this book, <strong>Java</strong> and games, is especially relevant and applicable.<br />
There will be more states (variables) than attributes (constants) for the InvinciBagel, as it is the game piece and<br />
will be especially active trying to save its hole and score points. Some of the states that you will want to define as<br />
variables include screen (x, y) location, orientation, travel direction, travel type, hits taken, and life span used.<br />
public int invinciBagelX = 0;<br />
// X screen location of the InvinciBagel<br />
public int invinciBagelY = 0;<br />
// Y screen location of the InvinciBagel<br />
public String bagelOrientation = "side"; // Defines bagel orientation (front, side, top)<br />
public int lifeIndex = 1000; // Defines units of lifespan used<br />
public int hitsIndex = 0; // Defines units of damage (hits taken)<br />
public String directionFacing = "E"; // Direction that the object is facing<br />
public String movementType = "idle" // Type of movement (idle, fly, run, jump)<br />
public boolean currentlyMoving = false; // Flag showing if the object is in motion<br />
As you progress through this book and create the InvinciBagel game, you will be adding attributes, states, and<br />
behaviors that will make the InvinciBagel, as well as its game environment and game play, more realistic, fun, and<br />
exciting, just as you would do in real life. In fact, you are using <strong>Java</strong> objects and <strong>Java</strong> constructs to model, a realistic<br />
virtual world in which InvinciBagel players can triumph over evil and shoot cream cheese balls at delicious targets.<br />
Let’s look at a couple of the methods that you might develop to control the InvinciBagel behavior. You will be<br />
creating complex methods over the course of this book to accomplish game play objectives, so I am just going to give<br />
you an idea here of how methods provide behaviors to objects for the purpose of demonstrating how objects can be<br />
created that reflect how real-world objects function.<br />
For your game play of the InvinciBagel, the main behaviors will be 2D movement around the screen, relative to<br />
the x (width) and y (height) dimension, which will access, use, and update the integer invinciBagelX, invinciBagelY,<br />
and the boolean currentlyMoving data fields discussed previously; the InvinciBagel character’s orientation (front<br />
facing, sideways, facing down, and so on), which will access, use, and update the bagelOrientation String field;<br />
the life expectancy of the InvinciBagel, which will access, use, and update the lifeIndex variable; the health of the<br />
InvinciBagel, which will access, use, and update the hitsIndex variable; the direction (East or West) in which the<br />
InvinciBagel is traveling, which will access, use, and update the directionFacing String variable; and the type of<br />
movement (flying, jumping, running, idle) that the InvinciBagel is using , which will access, use, and update the<br />
movementType String variable.<br />
Here is how you declare these methods (behaviors) and pseudocode regarding what they are going to do:<br />
public void moveInvinciBagel(int x, int y) {<br />
currentlyMoving = true;<br />
invinciBagelX = x;<br />
invinciBagelY = y;<br />
70<br />
www.it-ebooks.info