// ROCKET AND ASTEROIDS flamingRocket r1 = new flamingRocket(50, 60, 0); //added Asteroid[] a; final int numAsteroids = 15; void setup() { background(0); size(400, 400); r1.drawMe(); a = new Asteroid[numAsteroids]; for(int i = 0; i < numAsteroids; i++) { a[i] = new Asteroid(); } } void draw() { background(14, 14, 64); if (keyPressed) { if (key == CODED) { if (keyCode == RIGHT) { r1.rotateClockwise(); } else if (keyCode == LEFT) { r1.rotateCounterclockwise(); } } else if (key == ' ') { r1.fireThrusters(); r1.drawFlames(); //added } } r1.drawMe(); for(int i = 0; i < numAsteroids; i++) { if(!((abs(a[i].xPos - r1.xPos) < 20) && (abs(a[i].yPos - r1.yPos) < 20))) { a[i].drawMe(); } } } class Asteroid { float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; Asteroid() { xPos = random(0, 400); yPos = random(0, 400); rotation = random(0, TWO_PI); velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; } 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(); 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(); } } class Rocket { float rotation = 0; float xPos; float yPos; final int halfWidth = 11; final int halfHeight= 11; float velocityX = 0; float velocityY = 0; long lastDrawMillis = 0; Rocket(int initialX, int initialY, float initialRot) { xPos = initialX; yPos = initialY; rotation = initialRot; } void drawMe() { fill(255, 255, 180); 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) * 1; velocityY = velocityY - cos(rotation) * 1; } //sub class } class flamingRocket extends Rocket { flamingRocket(int initialX, int initialY, float initialRot) { super(initialX, initialY, initialRot); } void drawFlames() { smooth(); pushMatrix(); translate (xPos, yPos); rotate (rotation); fill(255, 0, 0); noStroke(); ellipse(halfHeight-11,halfWidth+30 ,20, 50); fill(random(0,255),random(0,255),random(0,255)); ellipse(halfHeight-11,halfWidth+30 ,15, 40); popMatrix(); } }