crapkiller
Forum Addict
- Joined
- Jul 19, 2006
- Sythe Gold
- 0
- Messages
- 426
Here is my game. First, lets go through running it. I will include the .class so you don't need to compile. (Bottom of post) Download the .class. In the same folder as the .class, create a new text document. In that doccument, type this:
Now save this as dotAttack.html
Now double click on the file that you just created (dotAttack.html)
What this does is it creates a very simple webpage with our applet imbedded. Now most modern day browsers support java applets. This will run our applet and make it the right size so everything works right.
It is a simple game where a little dot chases your mouse, just try to run away from it without letting it hit the walls. (if it hits the walls, you lose!) It has 3 difficulty levels, easy, medium, and hard. It also has a point system.
Now onto the source!
Keep in mind i am also learning so if i did something wrong please do tell. Other wise,
Enjoy!
.Class:
http://www.mediafire.com/?id1yi1ymwlt
Code:
<html>
<body>
<applet code = dotAttack width = 501 height = 501></applet>
</body>
</html>
Now save this as dotAttack.html
Now double click on the file that you just created (dotAttack.html)
What this does is it creates a very simple webpage with our applet imbedded. Now most modern day browsers support java applets. This will run our applet and make it the right size so everything works right.
It is a simple game where a little dot chases your mouse, just try to run away from it without letting it hit the walls. (if it hits the walls, you lose!) It has 3 difficulty levels, easy, medium, and hard. It also has a point system.
Now onto the source!
Code:
import java.awt.*; // A few imports
import java.awt.event.*;
import java.applet.*;
public class dotAttack extends Applet implements MouseMotionListener, ActionListener{ // Make our class an Applet and let it use MouseMotionListener and ActionListener
private Thread t = new Thread(); // a declaration for timing
private static Button easyBut; //easy medium and hard button declarations
private static Button medBut;
private static Button hardBut;
private int speed = 10; // the speed our dot moves
private int ourX; // our current X and Y coordinate
private int ourY;
private int enemyX = 50; // the dot's current X and Y coordinate
private int enemyY = 50;
private int points = 0; // the amount of points we have
public void actionPerformed(ActionEvent evt){ // when ever we click a button, this function is triggered
if(evt.getSource() == easyBut) // if we click the easy button make the dot move slow speed
speed = 15;
if(evt.getSource() == medBut) // if we click the medium button make the dot move medium speed
speed = 10;
if(evt.getSource() == hardBut) // if we click the hard button make the dot move fast
speed = 5;
//NOTE: putting this on hard earns points faster than on easy.
}
public void init(){
addMouseMotionListener(this); //Make our applet check for movement of the mouse
t.start(); // starting our timer
easyBut = new Button("Easy"); //telling the easy, medium, and hard buttons what the captions should be
medBut = new Button("Medium");
hardBut = new Button("Hard");
easyBut.addActionListener(this); // make the buttons wait to be clicked annd when they are cklicked, trigger the actionPerformed function
medBut.addActionListener(this);
hardBut.addActionListener(this);
add(easyBut); //add the buttons to our form
add(medBut);
add(hardBut);
}
public void mouseMoved(MouseEvent e){ // when our applet senses mouse movement it triggers this function
ourX = e.getX(); //storing the current X value of the mouse into a variable
ourY = e.getY(); //storing the current Y value of the mouse into a variable
}
public void mouseDragged(MouseEvent e){ //when our applet senses the mouse being clicked and then dragged
ourX = e.getX(); // storing the current X value of the mouse into a variable
ourY = e.getY(); // storing the current Y value of the mouse into a variable
}
public void paint (Graphics g){//this is a standard function on most applets
g.drawLine(0, 0, 500, 0); // drawing lines around the edges of the applet so we can try to avoid letting the dot hit the wall
g.drawLine(500, 0, 500, 500);
g.drawLine(500, 500, 0, 500);
g.drawLine(0, 500, 0, 0);
g.drawRect(enemyX, enemyY, 2, 2); //drawing and filling the dot that chases us
g.fillRect(enemyX, enemyY, 2, 2);
g.drawString(points + " Points", 20, 60); //draw out points
try{ //try whats in the curly braces
t.sleep(speed); //wait for either 5, 10, or 15 milliseconds (Depends on difficulty level)
} catch(InterruptedException ex){ //for some reason, we get an exception with the above line, this fixes the problem
}
touch(g); // a function i made that checks to see if our mouse is touching the dot
whichDir(); // check to see which direction to move the dot so that it follows the mouse
points++; // give us 1 point :D NOTE: this line is the same as 'points = points + 1;' Just shorter
}
public void whichDir(){ // our function that checks to see which way we should move the dot to follow the mouse
if(ourX > enemyX) // if our mouse's X coord is higher than the enemy's, than move the enemy 1 spot closer in that direction
enemyX++;
if(ourX < enemyX) // if our mouse's X coord is lower than the enemy's, than move the enemy 1 spot closer in that direction
enemyX--;
if(ourY > enemyY) //if our mouse's Y coord is higher than the enemy's, than move the enemy 1 spot closer in that direction
enemyY++;
if(ourY < enemyY)//if our mouse's Y coord is lower than the enemy's, than move the enemy 1 spot closer in that direction
enemyY--;
repaint(); // trigger the paint function again(See how it is an endless loop?)
}
public void touch(Graphics g){ //our function to check if out mouse and the dot are touching
if(ourX == enemyX && ourY == enemyY){ // if our x coord is the same as the enemy's and our Y coord is the same as the enemy's then do the stuff in the curly braces
g.drawString("You Lost!", 50, 40); // draw on our applet that we lost
this.showStatus("You Lost!"); // in firefox, in the lower left corner it says you lost
points = 0; // reset our points back to 0
try{ // same as the paint method
t.sleep(5000); // wait 5 second for the player to get ready to start again
} catch(InterruptedException ex){
}
}
}
}
Keep in mind i am also learning so if i did something wrong please do tell. Other wise,
Enjoy!
.Class:
http://www.mediafire.com/?id1yi1ymwlt

