EAT THAT FISH, Mini Game


Interactive game made in Processing using Java and physics engine Fisica.




import fisica.*;

FWorld world;

FCompound cage;
FCompound fish;
FCompound shark; 

float x = 20;
float y = 40;
float r = PI/6;
float size = 2;
float sizes = 2;
float c = 20;
float u = 40;
float t = PI/6;
int points = 0;
int pointf = 0;

void setup() {
  size(400, 700);
  smooth();
  noStroke();

  Fisica.init(this);

  world = new FWorld();
  world.setEdges();
  //world.remove(world.bottom);
  world.setGravity(0, 15);

  /*
  A circle
   FCircle b = new FCircle(40);
   b.setFill(129, 191, 255);
   b.setNoStroke();
   world.add(b);
   */

  /*
  A blob
   
   FBlob myBlob = new FBlob();
   myBlob.setAsCircle(40);
   world.add(myBlob);
   */

  /*
  Diamond shape
   FPoly r = new FPoly();
   r.vertex(50, 100);
   r.vertex(48, 105);
   r.vertex(50, 110);
   r.vertex(52, 105);
   r.setStatic(false);
   r.setFill(129, 191, 255);
   r.setFriction(0);
   r.setNoStroke();
   world.add(r);
   */

  /* 
   Experimentation
   FPoly t = new FPoly();
   t.vertex(300,320);
   t.vertex(250, 300);
   t.vertex(260,280);
   t.vertex(270,270);
   t.vertex(280,280);
   t.vertex(290,270);
   t.vertex(300,280);
   t.vertex(350, 300);
   r.setStatic(true);
   r.setFill(0);
   r.setFriction(0);
   r.setNoStroke();
   world.add(r);
   */

  /*
  Rectangle
   FBox myBox = new FBox(40, 20);
   world.add(myBox);
   */

  /* 
   Compound, cage
   cage = createCage();
   cage.setPosition(width/2, height/2);
   cage.setRotation(PI/6);
   cage.setBullet(true);
   world.add(cage);
   */

  fish = createFish(size);
  fish.setPosition(x, y);
  fish.setRotation(r);
  fish.setBullet(true);
  world.add(fish);

  shark = createShark(sizes);
  shark.setPosition(width/2+20, height/2+20);
  shark.setRotation(PI/6);
  shark.setBullet(true);
  world.add(shark);
}

