Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
Robot Interactive Toy
 Author: Jonathon Klassen
 Date: October 3, 2015
 */
PVector pos; //toy position
PVector fPos; //firework position
float rLeg; //right leg y position
float lLeg; //left leg y position
float jSpeed=8; //jump speed
float[] starPosx = new float[40]; //stars' x positions
float[] starPosy = new float[40]; //stars' y position
int speed=1; //leg move speed
int fSpeed=1; //firework speed
int dir=0; //horizontal direction indicator
int state=0; //walking state
int fState=0; //firework state
int fireSize=0; //firework size
color f = color(random(0, 255), random(0, 255), random(0, 255)); //firework color
boolean walk = false; //whether toy is moving
boolean jump = false; //whether toy is jumping
boolean launch = false; //whether firework has launched
void setup() {
  size(600, 600); //set screen size
  pos = new PVector(200, 295); //set toy position
  fPos = new PVector(300, 400); //set firework position
  rLeg = pos.y+53; //set right leg position in relation to toy position
  lLeg = pos.y+53; //set left leg position in relation to toy position
  //for loop which randomizes star positions
  for (int i=0; i<starPosx.length; i++) {
    starPosx[i] = random(5, 600); //randomize stars' x positions
    starPosy[i] = random(0, 370); //randomize stars' y positions
  }
  rectMode(CENTER);
}

void draw() {
  background(7, 14, 80);
  firework(); //function which handles the firework
  strokeWeight(3);
  stroke(0);
  fill(255);
  drawBackground(); //function which draws the background
  drawBot(); //function which draws majority of toy
  turnAround(); //function which draws reflecting parts of toy
  moveToy(); //function which handles walking
  doJump(); //function which handles jumping
  boundaries(); //function which keeps toy on screen
  fill(50, 50, 50, 100); 
  rect(300, 300, 610, 610); //apply filter on screen
}

void drawBackground() {
  noStroke();
  fill(244, 250, 162, 200); 
  ellipse(40, 40, 200, 200); //draw moon
  //for loop which draws stars
  for (int i=0; i<starPosx.length; i++) {
    stroke(255);
    ellipse(starPosx[i], starPosy[i], 5, 5); //draw stars
  }
  stroke(0);
  fill(5, 62, 5);
  //draw hills
  ellipse(450, 300, 400, 150);
  ellipse(180, 300, 400, 100);
  fill(10);
  rect(300, 375, 600, 150); //draw road
  //for loop which draws road markings
  for (int i=0; i<6; i++) {
    fill(210, 210, 0);
    rect(50+i*100, 350, 50, 10); //draw road markings
  }
  fill(5, 62, 5);
  rect(300, 525, 600, 150); //draw grass
}

void drawBot() {
  fill(30);
  rect(pos.x+20, rLeg, 20, 40); //draw right leg
  rect(pos.x-20, lLeg, 20, 40); //draw left leg
  fill(80);
  rect(pos.x, pos.y, 80, 120, 5); //draw body
  fill(111, 57, 0);
  rect(pos.x, pos.y+43, 80, 5); //draw belt
  fill(80);
  rect(pos.x, pos.y-75, 30, 40); //draw firework pipe
  fill(50);
  ellipse(pos.x, pos.y-62, 80, 45); //draw hunch
}

void turnAround() {
  if (dir == 0) { //if the robot is walking right
    //draw the following parts oriented right
    fill(80);
    rect(pos.x+20, pos.y+10, 40, 60); //draw body door
    line(pos.x+30, pos.y, pos.x+30, pos.y+40); //draw seam
    //for loops which draws buttons
    for (int i=0; i<4; i++) { 
      for (int j=0; j<2; j++) {
        fill(185, 185, 0);
        ellipse(pos.x+25-15*j, pos.y+i*12-3, 5, 5); //draw button
      }
    }
    fill(100);
    ellipse(pos.x+30, pos.y-30, 40, 40); //draw head
    fill(182, 32, 15);
    ellipse(pos.x+42, pos.y-35, 15, 15); //draw eye
    fill(50);
    rect(pos.x+33, pos.y-13, 40, 20); //draw jaw
    fill(120);
    rect(pos.x+42, pos.y-50, 25, 28, 5); //draw hat
    rect(pos.x+30, pos.y-55, 43, 38, 5);
    rect(pos.x+19, pos.y-50, 25, 28, 5);
    fill(255, 255, 0);
    ellipse(pos.x+42, pos.y-55, 7, 14);
    //if the toy isn't jumping
    if (jump == false) {
      fill(90);
      rect(pos.x-19, pos.y+5, 15, 60); //draw arm upwards
      fill(50);
      rect(pos.x-19, pos.y+25, 21, 10); //draw cuff upwards
      //otherwise
    } else if (jump == true) {
      fill(90);
      rect(pos.x-19, pos.y-35, 15, 60); //draw arm downwards
      fill(50);
      rect(pos.x-19, pos.y-55, 21, 10); //draw cuff downwards
    }
    ellipse(pos.x-19, pos.y-15, 25, 25); //draw shoulder
  } else if (dir == 1) { //if the robot is walking left
    //draw the following parts oriented left
    fill(80);
    rect(pos.x-20, pos.y+10, 40, 60); //draw body door
    line(pos.x-30, pos.y, pos.x-30, pos.y+40); //draw seam
    //for loop which draws buttons
    for (int i=0; i<4; i++) { 
      fill(185, 185, 0);
      ellipse(pos.x-35, pos.y+i*12-3, 5, 5); //draw button
    }
    fill(100);
    ellipse(pos.x-30, pos.y-30, 40, 40); //draw head
    fill(182, 32, 15);
    ellipse(pos.x-42, pos.y-35, 15, 15); //draw eye
    fill(50);
    rect(pos.x-33, pos.y-13, 40, 20); //draw jaw
    fill(120);
    rect(pos.x-42, pos.y-50, 25, 28, 5); //draw hat
    rect(pos.x-30, pos.y-55, 43, 38, 5);
    rect(pos.x-19, pos.y-50, 25, 28, 5);
    fill(255, 255, 0);
    ellipse(pos.x-42, pos.y-55, 7, 14);
    //if the toy isn't jumping
    if (jump == false) {
      fill(90);
      rect(pos.x+19, pos.y+5, 15, 60); //draw arm upwards
      fill(50);
      rect(pos.x+19, pos.y+25, 21, 10); //draw cuff upwards
      //otherwise
    } else if (jump == true) {
      fill(90);
      rect(pos.x+19, pos.y-35, 15, 60); //draw arm downwards
      fill(50);
      rect(pos.x+19, pos.y-55, 21, 10); //draw cuff downwards
    }
    ellipse(pos.x+19, pos.y-15, 25, 25); //draw shoulder
  }
}

