We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Asteroids Game!
Page Index Toggle Pages: 1
Asteroids Game! (Read 812 times)
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>
Re: Asteroids Game!
Reply #1 - Sep 30th, 2006, 10:08pm
 
Here are some thoughts to help you get started. . .

Most likely, you'll want to create a "Photon" class to describe an individual photon.  It would have the properties that you use in your example code: x,y, deltaX, deltaY, active, etc.

Code:

class Photon {
float x,y,deltaX,deltaY;
etc.
}


You ship could then include an array of Photon objects as one of its properties, as well as a variable to keep track of how many shots have been fired.

Code:

class Rocket {
Photon[] photons = new Photon[10];
int photonIndex = 0;
etc.


Then when a key is pressed,

http://www.processing.org/reference/keyPressed_.html  

you can call a method inside of the Rocket to fire a photon, running your code:

Code:

void firePhoton() {
photonIndex++;
if (photonIndex >= photons.length) photonIndex = 0;
photons[photonIndex] = new Photon(x,y, etc.)
}


You could also add a loop to draw all of the relevant photons inside the ship's drawMe() method, calling a drawMe() method that you would write into the Photon class.

Code:

for (int i = 0; i < photonIndex; i++) {
photons[i].drawMe()
}
Page Index Toggle Pages: 1