Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Interactive Toy Robot created by Tyler Moretto
//Exhaust Idea from Nicolas Hesler's Cylon Lander

float robotX;
float robotY;
float robotSpeedX;
float robotSpeedY;
float robotExhaustFlamesSize;
float robotExhaustFlamesExtra;
float robotExhaustFlamesAmount;
float gravity=.015;

//Setup the basic canvas
void setup() {
  frameRate(60);
  size(400, 400);

  //Robot Starting Position
  robotX=width/2;
  robotY=height/2;

  //Robot Starting Speed;
  robotSpeedX=0;
  robotSpeedY=0;
}

//Create each function on the screen
void draw() {
  robotSpeed();
  robotFallSpeed();
  robotPosition();
  robotOnScreen();
  robotFuel();
  drawBackground();
  drawStars(130, 10, 1);
  drawStars(290, 30, 1);
  drawStars(50, 50, 1);
  drawStars(230, 70, 1);
  drawStars(140, 100, 1);
  drawStars(350, 110, 1);
  drawStars(280, 120, 1);
  drawStars(80, 140, 1);
  drawStars(20, 160, 1);
  drawStars(240, 180, 1);
  drawStars(370, 210, 1);
  drawStars(80, 240, 1);
  drawStars(300, 280, 1);
  drawStars(100, 310, 1);
  drawGround();
  drawHouse();
  drawBuilding();
  drawAsteroid(100, 60, 10, color(150));
  drawAsteroid(260, 90, 0, color(150));
  drawAsteroid(50, 140, 5, color(150));
  drawRobot();
  robotMovingHands();
}

//Changes the horizontal and vertical speed of the robot depending on the key that is pressed
void robotSpeed() {
  if (keyCode==UP) {
    robotSpeedY+=-.06;
  } else if (keyCode==RIGHT) {
    robotSpeedX+=.03;
  } else if (keyCode==LEFT) {
    robotSpeedX+=-.03;
  } else if (keyCode==DOWN) {
    robotSpeedY+=.04;
  }
}

//How Fast the Robot Falls From the Air when Descending
void robotFallSpeed() {
  robotSpeedY+=gravity;
}

//Change Robot Position or Make the Robot Move
void robotPosition() {
  robotX+=robotSpeedX;
  robotY+=robotSpeedY;
}

//Keep Robot on the Screen so that it can Always be Seen
void robotOnScreen() {
  if (robotX>width) {
    robotSpeedX=0;
    robotX=width;
  } else if (robotX<0) {
    robotSpeedX=0;
    robotX=0;
  }
  if (robotY>height) {
    robotSpeedY=0;
    robotSpeedX=0;
    robotY=height;
  } else if (robotY<0) {
    robotSpeedY=0;
    robotY=0;
  }
}

//Use Exhaust When Robot is Moving (Flames Grow Bigger than when Stationary)
void robotFuel() {
  if (keyPressed) {
    robotExhaustFlamesAmount=random(15, 30);
    robotExhaustFlamesSize=20;
    robotExhaustFlamesExtra=15;
  } else {
    robotExhaustFlamesAmount=random(3, 9);
    robotExhaustFlamesSize=12;
    robotExhaustFlamesExtra=5;
  }
}

//Draw Background (Colour the Background Black)
void drawBackground() {
  background(0);
}

//Draw Flashing Stars in the Background
void drawStars(int x, int y, int size) {
  float offset = size/1.5;
  stroke(random(50, 255));
  point(x - offset, y - offset);
}

//Draw the Ground
void drawGround() {
  stroke(0);
  fill(0, 0, 100);
  rect(0, 340, 400, 60);
}

//Draw a House in the Background
void drawHouse() {

  //Draw House Body
  noStroke();
  fill(50);
  rect(20, 300, 40, 40);

  //Draw House Door
  fill(0);
  rect(30, 330, 20, 10);

  //Draw House Roof
  fill(30);
  triangle(20, 300, 40, 280, 60, 300);

  //Draw House Window
  fill(random(200, 255), random(200, 255), random(0, 1));
  rect(30, 300, 20, 20);
  stroke(0);
  line(40, 300, 40, 320);
  line(30, 310, 50, 310);

  //Draw Window Light Reflection on Ground
  noStroke();
  fill(random(150, 200), random(150, 200), random(0, 1));
  ellipse(40, 370, 40, 20);
}

//Draw a Building in the Background
void drawBuilding() {

  //Draw Building Body
  noStroke();
  fill(60);
  rect(320, 240, 60, 100);

  // Draw Building Door
  fill(0);
  rect(340, 320, 20, 20);

  //Draw Building Windows
  fill(150, 150, 0);
  rect(330, 250, 20, 20);
  stroke(0);
  line(340, 250, 340, 270);
  line(330, 260, 350, 260);

  noStroke();
  fill(100, 100, 0);
  rect(330, 280, 20, 20);
  stroke(0);
  line(340, 280, 340, 300);
  line(330, 290, 350, 290);
}

