(12) Creating astroids...

So in previous post, we have a ship that can shoot bullets at... nothing. Not much fun as a game. If we are to try to create a simple astroids clone, then we need... astroids. In my version they will come from outside the screen at random angles and speeds, and traverse in a straight line across. For making them look better, instead of just a circle, I use an object called "PShape" (click to read about ir). This shape is created unique per astroid as I create the shape for each new astroid in the constructor for the object. (Tutorial to objects and explanation of constructor here). I decided to keep the astroid roughly circular to make collision detection easier later...

How does it look like? Watch this:



Try it your self and play with it using this code. Clicking right mouse button spawns new astroid.

ArrayList<astroid> astroids = new ArrayList<astroid>();


// Settings - how many seconds between each new astroid (3 seconds = 3 * 60)
int astroid_rate = 3 * 60;
int astroid_count = 0;
// Size in pixel of nominal astroid
float ast_size = 10;

// Run once
void setup () {
  frameRate(60);
  size(500, 500);
}

// Called 60 times per second
void draw()
{
  int i;
  // Find the angle from x=250, y=250 to the mouse
  float angle = atan2(mouseY - 250, mouseX - 250);

  // 1 new astroid every 5 seconds (60 fps * 4 sec)
  if (astroid_count--==0) {
    astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
      random(-150, 150), random(-150, 150)));
    // Increase rate just a little
    astroid_count = astroid_rate--;
  }

  // Clear screen, black
  background(0);
  // Go through all astroids (if any) and update their position
  for (i = 0; i<astroids.size(); i++) {
    astroid a = astroids.get(i);
    if (a.update()) {
      astroids.remove(i);
    }
  }  
  // "pushMatrix" saves current viewpoint
  pushMatrix();
  // Set 250,250 as the new 0,0 
  translate(250, 250);
  // Rotate screen "angle" 
  rotate(angle);
  fill(255);
  // Draw a triangle (the ship)
  triangle(20, 0, -20, -10, -20, 10);
  // Bring back normal perspektive
  popMatrix();
}

// When right mouse button is pressed, create a new astroid
void mousePressed() {
  if (mouseButton == RIGHT) {
    astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
      random(-80, 80), random(-80, 80)));
  }
}

// Class definition for the astroid
class astroid {
  // An astroid angle, speed, size, rotation
  float angle, speed, size, rotSpeed;
  float position;
  float rotation;
  float xoff, yoff;
  float x, y;
  PShape s;  // The PShape object - Keeps the astroid shape
  float i;

  // Constructor  
  astroid(float _angle, float _speed, float _size, float _rotSpeed, float _xoff, float _yoff) {
    angle = _angle;
    speed = _speed;
    size = _size;
    rotSpeed = _rotSpeed;
    // This will be used when later breaking down astroids
    if (xoff<1000) {
      // Normal - start outside screen
      x = 250+500*cos(angle)+_xoff;
      y = 250+500*sin(angle)+_yoff;
    } else {
      // Start on specific x,y
      x = _xoff-2000;
      y = _yoff-2000;
    }
    rotation = 0; 
    // Generate the shape of the astroid - Some variations for all, roughly sircular
    s = createShape();
    s.beginShape();
    s.fill(255, 255, 100);
    s.noStroke();
    for (i=0; i<TWO_PI; i=i+PI/(random(4, 11))) {
      s.vertex(random(ast_size*0.8, ast_size*1.2)*cos(i), random(ast_size*0.8, ast_size*1.2)*sin(i));
    }
    s.endShape(CLOSE);
  }

  // Update position, return true when out of screen
  boolean update() {
    x = x - cos(angle)*speed;
    y = y - sin(angle)*speed;
    rotation = rotation + rotSpeed; 

    pushMatrix();
    // Set position as the new 0,0 
    translate(x, y);
    // Rotate screen "angle" 
    rotate(rotation);
    // Draw astroid
    scale(size);
    shape(s, 0, 0);
    // Bring back normal perspektive
    popMatrix();

    if (x<-300 || x>800 || y<-300 || y>800) {
      return true;
    } else {
      return false;
    }
  }
}

No comments:

Post a Comment