FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Responsive Form, Games
(Moderator: REAS)
   asteroids revisited
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: asteroids revisited  (Read 642 times)
JimQode

251091251091 Email
asteroids revisited
« on: Jan 24th, 2003, 1:39am »

Sorry about the long-long code. I rewrote the code from a more object-oriented approach. Since I'm new to OOP and java I couldnt do firing. Any comments and object oriented firing code welcome.  
 
// Proto-Asteroids 0.1 OOP
// Started by REAS
// Acceleration by nashdj
// Firing, deceleration by REAS
// just tried to add a sample of one asteroid and the destruction of the ship, Anurag.
 
/* Rewrote code from a more object-oriented approach. Since I'm new to OOP and java
   I couldnt do firing. Any comments and object oriented firing code welcome! */
 
// Mouse-based Asteroids, navigate by pressing the mouse
// Fire with the 'space bar'.
// fire asteriods and avoid them to avoid destruction.
int life=1;long time;int cola;int cols;int gepx;int gepxx;
int gepxy;
int tfire = 10;
float[] firex = new float[tfire];
float[] firey = new float[tfire];
float[] firea = new float[tfire];
float vfire;int nfire = 0; int slength = 15; int sheight = 10;
asteroid[] ast = new asteroid[5];
ship gsh;
int scrook = 7;
 
Vector gameStack;
                                                                                                                  
class objectType {
  static final public int ASTEROID = 0;
  static final public int SHIP = 1;
  static final public int FIRE = 2;
}
 
class gameObject {
    int X;
    int Y;
    int Size;
    float speedX;
    float speedY;
    int type;
    boolean wrap = false;
    gameObject() {
      gameStack.add(this);
    }
    void remove() {
      gameStack.remove(this);
    }
    void update() {
      X +=speedX;
      Y +=speedY;
      if (wrap) {
        if (X > width+Size) X = -Size;
        if (X < 0-Size) X += width + Size;
        if (Y > height+Size) Y = -Size;
        if (Y < 0-Size) Y += height + Size;
      }    
      collisionDetection();
      draw();
    }    
   void draw() {
   }
   void collisionDetection() {
     for(int i=0;i<gameStack.size();i++) {
        gameObject target = (gameObject) gameStack.get(i);
        if ((abs(target.X - X) < (Size/2) + (target.Size/2) && abs(target.Y - Y) < (Size/2) + (target.Size/2)) && target != this) {
            println("collision type:"+ type + " type:" + target.type);
        }
     }
   }
}
 
class asteroid extends gameObject {
    asteroid(int pSize) {
      Size = pSize;
      wrap = true;
      type = objectType.ASTEROID;
      init();
    }
    void init() {
      X = int(random(width));
      Y = int(random(height));
      while (abs(speedX * speedY)==0) {
            speedX = int(random( 8 )-4);
            speedY = int(random( 8 )-4);
      }
    }
    void draw() {
      fill(cola);
      stroke(255);
      ellipse(X,Y,Size,Size);
    }
}
 
class ship extends gameObject {
      float Vmax;
      float Acceleration;
      float Deceleration;
      float angle;
      ship() {
        wrap = true;
        X = width/2;
        Y = height/2;
        Acceleration = 2.0;
        Deceleration = 50.0;
        Vmax = 200;
        Size = 8;
        type = objectType.SHIP;
      }        
      void update() {
        angle = atan2(mouseY-Y, mouseX-X);
        if (mousePressed) {
          if(sqrt(speedX*speedY) < Vmax) {
            speedX += cos(angle) * Acceleration;
            speedY += sin(angle) * Acceleration;
          }
        } else {
          if(abs(speedX) > .01) { speedX -= speedX/Deceleration; }
          if(abs(speedY) > .01) { speedY -= speedY/Deceleration; }
        }
        super.update();
    }
 
    void draw() {
      push();
      translate(X,Y);
      rotate(angle);
      fill(cols);  
      stroke(244);  
      beginShape(POLYGON);
      vertex(-slength, sheight); vertex(slength, 0);
      vertex(-slength,-sheight); vertex(-scrook, 0);
      endShape();  
      beginShape(POINTS);
      vertex(-slength, sheight); vertex(slength, 0);
      vertex(-slength,-sheight); vertex(-scrook, 0);
      endShape();
      pop();
   }
 
}
 
void setup(){
  size(500, 500); background(0); noFill();
  stroke(255);
  cola=color(0,100,100);
  cols=color(100,0,100);
  gameStack = new Vector();
  for (int i=0;i<5;i++) {
    ast[i] = new asteroid(20);
  }
  gsh = new ship();
}
 
void loop(){
  gsh.update();
  for (int i=0;i<5;i++) {
    ast[i].update();
  }
}
 
« Last Edit: Jan 24th, 2003, 1:41am by JimQode »  

--- End Of Transmission ---
Pages: 1 

« Previous topic | Next topic »