ArmedRocket ar1 = new ArmedRocket(200,200,0); Asteroid[] asteroid; ExplodeAsteroid ea = new ExplodeAsteroid(200,200); final int numAsteroids = 5; //Declare the object void setup() { background(0); size(400, 400); smooth(); asteroid = new Asteroid[numAsteroids]; for(int i = 0; i < numAsteroids; i++) { asteroid[i] = new Asteroid(); } } void draw() { background(0); if (keyPressed) { if (key == CODED) { if (keyCode == RIGHT) { ar1.rotateClockwise(); } else if (keyCode == LEFT) { ar1.rotateCounterclockwise(); } if (keyCode == UP) { ar1.fireThrusters(); } } } ar1.drawMe(); for(int i = 0; i < numAsteroids; i++) { if(!((abs( asteroid[i].xPos - ar1.xPos) < 50) && (abs( asteroid[i].yPos - ar1.yPos) < 50))) { asteroid[i].drawMe(); } else { ea.drawMe(); } } } //////////////////////////////////////////////////////////////////////////////////////////////Asteroid class Asteroid { float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; // fields Asteroid( ) { xPos = random(0, 400); yPos = random(0, 400); rotation = random(0, TWO_PI); velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; } //constructor void drawMe() { 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(); fill(255); noStroke(); translate(xPos, yPos); rotate(rotation); 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(); } } //////////////////////////////////////////////////////////////////////////////////////////////Rocket class Rocket { float rotation = 0; float xPos; float yPos; final int halfWidth = 10; final int halfHeight= 10; float velocityX = 0; float velocityY = 0; long lastDrawMillis = 0; // fields Rocket(int initialX, int initialY, float initialRot) { xPos = initialX; yPos = initialY; rotation = initialRot; } // constructor void drawMe() { 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; } fill(255); noStroke(); 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) * 1; velocityY = velocityY - cos(rotation) * 1; } } //////////////////////////////////////////////////////////////////////////////////////////////ArmedRocket class ArmedRocket extends Rocket { ArmedRocket(int initialX, int initialY, float initialRot) { super(initialX, initialY, initialRot); } void fireThrusters() { super.fireThrusters(); pushMatrix(); translate(xPos, yPos); rotate(rotation); ellipseMode(CORNERS); noStroke(); fill(255, 0, 0, 230); ellipse(-halfWidth + 5, halfHeight+5, -halfWidth + 8, halfHeight+10); ellipse(halfWidth - 8, halfHeight+5, halfWidth - 5, halfHeight+10); fill(255, 0, 0, 130); ellipse(-halfWidth + 5, halfHeight+12, -halfWidth + 8, halfHeight+17); ellipse(halfWidth - 8, halfHeight+12, halfWidth - 5, halfHeight+17); fill(255, 0, 0, 90); ellipse(-halfWidth + 5, halfHeight+19, -halfWidth + 8, halfHeight+24); ellipse(halfWidth - 8, halfHeight+19, halfWidth - 5, halfHeight+24); popMatrix(); } } //////////////////////////////////////////////////////////////////////////////////////////////ExplodeAsteroid class ExplodeAsteroid extends Asteroid { ExplodeAsteroid (float x, float y) { float xPos = x; float yPos = y; } void drawMe () { stroke(255); strokeWeight (10); for (int i=0; i<100; i++);{ point ((random(xPos-60, xPos+60)),(random(yPos-60, yPos+60))); } } }