My Snake Game:-
// Demonstrate the key event handlers.
/*
<applet code="SmartGame" width=300 height=100>
</applet>
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class SmartGame extends Applet
implements KeyListener {
String msg = "Score: 0";
Image img1, runImg, runImg1, runImg2,
runImg3, runImg4, findImg;
int UserX; //for window width
int UserY; //for window Height
int RX, RY, i, j;// output coordinates
int count = 0;
int X = 10, Y = 30;
public void init() {
runImg1 = getImage(getDocumentBase(),
getParameter("myRunImg1"));
runImg2 = getImage(getDocumentBase(),
getParameter("myRunImg2"));
runImg3 = getImage(getDocumentBase(),
getParameter("myRunImg3"));
runImg4 = getImage(getDocumentBase(),
getParameter("myRunImg4"));
runImg = runImg1;
findImg = getImage(getDocumentBase(),
getParameter("myImgFind"));
img1 = getImage(getDocumentBase(),
getParameter("myImg1"));
setFont(new Font("serif",
Font.BOLD, 30));
setBackground(Color.gray); //lightGray
setForeground(Color.red);
addKeyListener(this);
requestFocus(); // request input focus
}
public void start() {
UserX =
(Integer.parseInt(getParameter("widthX"))) - 45; //for window width
UserY =
(Integer.parseInt(getParameter("heightY"))) - 45; //for window Height
RX = (int) (Math.random() * (UserX -
50));
RY = (int) (Math.random() * (UserY -
50));
i = (int) (Math.random() * (UserX -
50));
j = (int) (Math.random() * (UserY -
50));
}
public void keyPressed(KeyEvent ke) {
if ((i <= (RX + 50) && i
>= (RX - 30)) && (j <= (RY + 50) && j >= (RY - 30))) {
msg = "Score: " +
(++count);
repaint();
RX = (int) (Math.random() * (UserX -
50));
RY = (int) (Math.random() * (UserY
- 50)); // output coordinates
}
int key = ke.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
//msg += "<Left
Arrow>";
runImg = runImg1;
if (i > 0 && i <
UserX) {
i -= 10;
} else if (i <= 0) {
i = UserX - 10;
} else if (i >= UserX) {
i = 5;
}
break;
case KeyEvent.VK_RIGHT:
runImg = runImg2;
//msg += "<Right
Arrow>";
if (i > 0 && i <
UserX) {
i += 10;
} else if (i <= 0) {
i = UserX - 10;
} else if (i >= UserX) {
i = 5;
}
break;
case KeyEvent.VK_UP:
runImg = runImg3;
//msg += "<UP
Arrow>";
if (j > 0 && j <
UserY) {
j -= 10;
} else if (j <= 0) {
j = UserY - 10;
} else if (j >= UserY) {
j = 5;
}
break;
case KeyEvent.VK_DOWN:
runImg = runImg4;
//msg += "<Down
Arrow>";
if (j > 0 && j <
UserY) {
j += 10;
} else if (j >= UserY) {
j = 5;
} else if (j <= 0) {
j = UserY - 10;
}
break;
}
repaint();
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
}
// Display keystrokes.
public void paint(Graphics g) {
Thread t = Thread.currentThread();
int xpoints1[] = {1, (UserX + 45) - 1,
(UserX + 45) - 1, 1};
int ypoints1[] = {1, 1, (UserY + 45) -
1, (UserY + 45) - 1};
int num1 = 4;
g.drawPolygon(xpoints1, ypoints1,
num1);
int xpoints2[] = {2, (UserX + 45) - 2,
(UserX + 45) - 2, 2};
int ypoints2[] = {2, 2, (UserY + 45) -
2, (UserY + 45) - 2};
int num2 = 4;
g.drawPolygon(xpoints2, ypoints2,
num2);
if (count <=
Integer.parseInt(getParameter("wonGame"))) {
if (count >=
Integer.parseInt(getParameter("wonStar"))) {
g.setColor(Color.blue);
g.drawString("You Have
Won: ", UserX - 300, 30);
g.drawImage(img1, UserX - 100,
UserY - (UserY - 2), this);
g.setColor(Color.red);
}
} else {
g.drawString("You Have Wone
The Game !", UserX - (UserX - 350), UserY - (UserY - 150));
g.setColor(Color.green);
g.drawString("Congratulations!", UserX - (UserX - 250), UserY
- (UserY - 100));
g.setColor(Color.yellow);
g.drawString("Now Game will be
initialise!", UserX - (UserX - 450), UserY - (UserY - 200));
g.setColor(Color.red);
try {
Thread.sleep(5000);
} catch (Exception ex) {
}
count = 0;
msg = "Score: " + count;
g.drawString(msg, X, Y);
// g.drawString("
", UserX-(UserX-200), 30);
// repaint();
}
g.drawString(msg, X, Y);
g.drawImage(findImg, RX, RY, this);
g.drawImage(runImg, i, j, this);
//g.fillOval(i,j,30,30);
//g.fillOval(RX,RY,30,30);
// for Random x and Random y
}
}
No comments:
Post a Comment