Tutorial:Wackadot:JavaWIDE
From FANG
Using the FANG Engine you can write a working game very quickly. The steps below will guide you to making a game where red and blue circles appear randomly and you have to move your mouse over them to make the circles go away. The game will keep score of how many red circles and how many blue circles you catch on in 10 seconds.
You should be able to follow these instructions without downloading or installing any software. You can use the FANG Engine Sandbox now without creating any account, or you can use the FANG Engine Playground.
As with any game you make, you want to keep it functional as much as possible. After you make additions, test them completely before moving on. Keep the additions you make short and simple. Complex games are best written by many simple steps rather than few complex steps. We're going to follow these guidelines in making our game.
Be sure read the Explanations of the Source Code in each of the tables below. Also if you are having trouble, try visiting the FAQ.
Before you get started, be sure to read some advice from students who have already made this game. They will tell you what it feels like working on the game, the types of problems they encountered, the best strategies for solving these problems, and best of of all, what it feels like when you have your own working game.
1. Making a New Page
In a new browser tab or window, open the FANG Engine Sandbox, FANG Engine Playground (coming soon), or your local JavaWIDE site (your instructor will tell you about this if you have one). At the end of the address bar, you will see index.php/Main_Page.
To make a new page, you want to replace Main_Page with the title of the new page you want to create. Replace Main_Page with YourName/wackadot/Wackadot where you replace YourName with your actual name. For example, if you name were Jane Doe, you would replace Main_Page with JaneDoe/wackadot/Wackadot. The end of the address in the address bar should look like index.php/JaneDoe/wackadot/Wackadot (with your actual name used).
Press enter to load the new page. You should see some text like:
There is currently no text in this page, you can search for this page title in other pages or edit this page.
Click on edit this page.
2. Making the Packages and Class
Click on the
button. This generates the code you need to get started writing code. Click on the Show IDE button. This will show the Java Wiki Integrated Development Environment. This is where you can edit code more easily than in the simple text box. Click on the Hide IDE button. This will make the normal text box visible again. You can toggle the visibility of the IDE in this way.
3. Running the Game
Click on Save page. You should see a game that looks like the image to the right with your code showing below it in the browser window.4. JavaWIDE is easier to use than other IDEs
Using JavaWIDE is so easy to use, we don't need a step 4. Go to step 5.
5. Adding Sprites
To make Wackadot, we definitely need dots. The visual elements on the screen in games are called sprites. Sprites have some basic properties such as shape, orientation, location, and color. We're going to start by making a red dot appear in the middle of the screen. The coordinates in the gaming engine are designed such that resizing the screen is not difficult. The location of sprites is given in fractions of the screen. Internally, this fraction is then multiplied by the actual width and actual height of the screen to get the actual location. In this way, the sprites in the game scale smoothly with the size of the screen.
Click on edit at the top of the page. Add the code below which is not already there. Be sure to leave in the <java> tag at the beginning of the page and the </java> tag at the end of the page. These java tags indicate that what is between them is a Java program and not wiki text. You already have some of the code below from the previous instructions: new code is shown in bold blue. This code should make a sprite appear in the middle of the screen.
Test the changes to make sure you see the dot appear before going to the next step. Perform the test by running the program as an application, as in the previous section. Just click on Save page
Note: the line numbers in this and each of the following tables should match the line numbers in your program.
| Line | Source code | Explanation of Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package YourName.wackadot;
import wiki.Wiki;
import fang.*;
import java.awt.*;
import java.awt.geom.*;
/**
* All about my game here.
* @author YourName
*/
public class Wackadot extends GameLoop
{
private Sprite dot;
public void startGame()
{
makeSprites();
addSprites();
}
private void makeSprites()
{
dot=new OvalSprite(1, 1);
dot.setScale(0.1);
dot.setLocation(0.5, 0.5);
dot.setColor(Color.RED);
}
private void addSprites()
{
canvas.addSprite(dot);
}
}
|
Line 1: declares the package this class resides in Line 14: declares a Sprite instance variable that will be used in the game logic eventually. Lines 16-20: the startGame method is called when the Start button is pressed the first time. In this method, you need to make and add all the sprites. Note: for multiplayer games, the number of players is only available once the startGame method is called. Lines 22-28: helper method for constructing and placing sprites. The positions and sizes are relative to a screen which spans (0, 0) to (1, 1). Lines 30-33: helper method for adding sprites to the canvas. Simply making a sprite does not make it visible. Sprites are only visible once they are added to the canvas within the bounds of the canvas and sized properly. |
6. Making the Sprite Move with the Mouse
The gaming engine redraws the screen up to 25 times per second, and may calculate up to 40 intermediate frames for every single frame it displays. After each of these frames, there is an opportunity to provide the game logic in the advanceFrame method. The advanceFrame method is called for you by the gaming engine between frames. To start with, we are going to make the position of the dot follow the mouse. Add the following method to your Wackadot class.
| Line | Source code | Explanation of Source code |
34 35 36 37 38 39 40 |
public void advanceFrame(double timePassed)
{
Point2D.Double mouse=
getPlayer().getMouse().getLocation();
dot.setLocation(mouse);
}
|
Line 34: this line is intentionally left blank. In general, it is good practice to leave a blank line between methods Line 35: this method is called automatically between each frame calculated/displayed. Lines 37-38: gets the mouse of the current player. Line 39: places the sprite at the same position as the mouse. Line 40: close brace ending the advanceFrame method. Every method body begins and ends with braces. Last Line of your Code: don't forget the close brace for your entire program. The very last character in every single program is a close brace. Leaving off the close brace will cause many errors to appear. Every class body begins and ends with braces. |
7. Test Your Changes
Always test your changes before making more modifications. Run the game. Does the dot follow the mouse? (Remember to click Start to start the game running!) If so, go on to the next step. If not, reread the instructions above to see what went wrong.
8. Make Other Dots Appear Randomly
Now we're going to add one red dot and one blue dot which will appear at random positions on the screen. When writing your games, it is important to use the provided random number generator if you are going to make networked games. The provided random number generator gives the same sequence of numbers every time which is essential for making the networked games stay consistent. The gaming engine only sends mouse and keyboard input from all of the players and relies upon common game logic to keep the games consistent. If two games use different sequences of random numbers, then this logic is not identical over the networked games and they will quickly become inconsistent. Add the following instance variables (just below where you declared dot)
| Line | Source code | Explanation of Source code |
15 16 |
private Sprite redDot; private Sprite blueDot; |
Line 15: declares the red dot Line 16: declares the blue dot |
Then, alter the existing methods (new code is bold):
| Line | Source code | Explanation of Source code |
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
private void makeSprites()
{
dot=new OvalSprite(1, 1);
dot.setScale(0.1);
dot.setLocation(0.5, 0.5);
dot.setColor(Color.RED);
redDot=new OvalSprite(1, 1);
redDot.setScale(0.1);
redDot.setLocation(
random.nextDouble(),
random.nextDouble());
redDot.setColor(Color.RED);
blueDot=new OvalSprite(1, 1);
blueDot.setScale(0.1);
blueDot.setLocation(
random.nextDouble(),
random.nextDouble());
blueDot.setColor(Color.BLUE);
}
private void addSprites()
{
canvas.addSprite(dot);
canvas.addSprite(redDot);
canvas.addSprite(blueDot);
}
|
Lines 34-35, 41-42: uses the provided random number generator to get a random number between 0 and 1. Lines 49-50: adds the sprites to the canvas to make them visible |
9. Test Your Changes
Always test your changes before making more modifications. Run the game. Do the red and blue dots appear? If so, go on to the next step. If not, reread the instructions above to see what went wrong.
10. Repositioning Dots When They Collide
The advanceFrame method is where the game logic is based and is where you will need to test for collisions. We will write a helper method called handleCollisions and call this from the advanceFrame method. Add the following code to your class (new code is bold).
| Line | Source code | Explanation of Source code |
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
private void repositionRandomly(Sprite sprite)
{
sprite.setLocation(
random.nextDouble(),
random.nextDouble());
}
private void handleCollisions()
{
if(dot.intersects(blueDot))
{
repositionRandomly(blueDot);
}
if(dot.intersects(redDot))
{
repositionRandomly(redDot);
}
}
public void advanceFrame(double timePassed)
{
Point2D.Double mouse=
getPlayer().getMouse().getLocation();
dot.setLocation(mouse);
handleCollisions();
}
|
Lines 53-58: helper method for randomly positioning the dot. Lines 60-70: helper method for handling dot collisions Lines 62-65: if the dot moving with the mouse collides with the blue dot, then the blue dot is positioned in a random location. Lines 66-69: if the dot moving with the mouse collides with the red dot, then the red dot is positioned in a random location. Lines 72-78: don't duplicate the advanceFrame method, just add line 77. Line 77: calls the helper method for handling collisions |
11. Test Your Changes
Always test your changes before making more modifications. Run the game. Do the red and blue dots reposition randomly when they intersect with the moving dot? If not, reread the instructions above to see what went wrong. If so, do you notice how they always follow the same next random positions each time you run the program?
12. Change Dot Color
When the circle intersects with a dot of the same color, it should change to the other color. The goal of the game will be to move your dot over the dot of the same color. Alter the handleCollisions method to make this happen (new code is bold).
| Line | Source code | Explanation of Source code |
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
private void handleCollisions()
{
if(dot.intersects(blueDot))
{
repositionRandomly(blueDot);
if(dot.getColor().equals(Color.BLUE))
{
dot.setColor(Color.RED);
}
}
if(dot.intersects(redDot))
{
repositionRandomly(redDot);
if(dot.getColor().equals(Color.RED))
{
dot.setColor(Color.BLUE);
}
}
}
|
Lines 65-68, 73-76: checks to see if the dot intersected matches the color of the dot. If it does, the color of the dot is changed to the other color. |
13. Test Your Changes
Hopefully you get the point by now: Always test your changes before making more modifications. This Testing your changes step will now just be implied after every step.
14. Display the Score
Score can be kept using a StringSprite. First, we're going to display the score. In the next step we'll update the score when dots collide. Add the following instance variable (just under where you have declared the other Sprite instance variables like on Step 8):
| Line | Source code | Explanation of Source code |
17 |
private StringSprite scoreSprite; |
Line 17: a sprite which will consist of text |
Alter the following methods (new code is bold):
| Line | Source code | Explanation of Source code |
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
private void makeSprites()
{
dot=new OvalSprite(1, 1);
dot.setScale(0.1);
dot.setLocation(0.5, 0.5);
dot.setColor(Color.RED);
redDot=new OvalSprite(1, 1);
redDot.setScale(0.1);
redDot.setLocation(
random.nextDouble(),
random.nextDouble());
redDot.setColor(Color.RED);
blueDot=new OvalSprite(1, 1);
blueDot.setScale(0.1);
blueDot.setLocation(
random.nextDouble(),
random.nextDouble());
blueDot.setColor(Color.BLUE);
scoreSprite=new StringSprite("Score: 0");
scoreSprite.setHeight(0.1);
scoreSprite.rightJustify();
scoreSprite.topJustify();
scoreSprite.setLocation(1, 0);
}
private void addSprites()
{
canvas.addSprite(dot);
canvas.addSprite(redDot);
canvas.addSprite(blueDot);
canvas.addSprite(scoreSprite);
}
|
Lines 46-50: makes the score sprite. Right justifying means the location will represent the rightmost location. Similarly top justifying means the location represents the topmost location. (1, 0) is the top right which is where the score will be. The text will be 1/10 of the screen high. Line 58: adds the score sprite to the canvas to make it visible |
15. Update the Score
Now we're actually going to keep the score using an int instance variable. Let's keep score like this: you get 1 point for matching colors, and -1 point for intersecting different colors. Add the following instance variable just under where you have declared the other instance variables like on Step 8 and 14:
| Line | Source code | Explanation of Source code |
18 |
private int score; |
Line 18: the integer which will keep the score |
Alter the following methods (new code is bold):
| Line | Source code | Explanation of Source code |
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 ... 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
public void startGame()
{
score=0;
makeSprites();
addSprites();
}
private void makeSprites()
{
dot=new OvalSprite(1, 1);
dot.setScale(0.1);
dot.setLocation(0.5, 0.5);
dot.setColor(Color.RED);
redDot=new OvalSprite(1, 1);
redDot.setScale(0.1);
redDot.setLocation(
random.nextDouble(),
random.nextDouble());
redDot.setColor(Color.RED);
blueDot=new OvalSprite(1, 1);
blueDot.setScale(0.1);
blueDot.setLocation(
random.nextDouble(),
random.nextDouble());
blueDot.setColor(Color.BLUE);
scoreSprite=new StringSprite("Score: "+score);
scoreSprite.setHeight(0.1);
scoreSprite.rightJustify();
scoreSprite.topJustify();
scoreSprite.setLocation(1, 0);
}
... code not shown
private void updateScore()
{
scoreSprite.setText("Score: "+score);
}
private void handleCollisions()
{
if(dot.intersects(blueDot))
{
repositionRandomly(blueDot);
if(dot.getColor().equals(Color.BLUE))
{
dot.setColor(Color.RED);
score++;
}
else
{
score--;
}
updateScore();
}
if(dot.intersects(redDot))
{
repositionRandomly(redDot);
if(dot.getColor().equals(Color.RED))
{
dot.setColor(Color.BLUE);
score++;
}
else
{
score--;
}
updateScore();
}
}
|
Line 22: starts the score out at 0 Line ... : Don't type this line into your code. Lines 48: initializes the score sprite using the score instance variable. You need to change this line to what it looks like here since originally it just made the score zero. Lines 70-73: updates the score sprite to make sure it represents the current score Lines 83, 87, 97, 101: changes the score when the dot intersects one of the colored dots. If the dot is the same color as the one intersected, then 1 is added to the score. If the dot is the opposite color of the one intersected, then 1 is subtracted from the score. Lines 89, 103: when the int score changes, the StringSprite scoreSprite should change. These lines call the helper method to change the scoreSprite to represent the current score. |
16. Display the Timer
Add similar code to make a timer. The timer won't yet be functional, but it will appear on the screen. Make the following additions/modifications to the code:
| Line | Source code | Explanation of Source code |
19 20 |
private int timeLeft; private StringSprite timerSprite; |
Line 19: an integer representing the number of second left Line 20: the StringSprite which will show the time left |
Alter the following methods (new code is bold).
| Line | Source code | Explanation of Source code |
22 23 24 25 26 27 28 29 30 31 ... 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
public void startGame()
{
score=0;
timeLeft=10;
makeSprites();
addSprites();
}
private void makeSprites()
{
... code not shown for brevity
scoreSprite.setLocation(1, 0);
timerSprite=new StringSprite("Timer: "+timeLeft);
timerSprite.leftJustify();
timerSprite.topJustify();
timerSprite.setHeight(0.1);
timerSprite.setLocation(0, 0);
}
private void addSprites()
{
canvas.addSprite(dot);
canvas.addSprite(redDot);
canvas.addSprite(blueDot);
canvas.addSprite(scoreSprite);
canvas.addSprite(timerSprite);
}
private void updateTimer()
{
timerSprite.setText("Timer: "+timeLeft);
}
|
Line 25: starts the timer out at 10 Line ... : Do not copy this line, just read it. Lines 57-61: makes the timer sprite Line 70: adds the timer sprite to the canvas to make it visible Lines 73-76: updates the timer sprite to make sure it represents the current time |
17. Make the Timer Tick
You need an alarm to make the timer go down every second. To make an alarm, the class needs to have only a single method called alarm. This method will be called when it is scheduled. We're going to write a class to make the time go down by 1 second in the alarm method. Because this class is trivial, we're going to write it as an inner class. We'll call the alarm method every second while there is time remaining. Add the following inner class modification to the constructor and make the other changes indicated (new code is bold):
| Line | Source code | Explanation of Source code |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 ... 135 136 137 138 139 140 141 142 143 144 |
public void startGame()
{
score=0;
timeLeft=10;
makeSprites();
addSprites();
scheduleRelative(new TimeUpdater(), 1);
}
class TimeUpdater implements Alarm
{
public void alarm()
{
timeLeft--;
updateTimer();
if(timeLeft>0)
{
scheduleRelative(this, 1);
}
}
}
... Code not shown
public void advanceFrame(double timePassed)
{
if(timeLeft>0)
{
Point2D.Double mouse=
getPlayer().getMouse().getLocation();
dot.setLocation(mouse);
handleCollisions();
}
}
|
Line 28: schedules the alarm method of TimeUpdater to be called 1 second from the start of the game. Adding this line will cause an error until the class below in line 10-21 is copied into the class. Lines 31-42: an inner class in Wackadot used to update the timeLeft. Lines 33-41: the method called when the alarm goes off Lines 37-40: if there is still time remaining, this schedules the alarm to go off again in 1 second Line ... : Don't type this line into your code. Lines 137-143: allows game control and scoring only while there is time left |
18. Make the Help File
Every game should have a help screen. For now it does not matter what you put in the help screen, just that you have one.
Alter the following method (new code is bold).
| Line | Source code | Explanation of Source code |
22 23 24 25 26 27 28 29 30 |
public void startGame()
{
score=0;
timeLeft=10;
makeSprites();
addSprites();
scheduleRelative(new TimeUpdater(), 1);
setHelpText("Lots of help text here later");
}
| Line 29: customizes the help to display "Lots of help text here later". You should change this later to provide more information. |
19. Comment the Code
The game works. Congratulations! Comment the code you've written/copied. Documenting your code is essential if you are to share it or collaborate with others.
| Line | Source code | Explanation of Source code |
6 7 8 9 10 11 12 13 |
import java.awt.geom;
/**This is a fun, simple game I made using
* the FANG Engine.
* @author Your Name Here
*/
public class Wackadot extends GameLoop
{
|
Lines 8-11: javadoc style comments. When you learn about how to use these, you can make web page documentation using the comments in your program. |
20. Make it a Mod
Now that you have a working version, play with it. Make modifications and enhancements. Take a look at the examples page for some ideas. The game you have written is also an applet. You can also make a jar file of the project and put your code on this wiki or another web page. See instructions about posting your game on this wiki or another web page.
If you have any questions about making this sample game, be sure to take a look at the FAQ. Chances are someone else has had the same difficulty and the solution has been posted.
Good luck making your games with the FANG Engine!
- This page was last modified on 25 January 2008, at 03:45.
- This page has been accessed 4,149 times.
- Privacy policy
- About FANG
- Disclaimers
- Powered by MediaWiki!












