//Now I call to my subclass called Propulsion Rocket Rocket r1 = new PropulsionRocket(50, 60, 0); Asteroid[] a; final int numAsteroids = 5; //setup method void setup() { background(0); size(400, 400); r1.drawMe(); a = new Asteroid[numAsteroids]; for(int i = 0; i < numAsteroids; i++) { a[i] = new Asteroid(); } } //draw method void draw() { background(0); if (keyPressed) { if (key == CODED) { if (keyCode == RIGHT) { r1.rotateClockwise(); } else if (keyCode == LEFT) { r1.rotateCounterclockwise(); } } else if (key == ' ') { r1.fireThrusters(); } } 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(); } //Here I added this else for reappearing the asteroids in another place //than they were when there is a collision. Without this new part of the //code the asteroids are just not draw while colliding. else{ a[i].xPos = random(height); a[i].yPos = random(width); } } } //Asteroid Class class Asteroid { // fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; //constructor 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(); } } //This is the subclass I created, it inherits from the Rocket class. //This subclass draws flame from the thrusters when accelerating. class PropulsionRocket extends Rocket { boolean thrusting = false; PropulsionRocket(int initialX, int initialY, float initialRot){ super(initialX, initialY, initialRot); } void fireThrusters() { super.fireThrusters(); thrusting = true; } void drawMe() { super.drawMe(); if(thrusting){ pushMatrix(); translate(xPos, yPos); rotate(rotation); fill(255,0,0); ellipse(halfWidth-10, halfHeight+10, 10, 20); fill(255,255,255); popMatrix(); thrusting = false; } } } 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() { 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; } }