a simple java applet game

How Lucky You Are Today – Java Applet Game

“How Lucky You Are Today – Java Applet Game” is a game in Java Applet which shows how lucky you are today.

There is just one class “HowLuckyYouAreToday” for this game.

How Lucky You Are Today class:

<pre><code class=”java”>

/** This is an Applet game which indicates how luck you are today!
?* @author Parham Majdabadi
?* @version 28nd Feb 2012
?*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class HowLuckyYouAre extends Applet implements MouseListener, ActionListener {
int startPointX, startPointY, mouseX, mouseY, random, boxNumber, chance;
boolean [] covered;
boolean gameOn;
Button restart;
Font font;

/**
* This method called when the applet start for first time.
*/
public void init() {
setSize(610,150);
addMouseListener(this);
restart = new Button(“Restart Game”);
add(restart);
restart.addActionListener(this);
}
/**
* This method called when the applet start for first time or user click restart button.
*/
public void start() {
startPointX = 50;
startPointY = 50;
mouseX = -10;
mouseY = -10;
covered = new boolean[10];
random = (int) (Math.random() * 10);
gameOn = true;
chance = 0;
}
/**
* This method called each time user click the mouse or when the applet runs for the first time.
* @param g is a valid graphic object.
*/
public void paint(Graphics g) {
if(gameOn) {
int unit = 0;
for (int i = 0 ; i < 10; i++) {
if(!covered[i] && boxNumber != random) {
drawSquare(g,Color.green, unit);
} else if(covered[i] && boxNumber != random) {
drawSquare(g,Color.red, unit);
} else if(covered[i] && boxNumber == random) {
drawSquare(g,Color.black, unit);
showStatus(“Ooops, You Lost!”);
gameOn = false;
repaint();
} else {
drawSquare(g,Color.green, unit);
}
unit +=51;
}
chance +=10;
if(chance == 110) {
gameOn = false;
repaint();
}
} else {
if(chance != 110) {
g.drawString(“You are ” + (chance – 20) + ” % ” + “Lucky Today!”, 50, 50);
} else {
g.drawString(“Wooow, You are ” + (chance – 10) + ” % ” + “Lucky Today!. Do whatever you want!”, 50, 50);
}
}

}
/**
* This Method called once user click the mouse.
* @param e is a valid event driver
*/
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
if(mouseX >= 50 && mouseX <= 100) {
covered[0] = true;
boxNumber = 1;
} else if(mouseX >= 101 && mouseX <= 151) {
covered[1] = true;
boxNumber = 2;
} else if(mouseX >= 152 && mouseX <= 202) {
covered[2] = true;
boxNumber = 3;
} else if(mouseX >= 203 && mouseX <= 253) {
covered[3] = true;
boxNumber = 4;
} else if(mouseX >= 254 && mouseX <= 304) {
covered[4] = true;
boxNumber = 5;
} else if(mouseX >= 305 && mouseX <= 355) {
covered[5] = true;
boxNumber = 6;
} else if(mouseX >= 356 && mouseX <= 406) {
covered[6] = true;
boxNumber = 7;
} else if(mouseX >= 407 && mouseX <= 457) {
covered[7] = true;
boxNumber = 8;
} else if(mouseX >= 458 && mouseX <= 508) {
covered[8] = true;
boxNumber = 9;
} else if(mouseX >= 509 && mouseX <= 559) {
covered[9] = true;
boxNumber = 10;
}
repaint();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
/**
* This method called once user click on restart button.
*/
public void actionPerformed(ActionEvent e) {
showStatus(“”);
start();
repaint();
}
/**
* This Method draws a rectangle.
* @param gIn is a valid graphics object which is our brush.
* @param colorIn is a valid color object which indicates the colour of the brush.
* @param unitIn is an intger which indicates the new start of our x position by adding a specific amount each time to the startPointX.
*/
public void drawSquare(Graphics gIn,Color colorIn, int unitIn) {
gIn.setColor(colorIn);
gIn.fillRect(startPointX + unitIn, startPointY, 50, 50);
}
}

</code></pre>


Leave a Reply

Your email address will not be published. Required fields are marked *