Code

/*

 Germ Wars: the microscopic challenge
 by Herbert Spencer
 
 In this game you are the blue parasite. Your
 goal is to infect all the blood cells. 
 You also need to  escape from the green 
 lymphocytes that will try to erradicate you.
 
 Instructions: eat the read cells and avoid the green ones.
 move with the ARROW KEYS.
 Press 'R' for reseting the game.
 
 */

Element elem[];
int nElements = 102;
int score;
int hits = 0;
int eat = 0;
float grow = 1;
float DRAG = 0.95;
PFont AlbaSuper;
boolean showRadius = false;
color me = color(47,171,203); 



void setup(){
  size(500, 500);
  AlbaSuper = loadFont("AlbaSuper-32.vlw");
  elem = new Element[nElements];
  for (int i=0; i<nElements; i++){
    float x = random(width);
    float y = random(height);
    elem[i] = new Element(x,y);
    if (i == 0) {
      elem[i].mass = 60;
      elem[i].radius = 40;
    }
    if (i > 0 && i < 70) {
      elem[i].mass = 40;
      elem[i].radius = 30;
    }
    if (i >= 70 ){
      elem[i].mass = 80;
      elem[i].radius = 30;
    }
  }
}

void drawTrail(){
  fill(255,50);
  noStroke();
  rect(0,0,width,height);
}

