28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17<br />

Enhancing Game Play: Creating<br />

a Scoring Engine, Adding Treasure<br />

and an Enemy Auto-Attack Engine<br />

Now that we’ve implemented collision detection for our game, as well as putting the foundation in place for a scoring<br />

engine, let’s finish coding the scoring engine that is called from the .checkCollisions() method, and then add some<br />

more game play elements to take advantage of this scoring engine, as well as to make the game play more interesting.<br />

To implement our scoring engine display, we will create a gameScore integer variable and scoreText Text object in<br />

our InvinciBagel.java class. We will also create a scoreFont Font object, and use it to style the scoreText Text object, to<br />

make it stand out better. We will also learn how to use the <strong>Java</strong> instanceof evaluator in the conditional if() statement<br />

in the .scoringEngine() method that we will be creating, and use this to determine what type of Actor object the<br />

InvinciBagel has collided with. Of course, we will then increment our gameScore variable accordingly.<br />

We’ll also create a Treasure.java class, so that we can add valuable bounty to the game for our InvinciBagel<br />

character to pick up, while he is evading constant Enemy attacks from the InvinciBeagle, or iBeagle for short, who can<br />

shoot both deadly bullets (iBullets) as well as Cream Cheese Balls (iCheese) Projectile objects. We will also be creating<br />

these Enemy.java and Projectile.java classes during this chapter, so we will be using all of the knowledge that you’ve<br />

learned during this book to create some very advanced <strong>Java</strong> 8 classes and methods during the course of this chapter.<br />

Once we have the Enemy and Projectile classes in place, the real challenge comes in creating an auto-attack<br />

engine, so the game itself plays against the player, so that we can create a single-player version of this game. We will<br />

do this by wiring the Enemy.java class into the GamePlayLoop.java class, by calling the Enemy class .update() method<br />

from the GamePlayLoop class .handle() method, which will allow us to harness the <strong>Java</strong>FX pulse timing engine for<br />

the iBeagle Enemy object. Once this is done, we can code the Enemy object, and give it a life of its own, having it<br />

appear randomly on the screen, and attack the InvinciBagel by shooting deadly bullets, or scrumptious cream<br />

cheese balls.<br />

We will build this auto-attack engine logically and gradually, first by making the iBeagle Enemy appear on either<br />

side of the screen, and flip around to face the iBagel character correctly. Then we will add programming logic that will<br />

animate the iBeagle onto the screen, and then back off of the screen. Then we will make him shoot a bullet or cream<br />

cheese ball, and then we will add some timing code in order to make his movement far more realistic.<br />

After that, we will add randomization to the appearance, locations, and movement, so that the game player<br />

cannot tell where an iBeagle attack is going to come from. After that we will add physics simulation so that the bullets<br />

and cream cheese balls are affected by drag and gravity, all of which will make the game play more and more realistic,<br />

as the chapter progresses on, through the end of the book!<br />

I hope that you have enjoyed your learning experience during the book, as much as I have enjoyed writing it for<br />

you. Now let’s get into making our game play more and more challenging and professional, as we learn even more<br />

about <strong>Java</strong> 8 and <strong>Java</strong>FX classes and programming techniques.<br />

www.it-ebooks.info<br />

393

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

Saved successfully!

Ooh no, something went wrong!