Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
boolean rocketAlive = true;
int fireX = 210;
int fireY = 390;
boolean mouseLeft = false;
boolean mouseRight = false;
boolean mouseMiddle = false;
boolean fireworkAlive = true;
boolean explosionAlive = false;
int moonx = 130;
int r = 50;
int g = 50;
int b = 80;
int hillAndGrassColour = 50;
int characterColourR = 100;
int characterColourG = 100;
int characterColourB = 100;
int charSkinR = 120;
int charSkinG = 80;
int charSkinB = 80;

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

void draw()
{
  background(r, g, b);
  r = constrain(r, 20, 50);
  g = constrain(g, 20, 50);
  b = constrain(b, 40, 100);

  if (mouseX < width/2) //Changes Background Colour With Mouse Movement
  {
    r = r - 1;
    g = g - 1;
    b = b - 1;
  } 
  
  else
  {
    r = r + 1;
    g = g + 1;
    b = b + 1;
  }

  stars();
  moon();
  explosion();
  hillsAndGrass();
  characterBody();
  eyes();
  firework();
  rocket();
}

void characterBody() //Draw Character Head, Body, Legs and Feet
{
  stroke(0);
  strokeWeight(2);
  fill(0, characterColourG, 0);
  rect(180, 320, 60, 80);
  fill(charSkinR, charSkinG, charSkinB);
  ellipse(210, 270, 110, 110);
  fill(0, 0, characterColourB);
  rect(140, 380, 40, 20);
  rect(240, 380, 40, 20);
  fill(characterColourR, 0, 0);
  quad(150, 360, 140, 360, 140, 380, 160, 380);
  quad(260, 380, 270, 360, 280, 360, 280, 380);

  characterColourR = constrain(characterColourR, 80, 130);
  characterColourG = constrain(characterColourG, 80, 130);
  characterColourB = constrain(characterColourB, 80, 130);
  charSkinR = constrain(charSkinR, 90, 140);
  charSkinG = constrain(charSkinG, 50, 100);
  charSkinB = constrain(charSkinG, 50, 100);

  if (mouseX < width/2) //Changes Characters Colour Palette With Mouse Movement
  {
    characterColourR = characterColourR - 1;
    characterColourG = characterColourG - 1;
    characterColourB = characterColourB - 1;
    charSkinR = charSkinR - 1;
    charSkinG = charSkinG - 1;
    charSkinB = charSkinB - 1;
  } 
  
  else
  {
    characterColourR = characterColourR + 1;
    characterColourG = characterColourG + 1;
    characterColourB = characterColourB + 1;
    charSkinR = charSkinR + 1;
    charSkinG = charSkinG + 1;
    charSkinB = charSkinB + 1;
  }
}

void rocket() //Draw Character Rocket With Arms
{
  rotate();

  if (rocketAlive == true)
  {
    strokeWeight(2);
    stroke(0);
    fill(0, 0, 255);
    rect(200, 340, 20, 60);
    fill(255, 200, 200);
    ellipse(200, 370, 20, 20);
    ellipse(220, 370, 20, 20);
  }

  if (mouseX < width/3) //If Mouse On Left Side, Rocket Disappears
  {
    rocketAlive = false;
  } 
  
  else if (mouseX <= 2*width/3) //If Mouse In Middle, Rocket Stays Intact
  {
    rocketAlive = true;
  } 
  
  else if (mouseX > 2*width/3) //If Mouse On Right Side, Rocket Disappears
  {
    rocketAlive = false;
  }
}

void eyes() //Draw Character Eyes That Follow Mouse
{
  int pupilXMove1 = constrain(mouseX, 185, 195);
  int pupilYMove1 = constrain(mouseY, 245, 255);
  int pupilXMove2 = constrain(mouseX, 225, 235);
  int pupilYMove2 = constrain(mouseY, 245, 255);

  strokeWeight(2);
  fill(255);
  ellipse(190, 250, 20, 20);
  ellipse(230, 250, 20, 20);
  fill(0);
  ellipse(pupilXMove1, pupilYMove1, 5, 5);
  ellipse(pupilXMove2, pupilYMove2, 5, 5);
}

void rotate() //Rotates Rocket Left Or Right
{
  if (mouseX < width/3)
  {
    strokeWeight(2);
    stroke(0);
    fill(0, 0, 255);
    quad(182, 345, 200, 337, 225, 393, 207, 400);
    fill(255, 200, 200);
    ellipse(195, 373, 20, 20);
    ellipse(213, 365, 20, 20);
  }
  
  if (mouseX > 2*width/3)
  {
    strokeWeight(2);
    stroke(0);
    fill(0, 0, 255);
    quad(220, 337, 237, 345, 208, 400, 190, 393);
    fill(255, 200, 200);
    ellipse(205, 365, 20, 20);
    ellipse(222, 373, 20, 20);
  }
}