void keyPressed(){
  if(key == ' '){
    showRadius = !showRadius;
  }
  if ((key == 'r') || (key == 'R')){
  setup();
  }
}
void draw(){
  smooth();
  drawTrail();
  for (int i=0; i<nElements; i++){
    elem[i].clearForces();
  }

  float constant = 3.1;
  float rstrength = 2.9;

  for (int i=0; i<nElements; i++){
    for (int j=0; j<i; j++){
      if (i != j){
        float dx = elem[i].px - elem[j].px;
        float dy = elem[i].py - elem[j].py; 
        float dh = sqrt(dx*dx + dy*dy);
        float fx = constant * dx/(dh*dh);
        float fy = constant * dy/(dh*dh);
        float collition = (elem[i].radius + elem[j].radius)/2;

        if (dh < collition*2.5){
          fx *= -11.1;
          fy *= -11.1;
        }
        // hit a predator
        if ((j == 0) && (i>0) && (i<70)) {
          elem[i].accumulateForce(dx*-0.02,dy*-0.02);
          if (dh < collition){
            hits += 1;
            color me = color(252,89,8); 
            //elem[0].radius *= 0.99;
            //elem[0].mass *= 0.99;
            //fx *= -1.1;
            //fy *= -1.1;
          }
        }
        // hit a prey
        if ((j == 0) && (i>=70)) {
          elem[i].accumulateForce(dx*0.004,dy*0.004);
          if (dh <= collition){
            elem[i].alive = false;
            eat += 1;
            color me = color(47,171,203); 
            //grow += 0.1;
            //elem[0].radius *=1.1;
            //elem[0].mass *= 1.1;
          }
        }

        float mx = 0, my = 0;
        if (keyPressed){
          if(keyCode == UP){
            my -= 0.0051;
          }
          if(keyCode == DOWN){
            my += 0.0051;
          }
          if(keyCode == LEFT){
            mx -= 0.0051;
          }
          if(keyCode == RIGHT){
            mx += 0.0051;
          }
        }

        elem[i].accumulateForce(-fx, -fy);
        elem[j].accumulateForce( fx,  fy);
        elem[0].accumulateForce(mx,my);
        float rx = rstrength * random(-1,1); 
        float ry = rstrength * random(-1,1);
        elem[i].accumulateForce(rx, ry);
      }
    }
  }

  for (int i=0; i<nElements; i++){
    if (elem[i].alive){
      elem[i].update();
      elem[i].screenWrap();
      if (showRadius){
        elem[i].drawradius();
      }
      if (i == 0) {
        elem[i].player();
      }
      if (i > 0 && i < 70) {
        if (elem[i].alive){
          elem[i].predator();
        }
      }
      if (i >= 70 ){
        if (elem[i].alive == true){
          elem[i].prey();
        }
      }
    } 
    else { 
      elem[i].die();
    }
  }
  //====SCORE=======
  float life = (width*0.8)-(hits*1.1);
  textFont(AlbaSuper, 32);
  fill(0,20);
  text("GermWars", 8,30);
  textFont(AlbaSuper, 18);
  pushMatrix();
  {
    translate(8,485);
    fill(5,144,180,90);
    text("health =", 0,0);
    rectMode(CORNER);
    noStroke();
    rect(70,-9, life,10);
    fill(222,45,13,50);
    text("infected = "+eat, 0, -25);
  }
  popMatrix();
  if(life<=0){
    fill(#333333);
    rect(0,0, width,height);
    fill(255);
    textAlign(CENTER);
    textFont(AlbaSuper, 32);
    text("You have been disinfected", width/2,height/2);
    }
   if(eat>=32){
    fill(#333333);
    rect(0,0, width,height);
    fill(255);
    textAlign(CENTER);
    textFont(AlbaSuper, 32);
    text("You Won! the disease has spread!", width/2,height/2);
    }
}
class Element {
  float px, py;
  float vx, vy;
  float fx, fy;
  float mass;
  float radius;
  boolean alive = true;
  float seed;

  Element(float xin, float yin){
    px = xin;
    py = yin;
    vx = 0; 
    vy = 0;
    fx = 0; 
    fy = 0;
    radius = 10;
    mass = 10;
    seed = random(1000);
  }

  void clearForces(){
    fx = 0; 
    fy = 0;
  }

  void accumulateForce(float gx, float gy){
    fx += gx;
    fy += gy;
  }

  void update(){
    vx += fx / mass;
    vy += fy / mass;
    vx *= DRAG;
    vy *= DRAG;
    px += vx;
    py += vy;

  }

  void screenWrap(){
    if (py<0){
      py += height;
    } 
    else if (py>height){
      py -= height;
    }
    if (px<0){
      px += width;
    } 
    else if (px>width){
      px -= width;
    }
    else if (py>height){
      py -= height;
    }
  }

  void predator(){
    fill(45,147,15,50);
    stroke(11,70,2);
    strokeWeight(0.5);
    pushMatrix();
    //float orient = atan2(vy, vx);
    translate(px,py);
    //rotate(orient);
    int nPoints = 36;
    beginShape();
    for (int i=0; i<nPoints; i++){
      float t = TWO_PI*(float)i/nPoints; // thus 0...6.283;
      float r = noise(seed+i)*8.0+abs(sin(i+millis()*0.001))*8.0;
      float x = r*cos(t); // circular identity
      float y = r*sin(t); // circular identity
      vertex(x, y);
    }
    endShape(CLOSE);
    popMatrix();
  }

  void prey(){
    fill(222,45,13,50);
    stroke(116,25,5);
    strokeWeight(1.5);
    pushMatrix();
    float orient = atan2(vy, vx);
    translate(px,py);
    rotate(orient);
    int nPoints = 24;
    float a = 6.0;
    float b = (1.26 + 0.25*sin(millis()/500.0+seed)) * a;
    beginShape(POLYGON);
    for (int i=0; i<nPoints; i++){
      float t = TWO_PI*(float)i/nPoints; // thus 0...6.283;
      // from mathworld; equation given in the form r^2 = f(a,b,t)
      float r2 = (a*a) * (cos(2*t) + sqrt(pow(b/a,4.0)- sq(sin(2*t))));
      float r = sqrt(r2);
      float x = r*cos(t); // circular identity
      float y = r*sin(t); // circular identity
      vertex(x, y);
    }
    endShape(CLOSE);
    popMatrix();

  }

  void player(){
    fill(me);
    float orient = atan2(vy, vx);
    pushMatrix();
    {
      translate(px,py);
      rotate(orient);
      beginShape();
      stroke(23,154,188);
      strokeWeight(1.1);
      beginShape(POLYGON);
      for (float t=0; t<=2*PI; t=t+0.2){
        float x = ((1 + sin(t))*15.0)-15.0;
        float y = (cos(t)*(1+sin(t)))*8;
        vertex(x,y);
      }
      endShape(CLOSE);
      fill(255,255,255);
      strokeWeight(1);
      stroke(9,104,129);
      ellipse(7,4,7,7);
      ellipse(7,-4,7,7);
      fill(0);
      noStroke();
      ellipse(8,4.2,2,2);
      ellipse(8,-4.2,2,2);
    }
    popMatrix();
  }
  void drawradius(){
    noFill();
    stroke(5,144,180,50);
    ellipse(px,py,radius,radius);
  }
  
  void die(){
    px = -10000;
    py = -10000;
    mass = 0;
    radius = 0;
    fx = 0;
    fy = 0;     
  }
}

060 -- Personal Arcade: Implementing a classic game (Due W Nov 15)

Statement:Create (or reinvent) a classic arcade game, such as Pong, Breakout, Frogger, Pac-Man, Space Invaders, etc. Give your implementation a personal twist.
This is a game seen through a microscope.

hide statement