28.04.2019 Views

[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 17 ■ Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine<br />

Use a Run ➤ Project work process, and play and test the game, which is seen in Figure 17-55. The first thing that<br />

you will notice is that all of our z-index character layer ordering is correct. You will also see that your Enemy and<br />

Projectiles are not visible at game start-up, which is another step toward a professional end-user experience.<br />

Figure 17-55. Use a Run ➤ Project work process to test the game and the enemy attack, bullet types, and scoring engine<br />

You will notice that it is much more difficult to get the InvinciBagel into position, as you do not know where,<br />

when, or what direction the enemy attack is going to come from! Well, that is not entirely true, as we would need to<br />

randomize the attackFrequency variable to get the “when he appears” part to not be triggered at even time intervals.<br />

Since our objective is to make this game progressively more and more challenging and professional, let’s do this next!<br />

For the remainder of the chapter we will add some features that make the game play more challenging and<br />

realistic. We will add some of the important game design elements such as randomization, artificial intelligence, and<br />

physics simulation, all of which will make your <strong>Java</strong> 8 games more professional and popular. You need to have some<br />

exposure to these concepts before we finish this first (beginner) round of our core <strong>Java</strong> 8 games development cycle.<br />

Add the Element of Surprise: Randomizing the Attack Frequency<br />

Now that we have made the point of entry on the screen as well as the side of the screen that is used for the attack to<br />

be completely random, let’s go into the fourth dimension (time) and make when the attack will occur also random.<br />

This is done by randomizing the attackFrequency variable, which before this we had set to 4.167 seconds (250/60FPS).<br />

We will set this random value in the else structure in the .initiateAttack() method where we set your boolean flag<br />

settings and call the .load() methods that we created to make sure the auto-attack engine always has Enemy and<br />

Projectile objects to work with. We will put a <strong>Java</strong> statement that inserts a random value into the attackFrequency<br />

variable at the end of this else portion of the if(onScreen && launchIt) conditional structure, so that a new random<br />

time is in place when the .update() method starts using this variable for its attack delay counter programming logic.<br />

Since the .nextInt(int bounds) method call structure gives us a random integer between zero and the upper bounds, in<br />

order to get a range of attack delay between one second (60) and nine seconds (60+480), we will need to add 60 to the<br />

value generated by the randomNum.nextInt(480) part of the statement, and then set the attackFrequency variable<br />

equal to that value. The <strong>Java</strong> code for this attackFrequency randomization statement can be seen in Figure 17-56 and<br />

should look like the following:<br />

attackFrequency = 60 + randomNum.nextInt(480);<br />

448<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!