bethadillon
YaBB Newbies
Offline
Posts: 15
Asteroids Game!
Sep 30th , 2006, 9:04pm
Okay so I want to make the ship fire photons (lines) and makes a sound when the spacebar or another key is hit. In Java, understanding there are other components at play here, I think it would look something like: <code> if (e.getKeyChar() == ' ' && ship.active) { if (sound & !paused) fireSound.play(); photonTime = System.currentTimeMillis(); photonIndex++; if (photonIndex >= MAX_SHOTS) photonIndex = 0; photons[photonIndex].active = true; photons[photonIndex].x = ship.x; photons[photonIndex].y = ship.y; photons[photonIndex].deltaX = 2 * MAX_ROCK_SPEED * -Math.sin(ship.angle); photons[photonIndex].deltaY = 2 * MAX_ROCK_SPEED * Math.cos(ship.angle); } </code> My problem is trying to do this as a subclass in Processing that fits into: <code> // ROCKET AND ASTEROIDS // This code is the basis for an object-oriented version of the old arcade game Asteroids! // - originally coded by Michael Mateas and Mayhew Seavey III // - revised by Beth A. Dillon Rocket r1 = new Rocket(50, 60, 0); Asteroid[] a; // we can change the number of asteroids here. final int numAsteroids = 5; void setup() { background(0); size(400, 400); noFill(); stroke(255); 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.drawMe(); for(int i = 0; i < numAsteroids; i++) { // makes an asteroid disappear if we collide with it 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; 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; 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; } } </code>