//This is the latest version of the code for the Asteroids game... //What happens so far: //We have a field of asteroids (in an ArrayList). //We have a rocket of type ArmedRocket that can fire Missiles. //These missiles can hit an asteroid and blow it into two smaller asteroids. //The two smaller asteroids tumble off in different directions. //Still to do: //The rocket doesn't explode when it hits an asteroid. ArmedRocket r1 = new ArmedRocket(50, 60, 0); ArrayList asteroids; // Asteroid[] a; // we can change the initial number of asteroids here. final int numAsteroids = 5; Missile[] m; final int maxMissiles = 5; void setup() { background(0); size(400, 400); r1.drawMe(); asteroids = new ArrayList(); // a = new Asteroid[numAsteroids]; m = new Missile[maxMissiles]; for(int i = 0; i < numAsteroids; i++) { asteroids.add(new Asteroid()); // a[i] = new Asteroid(); } } 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 < asteroids.size(); i++) { // makes an asteroid disappear if we collide with it. can you think of a way // to make our rocket disappear? /* if (!a[i].collision(r1)) a[i].drawMe(); */ Asteroid asteroid = (Asteroid)asteroids.get(i); if (!asteroid.collision(r1)) asteroid.drawMe(); } for(int missileCounter = 0; missileCounter < maxMissiles; missileCounter++) { if (m[missileCounter] != null) { m[missileCounter].drawMe(); for(int asteroidCounter = 0; asteroidCounter < asteroids.size(); asteroidCounter++) { Asteroid asteroid = (Asteroid)asteroids.get(asteroidCounter); if (asteroid.collision(m[missileCounter])) { asteroid.destroy(asteroids); m[missileCounter] = null; break; } /* if (a[asteroidCounter].collision(m[missileCounter])) a[asteroidCounter].destroy(); */ } if (m[missileCounter] != null && !m[missileCounter].isVisible()) m[missileCounter] = null; } } } // With keyPressed(), we only check to see if we should fire a missile void keyPressed() { if (key == 'm') { // Only add a missle if there are less than maxMissiles on the screen. // Runs through the missile array checking for any empty slots (null) for(int i = 0; i < maxMissiles; i++) { if (m[i] == null) { m[i] = r1.fire(); break; // break is a java keyword that breaks out of for loop - // Here, there is no reason to keep looking for blank missile slots once we've found one } } } } class Asteroid { // fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis; boolean large; //constructor Asteroid() { this(true, random(0, 400), random(0, 400), 0); } Asteroid(boolean isLarge, float initialX, float initialY, long previousDrawTime) { println(initialX + " " + initialY); xPos = initialX; yPos = initialY; rotation = random(0, TWO_PI); if (isLarge) { velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; } else { velocityX = sin(rotation)*25; velocityY = -cos(rotation)*25; } large = isLarge; lastDrawMillis = previousDrawTime; } 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); if (!large) scale(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(); } boolean collision(Rocket r) { if ((r.xPos >= xPos - 26 && r.xPos <= xPos + 22) && (r.yPos >= yPos - 24 && r.yPos <= yPos + 26)) return true; else return false; } boolean collision(Missile m) { if ((m.xPos >= xPos - 26 && m.xPos <= xPos + 22) && (m.yPos >= yPos - 24 && m.yPos <= yPos + 26)) return true; else return false; } // Removes the asteroid from the list. If the asteroid is large, adds to new small asteroids to the list. 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() { 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; } } class ArmedRocket extends Rocket { ArmedRocket(int initialX, int initialY, float initialRot) { super(initialX, initialY, initialRot); // the keyword super refers to the parent. Here we're calling the parents constructor } Missile fire() { Missile m = new Missile(xPos, yPos, rotation); return m; } } class Missile { float xPos; float yPos; final float velocity = 150; float velocityX; float velocityY; long lastDrawMillis; Missile(float initialX, float initialY, float rotation) { xPos = initialX; yPos = initialY; velocityX = sin(rotation) * velocity; velocityY = -cos(rotation) * velocity; lastDrawMillis = millis(); } void drawMe() { long currentMillis = millis(); float timeSinceLastDraw = ((float)currentMillis - (float)lastDrawMillis)/1000; lastDrawMillis = currentMillis; xPos = xPos + velocityX * timeSinceLastDraw; yPos = yPos + velocityY * timeSinceLastDraw; pushMatrix(); translate(xPos, yPos); ellipse(0, 0, 5, 5); popMatrix(); } boolean isVisible() { if ((xPos > 400) || (xPos < 0) || (yPos > 400) || (yPos < 0)) return false; else return true; } }