//Create Asteroids in the Air
void drawAsteroid(int asteroidX, int asteroidY, int size, color c) {
  float offset = size/2;

  //Draw Asteroid Flames
  fill(255, 255, 0);
  triangle(asteroidX - offset - 10, asteroidY - offset, asteroidX - offset - random(20, 40), asteroidY - offset - random(20, 40), asteroidX - offset, asteroidY - 15);
  fill(255, 165, 0);
  triangle(asteroidX - offset - 5, asteroidY - offset, asteroidX - offset - random(10, 25), asteroidY - offset - random(10, 25), asteroidX - offset, asteroidY - 10);

  //Draw Asteroid
  noStroke();
  fill(c);
  ellipse(asteroidX, asteroidY, random(30, 40), random(30, 40));

  //Draw Asteroid Craters
  fill(0);
  ellipse(asteroidX - offset - random(1, 5), asteroidY - offset - random(3, 5), 10, 10);
  ellipse(asteroidX - offset + random(1, 5), asteroidY - offset + random(1, 2.5), 7, 5);
  ellipse(asteroidX - offset - random(4, 8), asteroidY - offset + random(7, 12), 8, 7);
}

//Create the Robot
void drawRobot() {

  //Draw Robot Body
  stroke(0);
  fill(230);
  rect(robotX, robotY, 20, 100);
  line(robotX, robotY + 90, robotX + 20, robotY + 90);
  line(robotX, robotY + 75, robotX + 20, robotY + 75);
  line(robotX, robotY + 60, robotX + 20, robotY + 60);
  fill(0);
  rect(robotX, robotY + 7, 20, 5);

  //Draw Robot Fuel Tank
  fill(230);
  rect(robotX - 40, robotY + 100, 100, 20);
  fill(150, 0, 0);
  rect(robotX - 40, robotY + 120, 100, 5);
  fill(0);
  ellipse(robotX - 30, robotY + 123, 18, 4);
  ellipse(robotX + 10, robotY + 123, 18, 4);
  ellipse(robotX + 50, robotY + 123, 18, 4);

  //Draw Robot Arms
  fill(230);
  rect(robotX - 20, robotY + 25, 60, 20);
  fill(150, 0, 0);
  rect(robotX - 40, robotY + 25, 20, 20);
  rect(robotX + 40, robotY + 25, 20, 20);

  //Draw Robot Light
  fill(random(150, 255), random(0, 1), random(0, 1));
  ellipse(robotX + 10, robotY - 15, 5, 5);


  //Draw Robot Head
  fill(230);
  rect(robotX - 20, robotY - 15, 60, 20);
  fill(0);
  rect(robotX - 18, robotY - 12, 55, 15);
  fill(230);
  rect(robotX - 25, robotY - 10, 5, 10);
  rect(robotX + 40, robotY - 10, 5, 10);

  //Draw Robot Eyes
  fill(255, 0, 0);
  ellipse(robotX - 5, robotY - 5, 12, 12);
  ellipse(robotX + 25, robotY - 5, 12, 12);

  //Draw Robot Exhaust Flames
  fill(255, 255, 0);
  triangle(robotX - robotExhaustFlamesSize/2 - 35, robotY + 123, robotX + robotExhaustFlamesSize/2 - 20, robotY + 123, robotX - 27.5, robotY + robotExhaustFlamesAmount + robotExhaustFlamesExtra/2 + 130);
  fill(255, 165, 0);
  triangle(robotX - robotExhaustFlamesSize/2 + robotExhaustFlamesSize/4 - 35, robotY + 123, robotX + robotExhaustFlamesSize/2 + robotExhaustFlamesSize/4 - 28, robotY + 123, robotX - 27.5, robotY + robotExhaustFlamesAmount/3 + robotExhaustFlamesExtra/2 + 130);
  fill(255, 255, 0);
  triangle(robotX - robotExhaustFlamesSize/2 + 5, robotY + 123, robotX + robotExhaustFlamesSize/2 + 15, robotY + 123, robotX + 10, robotY + robotExhaustFlamesAmount + robotExhaustFlamesExtra + 130);
  fill(255, 165, 0);
  triangle(robotX - robotExhaustFlamesSize/2 + robotExhaustFlamesSize/4 + 5, robotY + 123, robotX + robotExhaustFlamesSize/2 + robotExhaustFlamesSize/4 + 8, robotY + 123, robotX + 10, robotY + robotExhaustFlamesAmount/3 + robotExhaustFlamesExtra + 130);
  fill(255, 255, 0);
  triangle(robotX - robotExhaustFlamesSize/2 + 40, robotY + 123, robotX + robotExhaustFlamesSize/2 + 55, robotY + 123, robotX + 50, robotY + robotExhaustFlamesAmount + robotExhaustFlamesExtra/2 + 130);
  fill(255, 165, 0);
  triangle(robotX - robotExhaustFlamesSize/2 + robotExhaustFlamesSize/4 + 40, robotY + 123, robotX + robotExhaustFlamesSize/2 + robotExhaustFlamesSize/4 + 48, robotY + 123, robotX + 49, robotY + robotExhaustFlamesAmount/3 + robotExhaustFlamesExtra/2 + 130);
}

//Move Robot Hands Up as Robot Ascends or moves Horizontally
void robotMovingHands() {
  fill(150, 0, 0);
  if (keyCode==UP) {
    rect(robotX - 40, robotY + 5, 20, 20);
    rect(robotX + 40, robotY + 5, 20, 20);
  } else if (keyCode==RIGHT) {
    rect(robotX - 40, robotY + 5, 20, 20);
    rect(robotX + 40, robotY + 5, 20, 20);
  } else if (keyCode==LEFT) {
    rect(robotX - 40, robotY + 5, 20, 20);
    rect(robotX + 40, robotY + 5, 20, 20);
  } else {
    rect(robotX - 40, robotY + 45, 20, 20);
    rect(robotX + 40, robotY + 45, 20, 20);
  }
}