import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;
public class game extends JFrame implements KeyListener {
// alrighty , lets start taking this thing apart shall we?
// so 1st off here , we have the intergets that will manage the planes
// postion
// and regulate how it travels
int planeY = 200, planeX = 200, Xspeed = 0, Yspeed = 0;
// up next we have some game play varibles which will handle things like
// power ups , points , leveling up and other such things
double points = 0;
int level = 1, clocktime = 0;;
boolean manmode = false;
int manmodetimer = 0;
int firerate = 120, firedamage = 5;
boolean started = false;
int songtimer = 0;
// the compents below will be used to handle the clouds and to handle a
// delay on
// the menu pickup time
int x1 = 0, y1 = 0, x2 = 1000, check = 0;
int I = 0, T = 0, L = 0;
// below we will declare graphics handelers and other such components that
// will
// ease the use of double buffering
Graphics buffergraphics;
Image offscreen;
Dimension dim;
// below we have some booleans which will handel the pauseing, starting and
// other such things , throughout the game
boolean Run = false;
boolean Run2 = true;
boolean menu = true;
boolean dead = false;
boolean gameover = false;
// the blow booleans will handle other ingame taskes
// such as enableing the hit boxes to be seen
boolean showboxes = false;
boolean firingbullets = false;
boolean trifire = false;
boolean forcespawn = false;
// the below arraylists will take care of manageing al the bad guys ,
// bullets and powerups
ArrayList<Rectangle> bullets = new ArrayList<Rectangle>();
ArrayList<Rectangle> badguys = new ArrayList<Rectangle>();
ArrayList<Rectangle> badguyhealth = new ArrayList<Rectangle>();
ArrayList<Integer> healthvaule = new ArrayList<Integer>();
ArrayList<Boolean> deadornot = new ArrayList<Boolean>();
ArrayList<Rectangle> powerups = new ArrayList<Rectangle>();
// these are the collision boxes for the plane
Rectangle CPB = new Rectangle(planeX, planeY + 70, 140, 10);
Rectangle CPW = new Rectangle(planeX + 100, planeY + 5, 10, 140);
Rectangle CPW2 = new Rectangle(planeX + 80, planeY + 5, 10, 140);
Rectangle CPT = new Rectangle(planeX + 10, planeY + 45, 10, 60);
// the methods below will handle key events
public void keyPressed(KeyEvent e) {
// this section here handles entering and exiting the main menu
if (menu == true && T == 0) {
if (e.getKeyChar() == 'm') {
System.out.println("menu exited");
menu = false;
Run = true;
T++;
}
}
if (menu == false && T == 0) {
if (e.getKeyChar() == 'm') {
System.out.println("menu entered");
menu = true;
Run = false;
T++;
}
}
// the below key event will handel reseting the game after death
if (e.getKeyChar() == 'r') {
Run = true;
dead = false;
gameover = false;
bullets.clear();
badguys.clear();
badguyhealth.clear();
healthvaule.clear();
deadornot.clear();
powerups.clear();
planeY = 200;
planeX = 200;
CPB.y = planeY + 70;
CPW.y = planeY + 5;
CPW2.y = planeY + 5;
CPT.y = planeY + 45;
CPB.x = planeX;
CPW.x = planeX + 100;
CPW2.x = planeX + 80;
CPT.x = planeX + 10;
points = 0.0;
level = 1;
}
// the next coupld of lines should be easy to figure out
// they set the speed vaules of the plane based off
// which key is hit
if (e.getKeyChar() == 's') {
Yspeed = (0 - 1);
}
if (e.getKeyChar() == 'w') {
Yspeed = 1;
}
if (e.getKeyChar() == 'd') {
Xspeed = (0 - 1);
}
if (e.getKeyChar() == 'a') {
Xspeed = 1;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
firingbullets = true;
}
// avoit your eyes! these are alpha tester controls!
if (e.getKeyChar() == '0') {
dead = true;
}
if (e.getKeyChar() == '9') {
showboxes = true;
}
if (e.getKeyChar() == '8') {
trifire = true;
}
if (e.getKeyChar() == '7') {
forcespawn = true;
}
// this littl line here makes sure that the menu doesn't
// glitch to crap whenever you press it though it doesn't always work
// i gotta think of some other way to handle this but i don't have time
L++;
if (L >= 500) {
T = 0;
L = 0;
}
}
@Override
// the below method will handle when
// a key event ends (you take your finger off the key)
public void keyReleased(KeyEvent e) {
// mostly what is happening is the reseting of vaules
if (e.getKeyChar() == 's') {
Yspeed = 0;
}
if (e.getKeyChar() == 'w') {
Yspeed = 0;
}
if (e.getKeyChar() == 'd') {
Xspeed = 0;
}
if (e.getKeyChar() == 'a') {
Xspeed = 0;
// again avoid your eyes! these are alpha controls!
}
if (e.getKeyChar() == '0') {
dead = false;
}
if (e.getKeyChar() == '9') {
showboxes = false;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
firingbullets = false;
}
if (e.getKeyChar() == '8') {
trifire = false;
}
if (e.getKeyChar() == '7') {
forcespawn = false;
}
}
public void keyTyped(KeyEvent e) {
// these keys handle pause and starting, i should have done
// the menu this way, but i wanted to bind it to one key instead of 2
if (e.getKeyChar() == 'p') {
Run = false;
}
if (e.getKeyChar() == 'o') {
Run = true;
}
}
// here is where the game starts, this is the contructor!
public game() {
super("game");
addKeyListener(this);
setSize(1000, 600);
// not currently implemented , but soon i will not be useing the setSize
// command, instead i will let the user set there own size
// by dragging the window, but before i do that i need to work out
// a good ratio system so everything scales correctly
// i think that a system of double Xratio = currentsize.width / 1000
// (orginal working size)
// and double Yratio = currentsize.height / 600
// i think that creating those, then adding them onto every single X & Y
// relation
// would make my scaleing work, but im not too sure, and Im outta time
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// below i handle starting all the threads/methods i created
planemovement plane = new planemovement();
// the above thread handles the plane
refreshrate refresh = new refreshrate();
// the above thread handles the repaint and the cloud speed
hitdetection hit = new hitdetection();
// the above thread handles hit detection for the plane
bulletcreator bulletcreator = new bulletcreator();
// the above thread handles bullet creation
bulletmanager bulletmanager = new bulletmanager();
// the above thread handles bullet movement
badguyspawning spawning = new badguyspawning();
// the above thread handles random spawning
badguymovement badguymove = new badguymovement();
// the above thread handles badguy movement
bullethitdection bullethit = new bullethitdection();
// the above thread handels bullet collison
deathcontrol death = new deathcontrol();
// the above thread handles bad guy death and power up creation
powerupmovement powerup = new powerupmovement();
// the above thread handles power up movement
powerupclock powerclock = new powerupclock();
// the above thread handles the amount of time the power up lasts
levelclock clock = new levelclock();
// the above thread handles time passage
planedeath gamedeath = new planedeath();
// the above thread handles player dieing
audiomanager audio = new audiomanager();
// I don't think you know how hard it was for me to not make
// a method called "madness" cause ive been dying to make the
// "theres method to my madness " pun all friggin year
// any way , so now we start all the threads we declared above
refresh.start();
plane.start();
hit.start();
bulletcreator.start();
bulletmanager.start();
spawning.start();
badguymove.start();
bullethit.start();
death.start();
powerup.start();
powerclock.start();
clock.start();
gamedeath.start();
audio.start();
}
// here is where the graphics starts!
public void paint(Graphics g) {
// the below image array handles all my images
Image[] images = new Image[10];
// this handles fecthing the size of the window
// so i can double buffer correctly
dim = getSize();
// so we create a blank Image, and we make it the size of
// the current window
offscreen = createImage(dim.width, dim.height);
// then we tell our graphics engine to get whatever is currently on
// screen
buffergraphics = offscreen.getGraphics();
// then , we clear the screen
buffergraphics.clearRect(0, 0, dim.width, dim.width);
// now lets start drawing stuff
buffergraphics.setColor(Color.red);
// this handles directing the graphics to the main menu
if (menu == false) {
// this handles directing the graphics to game over screen
if (gameover == false) {
// and this handles directing the graphics to all the normal
// stuff
if (Run == true) {
int loop = 0;
// lets start loading up those images
try {
images[1] = ImageIO.read(new File("sky.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
images[2] = ImageIO.read(new File("sky.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
images[4] = ImageIO.read(new File("ufo.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
images[5] = ImageIO.read(new File("boom.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
images[6] = ImageIO.read(new File("powerup.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
images[7] = ImageIO.read(new File("manup.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// below will handle the planing blowing up before the game
// ends
if (dead == false) {
try {
images[3] = ImageIO
.read(new File("planefinal.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dead == true) {
try {
images[3] = ImageIO.read(new File("boom.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// now we go to where we actually draw stuff on the screen
// course , i used the auto format when i was done, so this
// layout looks like sh*t, but all for marks
// okay , lets draw the clouds 1st, because the order in
// which we
// draw things, is the order in which they get coverd up
// the last thing to be drawn, will always overlay ontop
// of everything else
buffergraphics
.drawImage(images[1], x1, y1, 1000, 600, this);
buffergraphics.drawImage(images[2], x2, y1, 1000 + 2, 600,
this);
// now lets draw the plane, so it overlays ontop the clouds
buffergraphics.drawImage(images[3], planeX, planeY, 150,
150, null);
// / now lets handle drawing those hitboxes , which are an
// ALPHA
// control! so don't you rember this!
buffergraphics.setColor(Color.CYAN);
if (showboxes == true) {
buffergraphics.fillRect(CPB.x, CPB.y, 140, 10);
buffergraphics.fillRect(CPW.x, CPW.y, 10, 140);
buffergraphics.fillRect(CPW2.x, CPW2.y, 10, 140);
buffergraphics.fillRect(CPT.x, CPT.y, 10, 60);
}
// now we handle drawing the bullets, this loop will go
// around
// and draw all the bullets
try {
if (bullets.size() > 0) {
while (loop < bullets.size()) {
buffergraphics.setColor(Color.YELLOW);
buffergraphics.fillRect(bullets.get(loop).x,
bullets.get(loop).y, 10, 10);
loop++;
}
}
}
// now, this try and catch are on that, beacuse it was
// giving off an
// error or null pointer (the bullet count was reduced while
// it was
// trying to draw the bullet)
// my program is filled with errors like this, where stuff
// is killed / delelted during its handeling process,
// rather then going and making sure all this stuff runs in
// some sort
// of order so this can't happen, i would rather just catch
// the errors
catch (NullPointerException e) {
loop = 0;
}
// now here we go and draws the power-ups, because
// we want the bad guys to show up overtop of the power ups
int loop3 = 0;
if (powerups.size() > 0) {
while (loop3 < powerups.size()) {
buffergraphics.drawImage(images[6],
powerups.get(loop3).x,
powerups.get(loop3).y, 50, 50, this);
buffergraphics.drawImage(images[7],
powerups.get(loop3).x,
powerups.get(loop3).y - 20, 50, 20, this);
loop3++;
}
}
// now we draw those badguys
// if the bad guys are dead, we draw a "boom"
int loop2 = 0;
if (badguys.size() > 0) {
while (loop2 < badguys.size()) {
try {
if (deadornot.get(loop2) == true) {
buffergraphics.drawImage(images[5],
badguys.get(loop2).x,
badguys.get(loop2).y, 50, 50, null);
} else {
buffergraphics.drawImage(images[4],
badguys.get(loop2).x,
badguys.get(loop2).y, 50, 50, null);
}
// course, while we draw the bad guys we also
// gotta draw their health vaules
buffergraphics.setColor(Color.RED);
buffergraphics.fillRect(
badguyhealth.get(loop2).x,
badguyhealth.get(loop2).y,
healthvaule.get(loop2), 10);
loop2++;
}
// more errors to catch!
catch (NullPointerException e) {
loop2 = 0;
}
}
}
}
// this 1st else here, is for when the "normal" engine isn't
// running,
// this means the game must be paused.. there is not real use
// for the pause, considering that i have the main menu, but oh
// well
// it was nice pratice
else {
try {
images[1] = ImageIO.read(new File("pause.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buffergraphics.drawImage(images[1], 0, 0, dim.width,
dim.height, this);
buffergraphics.setColor(Color.BLUE);
buffergraphics.setFont(new Font("TimesRoman", Font.PLAIN,
30));
buffergraphics.drawString("PRESS 0 ", 20, dim.height / 4);
buffergraphics
.drawString("TO UNPAUSE ", 20, dim.height / 5);
}
}
// now below we are going to handle the game over screen
// for when you die(beacuse you don't know the alpha controls!
// RIGHT?)
else {
Run = false;
buffergraphics.setColor(Color.BLACK);
buffergraphics.fillRect(0, 0, dim.width, dim.height);
buffergraphics.setColor(Color.RED);
buffergraphics.setFont(new Font("TimesRoman", Font.PLAIN, 120));
buffergraphics.drawString("YOU ARE DEAD", 20, dim.height / 2);
buffergraphics.setFont(new Font("TimesRoman", Font.PLAIN, 30));
buffergraphics.drawString("GAME OVER MAN! GAME OVER!", 200,
dim.height / 2 + 120);
buffergraphics.drawString("YOU ACHIVE A SCORE OF : " + points,
200, dim.height / 2 + 150);
buffergraphics.drawString("YOU MADE IT TO LEVEL : " + level,
200, dim.height / 2 + 180);
buffergraphics.drawString("PRESS R TO RESET", 200,
dim.height / 2 + 210);
}
}
// alrighty , now we are going to Draw the main menu!
else {
Rectangle background = new Rectangle(0, 0, dim.width, dim.height);
buffergraphics.setColor(Color.RED);
buffergraphics.fillRect(background.x, background.y, dim.width,
dim.height);
buffergraphics.setColor(Color.BLUE);
buffergraphics.setFont(new Font("TimesRoman", Font.PLAIN, 80));
buffergraphics.drawString("MAIN MENU", (dim.width / 2) - 200, 100);
Rectangle controls = new Rectangle((dim.width / 4),
dim.width / 4 - 100, 200, 100);
buffergraphics.fillRect(controls.x, controls.y, 600, 230);
buffergraphics.setFont(new Font("TimesRoman", Font.PLAIN, 30));
buffergraphics.setColor(Color.GREEN);
buffergraphics.drawString("controls", controls.x + 50,
controls.y + 50);
buffergraphics.drawString("press wsad to move", controls.x + 50,
controls.y + 80);
buffergraphics.drawString("press P to pause", controls.x + 50,
controls.y + 110);
buffergraphics.drawString("press spacebar to fire",
controls.x + 50, controls.y + 140);
buffergraphics.drawString("press R to reset", controls.x + 50,
controls.y + 170);
buffergraphics.drawString("and press M to return to menu/game",
controls.x + 50, controls.y + 200);
}
// almost done, lets draw the points!
buffergraphics.setColor(Color.ORANGE);
buffergraphics.setFont(new Font("TimesRoman", Font.PLAIN, 30));
buffergraphics.drawString("points : " + points + " level : " + level,
50, 50);
// now finally , lets draw the offscreen image,
// which contains all of our graphicical data
// this is how double buffer works, draw everything at once
// beacuse when you draw componets seperately , it flickers
g.drawImage(offscreen, 0, 0, this);
}
// i have no idea what the below method does, I just put it in there
// cause some coders from StackoverFlow (great site)
// said i needed it , though nothing happens when i take it out
public void update(Graphics g) {
paint(g);
}
// this thread will handle plane movement
public class planemovement extends Thread {
public void run() {
// inital loop to ensure 2ndary loop can keep running
while (Run2 == true) {
// this if statment is to make sure that the threads arn't
// taking up
// cpu power, when the game is paused
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
// kk , lets start off by moveing the Image of the plane
// around
planeY = planeY - Yspeed;
planeX = planeX - Xspeed;
// now the hitboxes for the body
CPB.x = CPB.x - Xspeed;
CPB.y = CPB.y - Yspeed;
// wings
CPW.x = CPW.x - Xspeed;
CPW.y = CPW.y - Yspeed;
CPW2.x = CPW2.x - Xspeed;
CPW2.y = CPW2.y - Yspeed;
// tail
CPT.y = CPT.y - Yspeed;
CPT.x = CPT.x - Xspeed;
// we will allow the thread to sleep for 5 milisecounds,
// simply to
// control the speed at which the plane travels around
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// now this thread will handle the refresh rate
public class refreshrate extends Thread {
public void run() {
while (Run2 == true) {
I++;
repaint();
// okay , below is the delay behinde frames / prints
// odly enough, a higher vaule such as 8 or 10 seems to make the
// game run
// smoother... you would think that a lower vaule would do
// better
// beacuse then you would have more frames, but i guess not
try {
Thread.sleep(8);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (I >= 2) {
I = 0;
x1--;
x2--;
if (x1 == -1000) {
x1 = 1000;
}
if (x2 == -1000) {
x2 = 1000;
}
}
}
}
}
// here we are handleing if the plane is
// collideing with a power up or the borders
// or any of the bad guys
public class hitdetection extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
int loop = 0;
int loop2 = 0;
// lets see if any of the hit boxes hit any of the bad guys
while (loop < badguys.size()) {
try {
try {
if (CPB.intersects(badguys.get(loop))) {
dead = true;
}
if (CPW.intersects(badguys.get(loop))) {
dead = true;
}
if (CPW2.intersects(badguys.get(loop))) {
dead = true;
}
if (CPT.intersects(badguys.get(loop))) {
dead = true;
}
} catch (IndexOutOfBoundsException e) {
loop = 0;
}
} catch (NullPointerException e) {
loop = 0;
}
loop++;
}
// now lets do the same and see if we hit any power ups
// if we do, lets activate the power up stats
while (loop2 < powerups.size()) {
try {
if (CPB.intersects(powerups.get(loop2))) {
System.out.println("man mode activated");
manmode = true;
manmodetimer = 0;
firerate = 70;
firedamage = 200;
trifire = true;
powerups.remove(loop2);
loop2 = 0;
}
if (CPW.intersects(powerups.get(loop2))) {
System.out.println("man mode activated");
manmode = true;
manmodetimer = 0;
firerate = 70;
firedamage = 200;
trifire = true;
powerups.remove(loop2);
loop2 = 0;
}
if (CPW2.intersects(powerups.get(loop2))) {
System.out.println("man mode activated");
manmode = true;
manmodetimer = 0;
firerate = 70;
firedamage = 200;
trifire = true;
powerups.remove(loop2);
loop2 = 0;
}
if (CPT.intersects(powerups.get(loop2))) {
System.out.println("man mode activated");
manmode = true;
manmodetimer = 0;
firerate = 70;
firedamage = 200;
trifire = true;
powerups.remove(loop2);
loop2 = 0;
}
} catch (IndexOutOfBoundsException e) {
loop2 = 0;
}
loop2++;
}
// okay , lets get the size , and then make sure that the
// plane isn't
// leaving the borders of the window
// of the plane is moving outside the box, move it back!
Dimension range = getSize();
if (planeY + 140 > range.height) {
planeY--;
CPB.y = CPB.y - 1;
CPW.y = CPW.y - 1;
CPW2.y = CPW2.y - 1;
CPT.y = CPT.y - 1;
}
if (planeY < 0) {
planeY++;
CPB.y = CPB.y + 1;
CPW.y = CPW.y + 1;
CPW2.y = CPW2.y + 1;
CPT.y = CPT.y + 1;
}
if (planeX + 140 > range.width) {
planeX--;
CPB.x = CPB.x - 1;
CPW.x = CPW.x - 1;
CPW2.x = CPW2.x - 1;
CPT.x = CPT.x - 1;
}
if (planeX < 0) {
planeX++;
CPB.x = CPB.x + 1;
CPW.x = CPW.x + 1;
CPW2.x = CPW2.x + 1;
CPT.x = CPT.x + 1;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// now we are going to handle the creation of bullets
public class bulletcreator extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
// so the area the the bullets spawn , should be where the
// plane is
int bulletspawnX = CPB.x + 120, bulletspawnY = CPB.y;
if (firingbullets == true) {
Rectangle bullet = new Rectangle(bulletspawnX,
bulletspawnY, 10, 10);
bullets.add(bullet);
if (trifire == true) {
Rectangle bullet2 = new Rectangle(
bulletspawnX - 45, bulletspawnY + 70, 10,
10);
Rectangle bullet3 = new Rectangle(
bulletspawnX - 45, bulletspawnY - 70, 10,
10);
bullets.add(bullet2);
bullets.add(bullet3);
}
}
try {
Thread.sleep(firerate);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// now we are going to handle bullet movement
public class bulletmanager extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
int loop = 0;
Dimension range = getSize();
// alrighty, so check every bullet, and lets see if it goes
// out of bounds
// or if it hits something , but most importantly , lets
// make it move!
if (bullets.size() > 0) {
while (loop < bullets.size()) {
try {
try {
bullets.get(loop).x = bullets.get(loop).x + 1;
} catch (IndexOutOfBoundsException e) {
loop--;
}
try {
if (bullets.get(loop).x > range.width) {
bullets.remove(loop);
loop--;
}
} catch (IndexOutOfBoundsException e) {
loop--;
}
loop++;
} catch (NullPointerException e) {
loop = 0;
}
}
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// now lets handle the creation and location of bad guys!
public class badguyspawning extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
int spawn = 0;
spawn = (int) (Math.random() * 100);
Dimension range = getSize();
// okay , so as the level increases, we will increase the
// chance
// of bad guys getting spawned
if (spawn <= (1 * (level * 1.25))) {
// okay , so we create the random Y cord,
// and if this is too close to the boreders (so the
// alien
// would be offscreen ) lets move him up a random
// amount!
int Y = (int) (Math.random() * range.height);
if (Y >= range.height - 80) {
Y = range.height
- ((int) (Math.random() * 300) + 50);
}
if (Y <= 80) {
Y = 0 + ((int) (Math.random() * 300) + 50);
}
// kk , now we make the varibles that repersent this
// badguy
int vaule = 50;
boolean dead = false;
Rectangle badguy = new Rectangle(range.width, Y, 50, 50);
Rectangle health = new Rectangle(range.width, Y - 15,
50, 10);
// and now lets add all the info to their respective
// arrays
badguys.add(badguy);
badguyhealth.add(health);
healthvaule.add(vaule);
deadornot.add(dead);
}
// ALPHA FEATURE, PLEASE IGNORE
if (forcespawn == true) {
int Y = (int) (Math.random() * range.height);
if (Y >= range.height - 80) {
Y = range.height
- ((int) (Math.random() * 300) + 50);
}
if (Y <= 80) {
Y = 0 + ((int) (Math.random() * 300) + 50);
}
int vaule = 50;
boolean dead = false;
Rectangle badguy = new Rectangle(range.width, Y, 50, 50);
Rectangle health = new Rectangle(range.width, Y - 15,
50, 10);
badguyhealth.add(health);
badguys.add(badguy);
healthvaule.add(vaule);
deadornot.add(dead);
}
// and now lets make the thread sleep ,
// so we don't get too much bad guys right off the bat
// (level 30 is insainty though!
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// now lets handle the bad guys moving across the screen
public class badguymovement extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
int loop = 0;
// course, were only doing this if there is any bad guys
// and also, if they go offscreen, they are deleted
if (badguys.size() > 0) {
while (loop < badguys.size()) {
try {
try {
badguys.get(loop).x = badguys.get(loop).x - 1;
badguyhealth.get(loop).x = badguyhealth
.get(loop).x - 1;
} catch (IndexOutOfBoundsException e) {
loop--;
}
try {
if (badguys.get(loop).x < 0) {
badguys.remove(loop);
badguyhealth.remove(loop);
healthvaule.remove(loop);
loop--;
}
} catch (IndexOutOfBoundsException e) {
loop = 0;
}
} catch (NullPointerException e) {
loop = 0;
}
loop++;
}
}
// and let them sleep for 30 milisecounds, so they don't out
// run the
// plane!
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// now we handle bullets hitting stuff!
public class bullethitdection extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
int loop = 0;
// okay , so while there are badguys on screen
while (loop < badguys.size()) {
int loop2 = 0;
try{
try {
// and while there are bullets also on screen
while (loop2 < bullets.size()) {
if (loop > badguys.size()){
loop=0;
}
if (loop2 > bullets.size()){
loop2=0;
}
// check to see if the current bad guys
// collides with any of the current bullets
// if so , remove that bullets, and give that
// bad guy damage
// because of the setup of these dual loops, every
// bad guy
// will be checked against every bullet
// this thread consumes the most power compared to
// any other
if (badguys.get(loop).intersects(bullets.get(loop2))) {
healthvaule.set(loop, healthvaule.get(loop)- firedamage);
bullets.remove(loop2);
loop2--;
}
loop2++;
}
}
catch (IndexOutOfBoundsException e){
System.out.println("bullet error one");
loop2=0;
}
}
catch (NullPointerException e){
System.out.println ("bullet error two");
loop2=0;
loop=0;
}
loop++;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
// this controls the baddies deaths, and makes sure you get power ups!
public class deathcontrol extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
int spawn = 0;
int loop = 0;
while (loop < badguys.size()) {
try {
try {
if (healthvaule.get(loop) <= 0) {
deadornot.set(loop, true);
// okay, so when we set the death to true
// we wana wait a little so that way ,
// we acutally see the little "boom"
// before they de-spawn
try {
Thread.sleep(120);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// now we determin the chance of man-up
// being dropped
spawn = (int) (Math.random() * (20 * (1 + level / 8)));
if (spawn == 1) {
Rectangle powerup = new Rectangle(
badguys.get(loop).x,
badguys.get(loop).y, 50, 50);
powerups.add(powerup);
}
// and finally we add points, and remove all
// things related
// to that baddie
points = points + 50;
badguys.remove(loop);
deadornot.remove(loop);
badguyhealth.remove(loop);
healthvaule.remove(loop);
}
} catch (IndexOutOfBoundsException e) {
loop = 0;
}
} catch (NullPointerException e) {
loop = 0;
}
loop++;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// this class , as you guessed it, handles the power-ups movement\
// i am not going into detail with this one
public class powerupmovement extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
int loop = 0;
while (loop < powerups.size()) {
powerups.get(loop).x = powerups.get(loop).x - 1;
if (powerups.get(loop).x < 0) {
powerups.remove(loop);
}
loop++;
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// this one here, handles how long the power up will last
// and resets vaules when its over
public class powerupclock extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
if (manmode == true) {
manmodetimer++;
if (manmodetimer >= 10) {
manmode = false;
firerate = 120;
firedamage = 5;
trifire = false;
manmodetimer = 0;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
// this one is just like the above thread, it counts, every 10 secounds the
// level
// increases by one and the player gets a bonus 10% of his current points
public class levelclock extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
clocktime++;
if (clocktime >= 10) {
clocktime = 0;
level++;
points = points * 1.01;
points = Math.round(points);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// this class here handles the plane getting blowed up , and the game over
// screen playing!
public class planedeath extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
while (Run == true) {
if (dead == true) {
// this sleeping allows the player to acutally see the
// plane blow up,
// before the game ends
try {
Thread.sleep(600);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// so , when game over is true, it takes us to the game
// over screen
gameover = true;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// screen playing!
public class audiomanager extends Thread {
public void run() {
while (Run2 == true) {
if (Run == false) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// this is the loop that handles all the code
Clip clip ;
System.out.println(started);
if (started == false){
{
try
{
clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File("song.wav")));
clip.start();
started = true;
}
catch (Exception exc)
{
exc.printStackTrace(System.out);
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
songtimer++;
System.out.println (songtimer);
if (songtimer == 122){
System.out.println("end of song");
songtimer=0;
started=false;
}
}
}
}
// and thats all folks!
// things that could have been done better :
// well I coulda offloaded allotta the threads to differnt classes
// such as the bullethitdection thread, this would made the game run
// smoother
// also, I coulda added more contet for the user, but times was of the
// essence
// the alien hit boxes should have been done better, they are currently just
// one bit square
// the menu button could have been done in a better fashion and some of the
// images
// could have had a better photoshop job done to them
// scaling is something that also needs to be added, but again, time
// so all in all it wasn't bad for my 1st major attempt at a game.
// its definately better then my classmates, who simply did a text base
// adventure (and when i offered to teach them image handeling so they could
// improve it
// they turned me down!) which is super lame.