boolean upPressed = false;
boolean downPressed = false;
boolean aPressed = false;
boolean dPressed = false;
float rotationAngle = .2;
PImage[] asteroidPics = new PImage[3];
ArrayList<Exhaust> exhaust;
ArrayList<Bullet> bullets;
ArrayList<Asteroid> asteroids;
int darkCounterLimit = 24*2;
font = createFont("Cambria", 32);
asteroidPics[0] = loadImage("Asteroid0.png");
asteroidPics[1] = loadImage("Asteroid1.png");
asteroidPics[2] = loadImage("Asteroid2.png");
rocket = loadImage("Rocket.png");
asteroids = new ArrayList<Asteroid>(0);
if( lives >= 0 && asteroids.size()>0){
float theta = heading2D(ship.rotation)+PI/2;
ship.update(exhaust, fire);
if(ship.checkCollision(asteroids)){
rotate2D(ship.rotation,-rotationAngle);
rotate2D(ship.rotation, rotationAngle);
ship.acceleration = new PVector(0,shipSpeed);
rotate2D(ship.acceleration, theta);
for(int i = 0; i < bullets.size(); i++){
if(bullets.get(i).update()){
if(bullets.get(i).checkCollision(asteroids)){
while(exhaust.size() > 20){
while(bullets.size() > 30){
for(Asteroid a : asteroids){
for(int i = 0; i < lives; i++){
image(rocket,40*i + 10,ship.r*1.5,2*ship.r,3*ship.r);
if(darkCounter < darkCounterLimit){
for(Asteroid a : asteroids){
fill(0, 255-(darkCounterLimit-darkCounter)*3);
for(Asteroid a : asteroids){
image(rocket,width/2 - 5 * ship.r,height/2-7.5*ship.r,10*ship.r,15*ship.r);
text("GAME OVER", width/2-80-2, height*.75-1);
text("GAME OVER", width/2-80, height*.75);
text("Click anywhere to play again", width/2-80-2, height*.9-1);
text("Click anywhere to play again", width/2-80, height*.9);
text("Stage " + (stage + 1) + " Complete", width/2-120, height/2);
text("Asteroids", width/2-80, height/2);
text("Click anywhere to start stage " + (stage + 2), width/2-100, height*.75);
asteroids = new ArrayList<Asteroid>(0);
} else if (asteroids.size()==0){
exhaust = new ArrayList<Exhaust>();
fire = new ArrayList<Exhaust>();
bullets = new ArrayList<Bullet>();
asteroids = new ArrayList<Asteroid>();
for(int i = 0; i <numAsteroids + diffCurve*stage; i++){
PVector position = new PVector((int)(Math.random()*width), (int)(Math.random()*height-100));
asteroids.add(new Asteroid(position, startingRadius, asteroidPics, stage));
PVector pos = new PVector(0, ship.r*2);
rotate2D(pos,heading2D(ship.rotation) + PI/2);
PVector vel = new PVector(0, bulletSpeed);
rotate2D(vel, heading2D(ship.rotation) + PI/2);
bullets.add(new Bullet(pos, vel));
} else if(keyCode==DOWN){
} else if(keyCode == LEFT){
}else if(keyCode==RIGHT){
ship.acceleration = new PVector(0,0);
} else if(keyCode==DOWN){
ship.acceleration = new PVector(0,0);
} else if(keyCode==LEFT){
} else if(keyCode==RIGHT){
ship.acceleration = new PVector(0,0);
ship.acceleration = new PVector(0,0);
float heading2D(PVector pvect){
return (float)(Math.atan2(pvect.y, pvect.x));
void rotate2D(PVector v, float theta) {
v.x = v.x*cos(theta) - v.y*sin(theta);
v.y = xTemp*sin(theta) + v.y*cos(theta);
public Asteroid(PVector pos, float radius_, PImage[] pics_, int stage_){
float angle = random(2 * PI);
velocity = new PVector(cos(angle), sin(angle));
velocity.mult((50*50)/(radius*radius));
velocity.mult(sqrt(stage + 2));
velocity.mult(dampening);
rotation = new PVector(cos(angle), sin(angle));
spin = (float)(Math.random()*omegaLimit-omegaLimit/2);
int rnd = (int)(Math.random()*3);
void breakUp(ArrayList<Asteroid> asteroids){
for(int i = 0; i < 2; i++){
float angle = random(2*PI);
PVector rand = new PVector(radius*sin(angle), radius*cos(angle));
asteroids.add(new Asteroid(rand, radius*.8, pics, stage));
for(int i = 0; i < 3; i++){
float angle = random(2*PI);
PVector rand = new PVector(radius*sin(angle), radius*cos(angle));
asteroids.add(new Asteroid(rand, radius*.8, pics, stage));
rotate2D(rotation, spin);
circ(position.x, position.y);
if (position.x < radius){
circ(position.x + width, position.y);
} else if (position.x > width-radius) {
circ( position.x-width, position.y);
if (position.y < radius) {
circ(position.x, position.y + height);
} else if (position.y > height-radius){
circ(position.x, position.y-height);
if (position.x > width) {
if (position.y > height){
void circ(float x, float y){
rotate(heading2D(rotation)+PI/2);
image(pic, -radius,-radius,radius*2, radius*2);
float heading2D(PVector pvect){
return (float)(Math.atan2(pvect.y, pvect.x));
void rotate2D(PVector v, float theta) {
v.x = v.x*cos(theta) - v.y*sin(theta);
v.y = xTemp*sin(theta) + v.y*cos(theta);
PImage img = loadImage("laser.png");
public Bullet(PVector pos, PVector vel){
if (position.x > width) {
if (position.y > height){
boolean checkCollision(ArrayList<Asteroid> asteroids){
for(Asteroid a : asteroids){
PVector dist = PVector.sub(position, a.position);
if(dist.mag() < a.radius){
translate(position.x, position.y);
rotate(heading2D(velocity)+PI/2);
image(img, -radius/2, -2*radius, radius, radius*5);
float heading2D(PVector pvect){
return (float)(Math.atan2(pvect.y, pvect.x));
public Exhaust(PVector pos, PVector vel, color col, int rad){
diameter = (float)(Math.random()*rad);
ellipse(position.x, position.y, diameter, diameter);
PImage img = loadImage("Rocket.png");
position = new PVector(width/2, height-50);
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
rotation = new PVector(0,1);
void update(ArrayList<Exhaust> exhaust, ArrayList<Exhaust> fire){
PVector below = new PVector(0, -2*r);
rotate2D(below, heading2D(rotation)+PI/2);
color grey = color(100, 75);
int exhaustVolume = (int)(velocity.mag())+1;
for(int i = 0; i <exhaustVolume; i++){
float angle = (float)(Math.random()*.5-.25);
angle += heading2D(rotation);
PVector outDir = new PVector(cos(angle), sin(angle));
exhaust.add(new Exhaust(below, outDir, grey, 15));
for(int i = 0; i <1; i++){
float angle = (float)(Math.random()*.5-.25);
angle += heading2D(rotation);
PVector outDir = new PVector(cos(angle), sin(angle));
color red = color((int)(200 + Math.random()*55),(int)( 150+Math.random()*105), 50, 250);
fire.add(new Exhaust(below,outDir, red, 5));
velocity.add(acceleration);
if (position.x > width-r) {
if (position.y > height-r){
boolean checkCollision(ArrayList<Asteroid> asteroids){
for(Asteroid a : asteroids){
PVector dist = PVector.sub(a.position, position);
if(dist.mag() < a.radius + r/2){
float theta = heading2D(rotation) + PI/2;
translate(position.x, position.y);
image(img,-r,-r*1.5,2*r,3*r);
float heading2D(PVector pvect){
return (float)(Math.atan2(pvect.y, pvect.x));
void rotate2D(PVector v, float theta) {
v.x = v.x*cos(theta) - v.y*sin(theta);
v.y = xTemp*sin(theta) + v.y*cos(theta);