Kinetic Interfaces: Week3 – Catch the triangle! (Ribirth Huang)

For this assignment I made a simple game. In the game you should move the mouse, which is an triangle to catch the other triangle. When you click the mouse, there would be a circle and a triangle dropping from the top. What you should do is use mouse to touch the moving triangle. Once […]

ArrayList<Tri> tris = new ArrayList<Tri>();
ArrayList<Ball> balls = new ArrayList<Ball>();

void setup() {
  size(500, 600);
  background(255, 255, 0);
}

void draw() {
  background(255, 255, 0);
  strokeWeight(5);
  noFill();
  triangle(mouseX, mouseY +10, mouseX +10, mouseY -10, mouseX-10, mouseY -10);
  
  
  //ellipse(random(0,500),random(0,500),20,20);
  
  
  for (int i=0; i<tris.size(); i++) {
    tris.get(i).move();
    tris.get(i).bounce();
    tris.get(i).display();
    tris.get(i).removeBall(i);
  }
  for (int i=0; i<balls.size(); i++) {
    balls.get(i).move();
    balls.get(i).bounce();
    balls.get(i).display();
  }
  
}



void mouseClicked() {
  balls.add(new Ball(random(5, 590), 10));
  tris.add(new Tri(random(5, 590), 10));
}

class Ball {
  float x1, y1, size1;
  float xspeed1, yspeed1;

  Ball(float tempX, float tempY) {
    x1 = tempX;
    y1 = tempY;
    size1 = 20;

    xspeed1 = random(-5, 5);
    yspeed1 = random(3, 10);
  }

  void display() {
    ellipse(x1, y1, size1, size1);
  }

  void move() {
    x1 += xspeed1;
    y1 += yspeed1;
  }

  void bounce() {

   

    if (y1 < 0) {
      yspeed1 = -yspeed1;
    } else if (y1 > width) {
      yspeed1 = -yspeed1;
    }
    if (x1 < 0) {
      xspeed1 = -xspeed1;
    } else if (x1 > width) {
      xspeed1 = -xspeed1;
    }
  

  //void removeBall(int i) {
    if (abs(x1-mouseX)<10 && abs(y1-mouseY)<10) {
      balls.add(new Ball(random(5, 590), 10));
      background(255,0,0);
    }
  }
}

class Tri {
  float x, y, size;
  color clr;
  float xspeed, yspeed;

  Tri(float tempX, float tempY) {
    x = tempX;
    y = tempY;
    //size = 20;
    //clr = color(random(255), random(255), random(255));

    xspeed = random(-2, 6);
    yspeed = random(3, 8);
  }

  void display() {
    //fill(clr);
    triangle(x, y+10, x+10, y-10, x-10, y-10 );
  }

  void move() {
    x += xspeed;
    y += yspeed;
  }

  void bounce() {




    if (y < 0) {
      yspeed = -yspeed;
    } else if (y> height) {
      yspeed = -yspeed;
    }
    if (x < 0) {
      xspeed = -xspeed;
    } else if (x > width) {
      xspeed = -xspeed;
    }
  }

  void removeBall(int i) {
    if (abs(x-mouseX)<10 && abs(y-mouseY)<10) {
      tris.remove(i);
      background(0,255,0);
      balls.remove(new Ball(random(5, 590), 10));
    }
  }
}