void moveToy() {
  //if the "d" key is pressed
  if (keyPressed && key == 'd' || key == 'D') {
    walk=true; //recognize the toy is walking
    pos.x+=2; //move the toy right
    dir=0; //set the toy's direction to right
    //if the "a" key is pressed
  } else if (keyPressed && key == 'a' || key == 'A') {
    walk=true; //recognize the toy is walking
    pos.x-=2; //move the toy left
    dir=1; //set the toy's direction to left
    //otherwise
  } else {
    walk=false; //recognize the toy is not moving
  }
  //if the toy isn't walking, or is in the midst of jumping
  if (walk == false || walk == true && jump == true) {
    rLeg = pos.y+58; //reset right leg position
    lLeg = pos.y+58; //reset left leg position
    state=0; //reset walking state
  }
  //if the toy is walking, and isn't jumping
  if (walk == true && jump == false) {
    //if the walking state is 0
    if (state == 0) {
      rLeg = pos.y+53; //set right leg position slightly higher
      lLeg = pos.y+53; //set left leg position slightly higher
      state=1; //proceed to next walking state
    }
    //if the walking state is 1
    if (state == 1) {
      rLeg +=speed; //move the right leg downwards
      lLeg -=speed; //move the left leg upwards
      //if the right leg moves too high or too low
      if (rLeg >= pos.y+60 || rLeg <=pos.y+46) { 
        speed*=-1; //reverse both legs' directions
      }
    }
  }
}

void doJump() {
  //if the "w" key is pressed
  if (keyPressed && key == 'w' || key == 'W') {
    jump = true; //the toy is jumping
    walk = false; //the toy is not walking
    //otherwise if the toy is coming down after a jump
  } else if (jSpeed < -8) {
    jump=false; //the toy is no longer jumping
    pos.y=295; //reset the toy's y position to its default
  }
  //if the toy is jumping
  if (jump == true) {
    pos.y-=jSpeed; //subtract jump speed from the toy's y position
    jSpeed-=0.3; //reduce jump speed to bring the toy back down
  }
  //if the toy is no longer jumping
  if (jump == false) {
    jSpeed = 8; //reset jump speed
  }
}

void boundaries() {
  //if the toy accidentally skips downwards
  if (pos.y+80 > 375) {
    jump=false; //ensure the toy isn't jumping
    pos.y = 295; //reset the toy's y position
  }
  //if the toy goes too far right
  if (pos.x >= width+60) {
    pos.x = -50; //relocate toy to left side of screen
  }
  //if the toy goes too far left
  if (pos.x <=-60) {
    pos.x = 650; //relocate toy to right side of screen
  }
}

void firework() {
  //if the firework has detonated
  if (fState == 1) {
    noFill(); //make the original firework invisible
    noStroke();
  } else { //otherwise
    fill(255); //make the original firework visible
    noStroke();
  }
  ellipse(fPos.x, fPos.y, 5, 5); //draw the firework
  //if the space key is pressed and the firework hasn't launched
  if (keyPressed && key == ' ') { 
    launch = true; //launch the firework
  }
  //if the firework hasn't launched
  if (launch == false) {
    fPos.x = pos.x; //reset firework x position beneath the player
    fPos.y = pos.y+50; //reset firework y position
    fState=0; //reset firework state
  }
  //if the firework has launched
  if (launch == true) {
    //if the firework state is 0
    if (fState == 0) {
      fPos.x+=random(-3, 3); //randomize the x position slightly
      fPos.y-=5; //launch it upwards
      //if the firework reaches the upper limits of the screen
      if (fPos.y <=int(random(30, 80))) {
        fState=1; //set firework state to 1
      }
    }
    //if the firework state is 1
    if (fState == 1) {
      fill(255, 255, 255, 255-fireSize*10);
      strokeWeight(10);
      stroke(f, 255-fireSize*3);
      //draw one ring of the firework explosion
      ellipse(fPos.x, fPos.y, 20+5*fireSize, 20+5*fireSize);
      //for loop which draws firework sparkles
      for (int i=0; i<10; i++) { 
        strokeWeight(2);
        //draw random sparkles below firework
        ellipse(fPos.x+random(-50, 50), fPos.y+random(20+5*fireSize), 5, 5);
      }
      fireSize++; //add 1 to firework size
      //if the firework size reaches 20
      if (fireSize ==20) {
        launch = false; //reset the firework
        fireSize=0; //reset firework size
        f = color(random(0, 255), random(0, 255), random(0, 255)); //randomize firework color
      }
    }
  }
}