void draw() {
  background(80, 120, 200);
  fill(#FFFFFF);
  rect(270, 27, 400, 70);
  fill(#000000);
  text("Shark: ", 290, 50);
  text(points, 350, 50);
  text("Fish: ", 290, 80);
  text(pointf, 350, 80);

  if (frameCount % 50 == 0) {
    FCircle g = new FCircle(15);
    g.setPosition(random(0+10, width-10), 50);
    g.setVelocity(0, 200);
    g.setRestitution(0);
    g.setNoStroke();
    g.setFill(0, 75, 0);
    world.add(g);
  }

  world.step();
  world.draw();
}

/* 
 Function for cage
 FCompound createCage() {
 FBox b1 = new FBox(10, 110);
 b1.setPosition(50, 0);
 b1.setFill(0);
 b1.setNoStroke();
 
 FBox b2 = new FBox(10, 110);
 b2.setPosition(-50, 0);
 b2.setFill(0);
 b2.setNoStroke();
 
 FBox b3 = new FBox(110, 10);
 b3.setPosition(0, 50);
 b3.setFill(0);
 b3.setNoStroke();
 
 FBox b4 = new FBox(110, 10);
 b4.setPosition(0, -50);
 b4.setFill(0);
 b4.setNoStroke();
 
 FCompound result = new FCompound();
 result.addBody(b1);
 result.addBody(b2);
 result.addBody(b3);
 result.addBody(b4);
 return result;
 }
 */
//Iron tail ; Sep 1 ; the fish can only "eat" with its tail
//Edit, it's been fixed. The fish, is cured!
FCompound createFish(float size) {
  FCircle c = new FCircle(size*8);
  c.setPosition(size*4, size*8);
  c.setFillColor(#FA8405);
  c.setNoStroke();

  FCircle b = new FCircle(size);
  b.setPosition(size*3, size*7);
  b.setFillColor(#FFFFFF);

  FPoly p = new FPoly();
  p.vertex(size*8, size*7);
  p.vertex(size*8, size*9);
  p.vertex(size*11, size*10.6);
  p.vertex(size*13, size*8);
  p.vertex(size*11, size*5.4);
  p.setFillColor(#FA8405);
  p.setNoStroke();

  FCompound result = new FCompound();
  result.addBody(c);
  result.addBody(p);
  result.addBody(b);

  return result;
}
void contactEnded(FContact g) {  
  if (g.getBody1().isStatic()) {
    FCircle b = (FCircle)g.getBody1();
    if (b.getSize()>5) {
      b.setSize(b.getSize()*0.9);
    }
  } 

  if (g.getBody2().isStatic()) {
    FCircle b = (FCircle)g.getBody2();
    if (b.getSize()>5) {
      b.setSize(b.getSize()*0.9);
    }
  }
}

void contactStarted(FContact g) {
  FBody ball = null;
  ArrayList temp = fish.getBodies();
  FBody head = temp.get(0);
  if (g.getBody1() == head) { 
    ball = g.getBody2();
    x = fish.getX();
    y = fish.getY();
    r = fish.getRotation();
    size = size+0.4;

    world.remove(fish);

    fish = createFish(size);
    fish.setPosition(x, y);
    fish.setRotation(r);
    fish.setBullet(true);
    world.add(fish);

    pointf += 1;
  } 
  if (g.getBody1() == shark && g.getBody2() != head) {
    ball = g.getBody2();
    c = shark.getX();
    u = shark.getY();
    t = shark.getRotation();
    sizes = sizes+0.1;

    world.remove(shark);

    shark = createShark(sizes);
    shark.setPosition(c, u);
    shark.setRotation(t);
    shark.setBullet(true);
    world.add(shark);

    points += 1;
  } 
  if (size*8 > sizes*25) {
    if (g.getBody1() == head && g.getBody2() == shark || g.getBody2() == head && g.getBody1() == shark ) { 
      x = fish.getX();
      y = fish.getY();
      r = fish.getRotation();
      size = size+sizes;

      world.remove(fish);

      fish = createFish(size);
      fish.setPosition(x, y);
      fish.setRotation(r);
      fish.setBullet(true);
      world.add(fish);


      world.remove(shark);

      pointf += points;
    }
  }
  /*
  Add this in to let the shark and fish "eat" eachother and the world boundaries. 
   else if (g.getBody2() == head || g.getBody2() == shark) {
   ball = g.getBody1();
   }*/

  if (ball == null) {
    return;
  }


  world.remove(ball);
}

FCompound createShark(float sizes) {
  FPoly a = new FPoly();
  a.vertex(sizes*25, sizes*50);
  a.vertex(sizes*30, sizes*65);
  a.vertex(sizes*45, sizes*65);
  a.vertex(sizes*60, sizes*58.5);
  a.vertex(sizes*60, sizes*56);
  a.vertex(sizes*25, sizes*50);
  a.setFillColor(#626262);
  a.setNoStroke();

  FPoly t = new FPoly();
  t.vertex(sizes*60, sizes*56);
  t.vertex(sizes*73.5, sizes*45);
  t.vertex(sizes*65, sizes*57.5);
  t.vertex(sizes*70, sizes*65);
  t.vertex(sizes*60, sizes*58.5);
  t.setFillColor(#626262);
  t.setNoStroke();

  FPoly y = new FPoly();
  y.vertex(sizes*35, sizes*52);
  y.vertex(sizes*45, sizes*45);
  y.vertex(sizes*45, sizes*54);
  y.setFillColor(#626262);
  y.setNoStroke();

  FPoly u = new FPoly();
  u.vertex(sizes*35, sizes*65);
  u.vertex(sizes*40, sizes*70);
  u.vertex(sizes*45, sizes*65);
  u.setFillColor(#626262);
  u.setNoStroke();

  FCircle i = new FCircle(sizes*2.5);
  i.setPosition(sizes*31.5, sizes*55);
  i.setFillColor(#FFFFFF);

  FPoly o = new FPoly();
  o.vertex(sizes*30, sizes*60);
  o.vertex(sizes*31.5, sizes*63.5);
  o.vertex(sizes*33, sizes*60);
  o.setFillColor(#FFFFFF);
  o.setNoStroke();

  FPoly p = new FPoly();
  p.vertex(sizes*33, sizes*60);
  p.vertex(sizes*34.5, sizes*63.5);
  p.vertex(sizes*36, sizes*60);
  p.setFillColor(#FFFFFF);
  p.setNoStroke();

  FCompound result = new FCompound();
  result.addBody(a);
  result.addBody(t);
  result.addBody(y);
  result.addBody(u);
  result.addBody(i);
  result.addBody(o);
  result.addBody(p);

  return result;
}