void firework() //Firework Shoots Diagonal Left, Right, or Center
{
  if (fireworkAlive == true)
  {
    if (mousePressed && mouseX < width/3 && mouseRight == false && mouseMiddle == false || mouseLeft == true)
    { //If Mouse On Left Side, Shoots Firework Left Only
      fireX = fireX - 1;
      fireY = fireY - 3;
      strokeWeight(10);
      stroke(150, 0, 0);
      point(fireX, fireY);
      mouseLeft = true;
    } 
    
    if (mousePressed && mouseX > 2*width/3 && mouseLeft == false && mouseMiddle == false || mouseRight == true)
    { //If Mouse On Right Side, Shoots Firework Right Only
      fireX = fireX + 1;
      fireY = fireY - 3;
      strokeWeight(10);
      stroke(150, 0, 0);
      point(fireX, fireY);
      mouseRight = true;
    }
    
    if (mousePressed && mouseX >  width/3 && mouseX < 2*width/3 && mouseLeft == false && mouseRight == false || mouseMiddle == true)
    { //If Mouse Is In Centre, Shoots Firework Center Only
      fireY = fireY - 3;
      strokeWeight(10);
      stroke(150, 0, 0);
      point(fireX, fireY);
      mouseMiddle = true;
    }
  }

  if (fireY <= 100) //If Firework Reaches A Certain Point, Explosion Replaces It
  {
    fireworkAlive = false;
    explosionAlive = true;
  }

  if (fireworkAlive == false && mousePressed && mouseX < width/3) //Allows To Shoot Another Firework Left
  {
    fireworkAlive = true;
    fireX = 210;
    fireX = fireX - 1;
    fireY = 390;
    fireY = fireY - 3;
    mouseLeft = true;
    mouseRight = false;
    mouseMiddle = false;
  } 
  
  if (fireworkAlive == false && mousePressed && mouseX > 2*width/3) //Allows To Shoot Another Firework Right
  {
    fireworkAlive = true;
    fireX = 210;
    fireX = fireX + 1;
    fireY = 390;
    fireY = fireY - 3;
    mouseRight = true;
    mouseLeft = false;
    mouseMiddle = false;
  }
  
  if (fireworkAlive == false && mousePressed && mouseX >  width/3 && mouseX < 2*width/3) //Allows To Shoot Another Firework Center
  {
    fireworkAlive = true;
    fireX = 210;
    fireY = 390;
    fireY = fireY - 3;
    mouseRight = false;
    mouseLeft = false;
    mouseMiddle = true;
  }
}

void explosion() //Firework Explosion
{
  if (explosionAlive == true) //Randomly Changes Size In Place
  {
    fill((int)random(0, 255), (int)random(0, 255), (int)random(0, 255));
    stroke((int)random(0, 255), (int)random(0, 255), (int)random(0, 255));
    ellipse(fireX, fireY, (int) random(100, 120), (int)random(100, 120));
  }
  
  if (mouseButton == LEFT) //Destroys Previous Explosion To Shoot Another Firework
  {
    explosionAlive = false;
  }
}

void stars() //Stars Randomly Flash In Different Positions
{
  strokeWeight(5);
  stroke((int)random(0, 255), (int)random(0, 255), 255);
  point((int)random(5, 399), (int)random(5, 399));
}

void moon() //Moon Changes Colour Depending On Mouse Location
{
  fill(moonx);
  stroke(0);
  strokeWeight(2);
  ellipse(40, 0, 100, 100);
  fill(50);
  ellipse(20, 0, 20, 20);
  ellipse(40, 30, 20, 10);
  ellipse(75, 5, 10, 20);
  moonx = constrain(moonx, 80, 160);

  if (mouseX < width/2)
  {
    moonx = moonx - 1;
  } 
  
  else
  {
    moonx = moonx + 1;
  }
}

void hillsAndGrass() //Grass And Hills Looped In Background
{ //Stroke Switches To Match explosion() Stroke

  for (int i = 0; i < 500; i += 100)
  {
    fill(100, hillAndGrassColour, hillAndGrassColour);
    hillAndGrassColour = constrain(hillAndGrassColour, 20, 50);
    ellipse(i, 400, 110, 200);
  }
  
  for (int x = 0; x < 500; x += 20)
  {
    fill(hillAndGrassColour, 100, hillAndGrassColour);
    hillAndGrassColour = constrain(hillAndGrassColour, 20, 50);
    triangle(0 + x, 400, 10 + x, 380, 20 + x, 400);
  }

  if (mouseX < width/2) //Changes Hill And Grass Colour With Mouse Movement
  {
    hillAndGrassColour = hillAndGrassColour - 1;
  } 
  
  else
  {
    hillAndGrassColour = hillAndGrassColour + 1;
  }
}