Raptor – Java fighter pilot game!

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;
import java.util.Random;
import static java.lang.Thread.sleep;
public class SetApplet extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
private boolean shoot;
int startX, startY, endX, endY;
Color color;
Image img;
Thread newplane = null;
Integer counter = 50;
Integer[] plainy = new Integer[5];
Boolean[] hit = new Boolean[5];
String[] picname = new String[4];
Integer[] picval = new Integer[5];
Integer score = 0;
Integer level = 0;
// [Todo: Set Applet width and height and Applet Area]
public void init() {
img = null;
endX = 51;
endY = 600;
picname[0] = “plane1.png”;
picname[1] = “plane1.png”;
picname[2] = “plane2.png”;
picname[3] = “plane3.png”;
}
public void loadImage(String filename) {
try {
img = getImage(getDocumentBase(), filename);
} catch (Exception e) {
System.out.println(e);
}
}
public SetApplet() {
MouseMotionListener planemove = new MouseMotionListener() {
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent arg0) {
if (endX >= 50 && endX <= 630) {
endX = arg0.getX();
endY = arg0.getY();
repaint();
}
if (endX <= 50) {
endX = arg0.getX();
endY = arg0.getY();
repaint();
}
if (endX >= 630) {
endX = arg0.getX();
endY = arg0.getY();
repaint();
}
}
};
addMouseMotionListener((MouseMotionListener) planemove);
MouseListener planeshoot = new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) {
repaint();
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
color = Color.RED;
shoot = true;
endX = arg0.getX();
endY = arg0.getY();
repaint();
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
color = Color.white;
shoot = false;
repaint();
}
};
addMouseListener((MouseListener) planeshoot);
}
Integer k = 50;
Integer counteriter = 0;
public void paint(Graphics g) {
g.setFont(new Font(“Arial”, Font.BOLD, 20));
g.setColor(Color.BLUE);
g.drawString(“Welcome to Raptor”, 20, 20);
g.drawString(“Score ” + score.toString() , 800, 680);
g.drawString(“Level ” + level.toString() , 800, 700);
Boolean flagallshotdown = true;
counteriter = counteriter + 1;
if (counter == 50) {
for (int m = 0; m < 5; m++) {
// newplane= new Thread(this);
Random random = new Random();
hit[m] = false;
plainy[m] = showRandomInteger((120 * m) + 50, 120 * (m + 1),
random);
picval[m] = showRandomInteger(0, 3, random);
}
}
g.drawRect(20, 20, 700, 700);
if (endX >= 50 && endX <= 630) {
loadImage(“plane.png”);
g.drawImage(img, endX, 600, 59, 87, this);
if (shoot == true) {
g.setColor(color);
g.drawLine(endX + 29, 20, endX + 29, 600);
for (int l = 0; l < 5; l++) {
if (endX + 29 > plainy[l] && endX + 29 < plainy[l] + 50) {
// g.drawImage(img, 700, 700, 0, 0, null);
hit[l] = true;
score = score + 5;
if (score % 100 == 0)
{
level = level + 1;
}
}
}
}
if (shoot == false) {
g.setColor(color);
g.drawLine(0, 0, 0, 0);
}
// newplane.start();
if (counter < 550) {
for (int p = 0; p < 5; p++) {
if (hit[p] == false) {
try {
flagallshotdown = false;
loadImage(picname[picval[p]]);
g.drawImage(img, plainy[p], counter, 50, 50, this);
//sleep(5);
g.drawLine(plainy[p], counter+50+score, plainy[p], counter + 55+score);
counter = counter + 1;
sleep(20-level);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }
}
} else if (counter == 550)
counter = 50;
if (flagallshotdown == true)
counter = 50;
}
else {
if (endX > 631) {
if (counter < 550) {
for (int p = 0; p < 5; p++) {
if (hit[p] == false) {
try {
loadImage(picname[picval[p]]);
g.drawImage(img, plainy[p], counter, 50, 50,
this);
counter = counter + 1;
sleep(20-level);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }
}
} else if (counter == 550)
counter = 50;
if (shoot == true) {
g.setColor(color);
g.drawLine(630 + 29, 20, 630 + 29, 600);
for (int l = 0; l < 5; l++) {
if (endX + 29 > plainy[l] && endX + 29 < plainy[l] + 50) {
// g.drawImage(img, 700, 700, 0, 0, null);
hit[l] = true;
//score = score + 10;
if (score % 100 == 0)
{
level = level + 1;
}
}
}
}
if (shoot == false) {
g.setColor(color);
g.drawLine(0, 0, 0, 0);
}
}
if (endX < 50) {
if (counter < 550) {
for (int p = 0; p < 5; p++) {
if (hit[p] == false) {
try {
loadImage(picname[picval[p]]);
g.drawImage(img, plainy[p], counter, 50, 50,
this);
counter = counter + 1;
sleep(20-level);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }
}
} else if (counter == 550)
counter = 50;
if (shoot == true) {
g.setColor(color);
g.drawLine(50 + 29, 20, 50 + 29, 600);
for (int l = 0; l < 5; l++) {
if (endX + 29 > plainy[l] && endX + 29 < plainy[l] + 50) {
// g.drawImage(img, 700, 700, 0, 0, null);
hit[l] = true;
//score = score + 10;
if (score % 100 == 0)
{
level = level + 1;
}
}
}
}
if (shoot == false) {
g.setColor(color);
g.drawLine(0, 0, 0, 0);
}
}
}
}
private int showRandomInteger(int aStart, int aEnd, Random aRandom) {
if (aStart > aEnd) {
throw new IllegalArgumentException(“Start cannot exceed End.”);
}
// get the range, casting to long to avoid overflow problems
long range = (long) aEnd – (long) aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long) (range * aRandom.nextDouble());
int randomNumber = (int) (fraction + aStart);
return randomNumber;
}
public void start() {
// user visits the page, create a new thread
if (newplane == null) {
newplane = new Thread(this);
newplane.start();
}
}
@SuppressWarnings(“deprecation”)
public void stop() {
if (newplane != null && newplane.isAlive())
newplane.stop();
newplane = null;
}
public void run() {
while (newplane != null) {
// if (paintplane == true)
// {
repaint();
// }
// Now the reason for threads
try {
Thread.sleep(100-(level*5));
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
public void destroy() {
// destroy it.
newplane = null;
}
}

import java.applet.Applet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.awt.event.MouseListener;import java.util.Random;
import static java.lang.Thread.sleep;
public class SetApplet extends Applet implements Runnable { private static final long serialVersionUID = 1L; private boolean shoot; int startX, startY, endX, endY; Color color; Image img; Thread newplane = null; Integer counter = 50; Integer[] plainy = new Integer[5]; Boolean[] hit = new Boolean[5]; String[] picname = new String[4]; Integer[] picval = new Integer[5]; Integer score = 0; Integer level = 0;
// [Todo: Set Applet width and height and Applet Area] public void init() { img = null; endX = 51; endY = 600; picname[0] = “plane1.png”; picname[1] = “plane1.png”; picname[2] = “plane2.png”; picname[3] = “plane3.png”; }
public void loadImage(String filename) { try { img = getImage(getDocumentBase(), filename); } catch (Exception e) { System.out.println(e); } }
public SetApplet() { MouseMotionListener planemove = new MouseMotionListener() { public void mouseDragged(MouseEvent arg0) { // TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent arg0) { if (endX >= 50 && endX <= 630) { endX = arg0.getX(); endY = arg0.getY(); repaint(); } if (endX <= 50) { endX = arg0.getX(); endY = arg0.getY(); repaint(); } if (endX >= 630) { endX = arg0.getX(); endY = arg0.getY(); repaint(); } } };
addMouseMotionListener((MouseMotionListener) planemove);
MouseListener planeshoot = new MouseListener() { public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) { repaint(); }
public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) { color = Color.RED; shoot = true; endX = arg0.getX(); endY = arg0.getY(); repaint(); }
public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub color = Color.white; shoot = false; repaint(); } }; addMouseListener((MouseListener) planeshoot); }
Integer k = 50; Integer counteriter = 0;
public void paint(Graphics g) { g.setFont(new Font(“Arial”, Font.BOLD, 20)); g.setColor(Color.BLUE); g.drawString(“Welcome to Raptor”, 20, 20); g.drawString(“Score ” + score.toString() , 800, 680); g.drawString(“Level ” + level.toString() , 800, 700); Boolean flagallshotdown = true; counteriter = counteriter + 1; if (counter == 50) { for (int m = 0; m < 5; m++) { // newplane= new Thread(this); Random random = new Random(); hit[m] = false; plainy[m] = showRandomInteger((120 * m) + 50, 120 * (m + 1), random); picval[m] = showRandomInteger(0, 3, random); } } g.drawRect(20, 20, 700, 700); if (endX >= 50 && endX <= 630) { loadImage(“plane.png”); g.drawImage(img, endX, 600, 59, 87, this); if (shoot == true) { g.setColor(color); g.drawLine(endX + 29, 20, endX + 29, 600); for (int l = 0; l < 5; l++) { if (endX + 29 > plainy[l] && endX + 29 < plainy[l] + 50) { // g.drawImage(img, 700, 700, 0, 0, null); hit[l] = true; score = score + 5; if (score % 100 == 0) { level = level + 1; } } } } if (shoot == false) { g.setColor(color); g.drawLine(0, 0, 0, 0); }
// newplane.start(); if (counter < 550) { for (int p = 0; p < 5; p++) { if (hit[p] == false) { try { flagallshotdown = false;
loadImage(picname[picval[p]]); g.drawImage(img, plainy[p], counter, 50, 50, this); //sleep(5); g.drawLine(plainy[p], counter+50+score, plainy[p], counter + 55+score); counter = counter + 1; sleep(20-level); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // } } } else if (counter == 550) counter = 50; if (flagallshotdown == true) counter = 50; }
else { if (endX > 631) { if (counter < 550) { for (int p = 0; p < 5; p++) { if (hit[p] == false) { try {
loadImage(picname[picval[p]]); g.drawImage(img, plainy[p], counter, 50, 50, this);
counter = counter + 1; sleep(20-level); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // } } } else if (counter == 550) counter = 50;
if (shoot == true) { g.setColor(color); g.drawLine(630 + 29, 20, 630 + 29, 600); for (int l = 0; l < 5; l++) { if (endX + 29 > plainy[l] && endX + 29 < plainy[l] + 50) { // g.drawImage(img, 700, 700, 0, 0, null); hit[l] = true; //score = score + 10; if (score % 100 == 0) { level = level + 1; } } } } if (shoot == false) { g.setColor(color); g.drawLine(0, 0, 0, 0); } } if (endX < 50) { if (counter < 550) { for (int p = 0; p < 5; p++) { if (hit[p] == false) { try {
loadImage(picname[picval[p]]); g.drawImage(img, plainy[p], counter, 50, 50, this);
counter = counter + 1; sleep(20-level); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // } } } else if (counter == 550) counter = 50;
if (shoot == true) { g.setColor(color); g.drawLine(50 + 29, 20, 50 + 29, 600);
for (int l = 0; l < 5; l++) { if (endX + 29 > plainy[l] && endX + 29 < plainy[l] + 50) { // g.drawImage(img, 700, 700, 0, 0, null); hit[l] = true; //score = score + 10; if (score % 100 == 0) { level = level + 1; } } } } if (shoot == false) { g.setColor(color); g.drawLine(0, 0, 0, 0); } } } }
private int showRandomInteger(int aStart, int aEnd, Random aRandom) { if (aStart > aEnd) { throw new IllegalArgumentException(“Start cannot exceed End.”); } // get the range, casting to long to avoid overflow problems long range = (long) aEnd – (long) aStart + 1; // compute a fraction of the range, 0 <= frac < range long fraction = (long) (range * aRandom.nextDouble()); int randomNumber = (int) (fraction + aStart); return randomNumber; }
public void start() {
// user visits the page, create a new thread
if (newplane == null) { newplane = new Thread(this); newplane.start(); } }
@SuppressWarnings(“deprecation”) public void stop() {
if (newplane != null && newplane.isAlive()) newplane.stop(); newplane = null; }
public void run() {
while (newplane != null) { // if (paintplane == true) // { repaint(); // } // Now the reason for threads try { Thread.sleep(100-(level*5)); } catch (InterruptedException e) { System.out.println(e); } } }
public void destroy() { // destroy it. newplane = null; }}

Advertisements

One Comment on “Raptor – Java fighter pilot game!

  1. Hello There. I discovered your weblog using msn. That is
    an extremely well written article. I will make sure to bookmark it and come
    back to read more of your useful info. Thanks for the post.
    I will certainly comeback.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: