/* Matt Martell IAT 800 - Dr. Christopher Shaw Due Oct. 6, 2008 A2-14 - Making Asteroids Game - Thrusters activate */ // 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 Rocket r1 = new Rocket(50, 60, 40); Asteroid[] a; // we can change the number of asteroids here. final int numAsteroids = 5; 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(0); if (keyPressed) { if (key == CODED) { if (keyCode == RIGHT) { r1.rotateClockwise(); } else if (keyCode == LEFT) { r1.rotateCounterclockwise(); } } else if (key == ' ') { r1.fireThrusters(); r1.drawThrusters(); } } r1.drawMe(); for(int i = 0; i < numAsteroids; i++) { // makes an asteroid disappear if we collide with it. can you think of a way // to make our rocket disappear? if(!((abs(a[i].xPos - r1.xPos) < 20) && (abs(a[i].yPos - r1.yPos) < 20))) { a[i].drawMe(); } } } 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; //Sets Boundry of Asteroid 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 { // 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; //Sets boundry behavior 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; } //Thrusters Image out back of rocket void drawThrusters() { pushMatrix(); translate (xPos, yPos); rotate (rotation); beginShape (POLYGON); beginShape (POLYGON); fill (255,0,0); vertex (-7,10); vertex (-9,15); vertex (-6,13); vertex (-11,20); vertex (-5,18); vertex (-7,23); vertex (-4,21); vertex (0,28); vertex (4,21); vertex (7,23); vertex (5,18); vertex (11,20); vertex (6,13); vertex (9,15); vertex (7,10); endShape(CLOSE); fill (255,255,0); arc(0,15,5,11,PI*1/2, PI*3/2); arc(0,15,5,11,PI*3/2, PI*1/2); endShape(); popMatrix(); } }