// 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 ArmedRocket r1 = new ArmedRocket(50, 60, 0); ArrayList a ; Missile m ; // we can change the number of asteroids here. final int numAsteroids = 6; ArrayList mstack; boolean upSpam = false; void setup() { background(0); size(400, 400); r1.drawMe(); 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) { m = r1.fire(); // fire returns missile, but it is not used anywhere mstack.add( m); upSpam = true ; } } else if (key == ' ') { r1.fireThrusters(); } } else { upSpam = false; } //r1.fire(); for(int i= 0; i < mstack.size(); i++) { Missile m = (Missile)mstack.get(i); //if(!((abs(m.xPos - r1.xPos) < 20) && (abs(m.yPos - r1.yPos) < 20))) { m.drawmissile(); m.update(); } //else /*/DUPLICATE DUPLICATE m.drawmissile(); for(int i = 0; i < a.size(); i++) { // makes an asteroid disappear if we collide with it. can you think of a way // to make our rocket disappear? Missile m = (Missile)mstack.get( i ); Asteroid ast = (Asteroid)a.get( i ); if(!((abs(ast.xPos - m.xPos) < 20) && (abs(ast.yPos - m.yPos) < 20))) { ast.drawMe(); } else { ast.destroy( a ); println( a.size() ); } } //DUP DUP DUP DERP*/ 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 // to make our rocket disappear? 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 ); println( a.size() ); } } } 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(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); 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; } } 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; int xPos, yPos, x, y; float speedX, speedY; //constructor Missile(int initialX, int initialY, float initialRot) { xPos = initialX; yPos = initialY; rotation = initialRot; } void drawmissile() { pushMatrix(); translate(xPos , yPos); fill(xPos, yPos, 65); ellipse(0, 0, 40, 40); popMatrix(); } void update(){ 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 draw yPos += speedY; // } }