// ROCKET AND ASTEROIDS (first pass) // This code is the basis for an object-oriented version of the old arcade game Asteroids! // Right now it doesn't do very much...the rocketship turns and fires thrusters, and // there are five asteroids tumbling around. // - originally coded by Michael Mateas and Mayhew Seavey III //Dylan McIntyre 301118562 // ArmedRocket r1 = new ArmedRocket(50, 60, 0); ArrayList a ; // we can change the number of asteroids here. final int numAsteroids = 5; ArrayList mstack; boolean upSpam = false; //boolean upSpam2 = false; int lives = 3; //PFont font; void setup() { background(0); size(400, 400); r1.drawMe(); //font = loadFont("AgencyFB-Reg-24.vlw"); //textFont(font, 24); int lives = 3; mstack = new ArrayList(); a = new ArrayList(); for(int i = 0; i < numAsteroids; i++) { a.add( new Asteroid() ); } } void draw() { background(0); if (keyPressed) { if (key == CODED) { if (keyCode == RIGHT) { r1.rotateClockwise(); } else if (keyCode == LEFT) { r1.rotateCounterclockwise(); } else if (keyCode == UP && !upSpam) { Missile m = r1.fire(); mstack.add( m); upSpam = true ; } } else if (key == ' ') { r1.fireThrusters(); } } else { upSpam = false; //missile // upSpam2 = false; //sillymissile } // collision for(int i = 0; i < mstack.size(); i++) { //println(mstack.size()); Missile m = (Missile)mstack.get( i ); m.drawmissile(); // missile m.update(); // update makes it move for (int f = 0; f < a.size(); f++){ Asteroid ast = (Asteroid)a.get( f ); if(m.collision(ast)){ mstack.remove(m); ast.destroy(a); } } } { fill(255); text("LIVES: ", 10, 15); text(lives, 50, 15); } r1.drawMe(); for(int i = 0; i < a.size(); i++) { // makes an asteroid disappear if we collide with it. can you think of a way Asteroid ast = (Asteroid)a.get( i ); if(!((abs(ast.xPos - r1.xPos) < 20) && (abs(ast.yPos - r1.yPos) < 20))) { ast.drawMe(); } else { ast.destroy( a ); r1 = new ArmedRocket(50, 60, 0); // changes location of rocket r1 back to starting teleportation lol lives--; println (lives);//add in loss of life here } } if (lives <= 0){ background(255); textAlign(CENTER); fill (0); text( " THE ASTEROIDS LEAD BY THEIR MONSTER LEADER\n HAVE DESTROYED YOUR PLANETS LAST CHANCE AT\n SURVIVAL... GOOD JOB!\n\n THE END " ,width/ 2, 180); } } class Asteroid { // fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; boolean large ; //constructor Asteroid() { xPos = random(0, 400); yPos = random(0, 400); rotation = random(0, TWO_PI); velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; large = true ; } // another constructor!! Asteroid( boolean big, float initX, float initY, long lastMillis) { xPos = initX ; yPos = initY ; lastDrawMillis = lastMillis ; rotation = random(0, TWO_PI); velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; large = big ; } void drawMe() { // fill(255); long currentMillis = millis(); float timeSinceLastDraw = ((float)currentMillis - (float)lastDrawMillis)/1000; lastDrawMillis = currentMillis; xPos = xPos + velocityX * timeSinceLastDraw; yPos = yPos + velocityY * timeSinceLastDraw; if (xPos > 400) { xPos -= 400; } else if (xPos < 0) { xPos += 400; } if (yPos > 400) { yPos -= 400; } else if (yPos < 0) { yPos += 400; } pushMatrix(); translate(xPos, yPos); rotate(rotation); if( !large ) scale( 0.5, 0.5 ); int[] xpts = { -20, 0, 18, 22, 5, 20, 17, -3, -17, -18, -26 }; int[] ypts = { -15, -24, -20, -5, 0, 10, 20, 26, 23, 14, 7 }; beginShape(POLYGON); for(int i = 0; i < 11; i++) { vertex(xpts[i], ypts[i]); } endShape(); popMatrix(); } void destroy(ArrayList asteroids) { asteroids.remove(this); if (large) { asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis)); asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis)); } } } class Rocket { // fields float rotation = 0; float xPos; float yPos; final int halfWidth = 10; final int halfHeight= 10; float velocityX = 0; float velocityY = 0; long lastDrawMillis = 0; // constructor Rocket(int initialX, int initialY, float initialRot) { xPos = initialX; yPos = initialY; rotation = initialRot; } void drawMe() { fill(225, 225, 225); long currentMillis = millis(); float timeSinceLastDraw = ((float)currentMillis - (float)lastDrawMillis)/1000; lastDrawMillis = currentMillis; xPos = xPos + velocityX * timeSinceLastDraw; yPos = yPos + velocityY * timeSinceLastDraw; if (xPos > 400) { xPos -= 400; } else if (xPos < 0) { xPos += 400; } if (yPos > 400) { yPos -= 400; } else if (yPos < 0) { yPos += 400; } pushMatrix(); translate(xPos, yPos); rotate(rotation); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth,halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); popMatrix(); } void rotateClockwise() { rotation = rotation + 0.1; } void rotateCounterclockwise() { rotation = rotation - 0.1; } void fireThrusters() { velocityX = velocityX + sin(rotation) * 2; velocityY = velocityY - cos(rotation) * 2; } // // println ("lives"); } class ArmedRocket extends Rocket { ArmedRocket(int initialX, int initialY, float initialRot) { super( initialX, initialY, initialRot ); } Missile fire() { Missile m = new Missile((int)xPos, (int)yPos, rotation); return m; } } class Missile { float rotation = 0; float xPos, yPos; int startingX, startingY; float speedX, speedY; long lastDrawMillis = 0; float velocityX = 0; float velocityY = 0; //constructor Missile(int initialX, int initialY, float initialRot) { startingX = initialX; startingY = initialY; xPos = initialX; yPos = initialY; rotation = initialRot; } void drawmissile() { if(dist(xPos, yPos, startingX, startingY) < 200) // distance the missile travels before being removed { pushMatrix(); translate(xPos , yPos); fill(xPos, yPos, 65); ellipse(0, 0, 10, 10); popMatrix(); } else { mstack.remove(this); } /* long currentMillis = millis(); float timeSinceLastDraw = ((float)currentMillis - (float)lastDrawMillis)/1000; lastDrawMillis = currentMillis; xPos = xPos + velocityX * timeSinceLastDraw; yPos = yPos + velocityY * timeSinceLastDraw; if (xPos > 400) { xPos -= 400; } else if (xPos < 0) { xPos += 400; } if (yPos > 400) { yPos -= 400; } else if (yPos < 0) { yPos += 400; }*/ } void update(){ //pythagorean theorum float speedX = 5*cos(rotation - (PI / 2));//fires missile in the correct facing direction float speedY = 5*sin(rotation - (PI / 2));// xPos += speedX; //allows the update to make it move. updates current xpos yPos += speedY; // } boolean collision(Asteroid ast){ if(ast.large) { if(!((abs(ast.xPos - xPos) < 30) && (abs(ast.yPos - yPos) < 30))) { return false; } else { return true; } } else { if(!((abs(ast.xPos - xPos) < 15) && (abs(ast.yPos - yPos) < 15))) { return false; } else { return true; } } } }