Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using FangEngine The FangEngine is created by Brian C. Ladd & Jam Jenkins Presentation by Pepper With much credit to: Jenkins, Jam & Brian C. Ladd. Introductory.

Similar presentations


Presentation on theme: "Using FangEngine The FangEngine is created by Brian C. Ladd & Jam Jenkins Presentation by Pepper With much credit to: Jenkins, Jam & Brian C. Ladd. Introductory."— Presentation transcript:

1 Using FangEngine The FangEngine is created by Brian C. Ladd & Jam Jenkins Presentation by Pepper With much credit to: Jenkins, Jam & Brian C. Ladd. Introductory Programming with Simple Games. Mass: Wiley, 2011

2 Teach Your Program Teach your program to play a game without you having to learn much about how Java plays a game. Basic engine: – Networked – Knows about players – Knows about how games are played

3 Game Playing Basics Define setup – Create/gather playing components – Set initial configuration Define advance – Update game state Call setup While (not game over) – Show game state – Get input from user – Call advance Indicate winner (or tie) * From Jenkins, Jam & Brian C. Ladd. Introductory Programming with Simple Games. Mass: Wiley, 2011, 28-29

4 Resources Fang Engine main page http://www.fangengine.org/index.php/Main_Page Download jar – fang 2 http://www.fangengine.org/index.php/download Install in BlueJ http://www.fangengine.org/index.php/Tutorials/BlueJ Tutorial for creating wackadot (but older fang engine and more than we are including) http://www.fangengine.org/index.php/Tutorial:Wackadot

5 Bring in the Fang Knowledge Bring the Fang 2 engine knowledge into our program so we can call on it. import fang2.core.*; import fang2.sprites.*; import java.awt.*; import java.awt.geom.*;

6 Package Set up a package so we can export easily package wackadot; import fang2.core.*; import fang2.sprites.*; import java.awt.*; import java.awt.geom.*;

7 Comment /** * Use mouse to wack dots and move them. * Score points for wacking dots * * @author kris pepper * @version 1 */

8 Inheritance Make our class be an extension of the GameLoop class. Now our Wackadot will know everything GameLoop knows We can use the variables GameLoop has plus we can add our own We can use the methods GameLoop has – Plus add our own – Plus Override GameLoop’s methods if we want to change what they do. (That is how we will teach Wackadot what we want that is different from GameLoop.) public class Wackadot extends GameLoop { // note that GameLoop contains the variable canvas // it also has a variable called random }

9 Main Method Add a main method so we can create a wackadot instance using the blueprint we are writing. We can then run the game as an application by asking our wackadot instance to run itself. public static void main(String[] args) { Wackadot mygame = new Wackadot(); mygame.runAsApplication(); } Run to see the result

10 Add a dot to our game Make a fang type of variable that knows how to make a dot private Sprite dot; Create an instance of Sprite to put into the dot variable Ask that Sprite to set itself to the shape, color and location we want. private void makeSprites() { dot=new OvalSprite(1, 1); dot.setScale(0.1); dot.setLocation(0.5, 0.5); dot.setColor(Color.RED); }

11 Now Add the Dot to the Canvas We have a dot variable filled with a Sprite Now add the dot to the canvas Override the startGame method to tell it to add a dot @Override public void startGame() { makeSprites(); addSprites(); } private void addSprites() { canvas.addSprite(dot); } Run to see the result

12 Add a mouse event Make the Sprite move with Mouse @Override public void advanceFrame(double timePassed) { Point2D.Double mouse= getPlayer().getMouse().getLocation(); dot.setLocation(mouse); } Run to see the result

13 2 More Dots – Random Locations Make 2 more dots appear Add 2 more variables private Sprite redDot; private Sprite blueDot; Create instances of Sprites to put into those new boxes inside makeSprites() redDot=new OvalSprite(1, 1); redDot.setScale(0.1); redDot.setLocation( random.nextDouble(), random.nextDouble()); redDot.setColor(Color.RED); Repeat for blueDot

14 Add the 2 dots to the canvas Change addSprites to add 2 more dots private void addSprites() { canvas.addSprite(dot); canvas.addSprite(redDot); canvas.addSprite(blueDot); Run to see the result

15 Handle Collisions Handle collisions by detecting them and then repositioning: private void handleCollisions() { if (dot.intersects(blueDot)) { blueDot.setLocation( random.nextDouble(), random.nextDouble()); } Repeat for redDot

16 Tell the fang2 engine how to advance Fang2 engine wont use your collision handling unless you tell it Tell it to run your method after every move: @Override public void advanceFrame(double timePassed) { Point2D.Double mouse = getPlayer().getMouse().getLocation(); dot.setLocation(mouse); handleCollisions(); } Run to see the result

17 Add a Score Add a Sprite that knows about text: Private StringSprite scoreSprite; Add a variable to keep track of collisions private int redScore, blueScore; Set the scores to 0 when the game starts @Override public void startGame() { makeSprites(); addSprites(); redScore = 0; blueScore = 0; }

18 Add the Score Object to the Canvas Create the Sprite instance to put into your scoreSprite inside makeSprite: scoreSprite=new StringSprite("Red Score: " + redScore + " Blue Score: " + blueScore); scoreSprite.setHeight(0.08); scoreSprite.rightJustify(); scoreSprite.topJustify(); scoreSprite.setLocation(1, 0); Add the score to your canvas inside addSprite canvas.addSprite(scoreSprite);

19 Increase the Score upon a hit Upon a hit, increase the score Also, reset the scoreSprite to show the new score private void handleCollisions() { if (dot.intersects(blueDot)) { blueDot.setLocation( random.nextDouble(), random.nextDouble()); blueScore++; scoreSprite.setText("Red Score: " + redScore + " Blue Score: " + blueScore); } Run to see the result

20 Your Job Add one more Yellow Sprite When the Yellow Sprite is hit, reduce one point from the blue and red scores Change the Red Sprite to start in the upper left corner Make any other changes to the game logic you like. See available methods for fang2 sprites at: http://www.fangengine.org/images/docs/api/


Download ppt "Using FangEngine The FangEngine is created by Brian C. Ladd & Jam Jenkins Presentation by Pepper With much credit to: Jenkins, Jam & Brian C. Ladd. Introductory."

Similar presentations


Ads by Google