Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
  By Leona Wu
 interactive_toy1:Tobi the Tapioca
 
 Avoid the falling straws or Tobi will get hurt. "Ouch"would be printed on screen if Tobi gets hit by straw.
 In console log it'll show feedback if Tobi gets hit by straw
 
 Use mouseX to move Tobi from side to side
 Mouse press will change the color of Tobi
 
 reference Daniel Shiffman "Learning Processing" 5-09 Gravity
 */

float ballX= 0;
float ballY1= random(0, 250);
float ballY2= random(0, 250);
float ballY3= random(0, 250);
float ballSizeX1= random(5, 50);
float ballSizeY1= ballSizeX1;
float ballSizeX2= random(10, 60);
float ballSizeY2= ballSizeX2;
float ballSizeX3= random(7, 57);
float ballSizeY3= ballSizeX3;
float ballSpeed= 5;

float strawx= random(30, 370);
float strawy= 0;
float speed = 0;  
float gravity = 0.30; 

float tobiPositionY= 360;
color tobiColor= color(255, 221, 215);

void setup() {
  size(400, 400);
  noCursor();
}

void draw() {
  drawBackground();
  backgroundBalls();
  drawTobi();

  strawFall();

  //When straw touches Tobi text will appear in the middle of the screen and console log
  if (strawx-21<mouseX && strawx+21>mouseX&& strawy-25< 340 && strawy+25>340 ) {
    fill(150, 147, 136);
    textAlign (CENTER, CENTER);
    textSize (80);
    text("Ouch", width/2, height/2);
    println("Tobi got hurt");
  }
}

void drawBackground() {
  //background colour
  background(200, 255, 255);
  fill(119, 156, 209);
  rect(width/2, 400, 400, 200);
  fill(164, 186, 227);
  rect(width/2, 400, 400, 150);
  fill(183, 191, 228);
  rect(width/2, 400, 400, 100);
  fill(196, 197, 227);
  rect(width/2, 400, 400, 50);

  //edges
  fill(241, 217, 191);
  triangle(0, 80, 50, 400, 0, 400);
  triangle(400, 80, 350, 400, 400, 400);
}

void backgroundBalls() {
  //draw balls
  noStroke();
  fill(254, 254, 220);
  //ball1
  ellipse(ballX, ballY1, ballSizeX1, ballSizeY1);
  //ball2
  ellipse(ballX, ballY2, ballSizeX2, ballSizeY2);
  //ball3
  ellipse(ballX, ballY3, ballSizeX3, ballSizeY3);

  //makes balls move from side
  ballX += ballSpeed;

  //when balls reache the edge it'll reset
  if (ballX > width) {
    ballX=0;
    speed=0;
    ballSizeX1=random(5, 50);
    ballSizeY1=ballSizeX1;
    ballSizeX2= random(10, 60);
    ballSizeY2= ballSizeX2;
    ballSizeX3= random(7, 57);
    ballSizeY3= ballSizeX3;
    ballY1=random(0, 250);
    ballY2=random(0, 250);
    ballY3=random(0, 250);
  }
}

void strawFall() {
  fill(254, 236, 152);
  stroke(255);
  rectMode(CENTER);
  rect(strawx, strawy, 30, 120, 5);
  noStroke();
  fill(238, 215, 174);
  rect(strawx+6, strawy, 15, 120, 5);

  //straw speed
  strawy += speed;

  //straw gravity
  speed += gravity;

  //when straw reaches bottom it'll reset to top 
  if (strawy > height) {
    strawy=0;
    speed=0;
    strawx=random(0, 400);
  }
}

void drawTobi() {
  //Tobi moves according to mouseX position
  ellipseMode(CENTER);
  //legs
  fill(tobiColor);
  stroke(205, 157, 149);
  ellipse(mouseX-8, tobiPositionY+22, 10, 15);
  ellipse(mouseX+8, tobiPositionY+22, 10, 15);
  //body
  ellipse(mouseX, tobiPositionY, 43, 50);
  //face
  noStroke();
  fill(138, 105, 93);
  ellipse(mouseX-10, tobiPositionY-5.5, 6, 6);
  ellipse(mouseX+10, tobiPositionY-5.5, 6, 6);
  rect(mouseX, tobiPositionY, 7, 2);
  //hands
  fill(200, 179, 176);
  arc(mouseX+7, tobiPositionY+10, 10, 9, 0, PI+HALF_PI);
  arc(mouseX-7, tobiPositionY+10, 10, 10, radians(-75), radians(179));
}

void mousePressed() {
  //when mouse is pressed TObi changes color
  tobiColor= color(random(255), random(255), random(255));
}