Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
 Assignment #1 - INTERACTIVE DRAWING
 "Golf Course at Sunset"
 Created by Eric James
 September 19, 2016
 
 In this program the user moves the mouse, presses the mouse button and presses keyboard keys to interact with what is displayed onscreen.
 These interactions include moving objects such as clouds and a golf club, making the scene brighter or darker and making lightning strike.
 The "goal" of the program is to hit the golf ball with the club and get it in the hole.
 
 ***All shapes change colour (become brighter or darker) as mouseY changes except for the shapes which make up the lightning.***
 */

void setup() {

  // Initial Setup of shape parameters, stroke parameters, window size and framerate.
  ellipseMode(CENTER);
  rectMode(CORNERS);
  noStroke();
  size(400, 400);
  frameRate(60);
}

void draw() {

  //Setup of framerate so it resets after the mouse is clicked or a key is pressed.
  frameRate(60);

  // Setup of background which will change colours as mouseY is changed.
  background(mouseY/20 + 200, mouseY/20 + 130, mouseY/20 + 10);


  //Setup of golf course grass.


  //Setup of the hill on the right.
  fill(0 + mouseY/20, 160 + mouseY/20, 20 + mouseY/20);
  ellipse(300, 300, 300, 180);

  //Setup of the hill on the left.
  fill(0 + mouseY/20, 170 + mouseY/20, 20 + mouseY/20);
  ellipse(100, 300, 300, 160);

  //Setup of the flat part of the golf course in the foreground.
  fill(0 + mouseY/20, 180 + mouseY/20, 20 + mouseY/20);
  rect(0, 280, 400, 400);
  ellipse(200, 280, 420, 30);


  //Setup of golf balls. There is a static golf ball and a moving golf ball. The static golf ball is covered by moving grass once it is overlapped with the moving golf ball.
  //This gives the illusion that the golf club hit the static ball and it moved.


  //Setup of static golf ball
  fill(200 + mouseY/20);
  ellipse(140, 380, 10, 10);

  //Setup of grass which will cover up static golf ball.
  fill(0 + mouseY/20, 180 + mouseY/20, 20 + mouseY/20);
  rect(0, 280, 15 + mouseX, 400);

  //Setup of moving golf ball.
  fill(200 + mouseY/20);
  ellipse(20 + mouseX, 380, 10, 10);

  //Setup of grass which will cover up moving golf ball.
  fill(0 + mouseY/20, 180 + mouseY/20, 20 + mouseY/20);
  rect(0, 280, 135, 400);

  //Setup of grass which will cover up moving golf ball once it reaches the golf ball hole.
  fill(0 + mouseY/20, 180 + mouseY/20, 20 + mouseY/20);
  rect(350, 280, 400, 400);


  //Setup of golf ball hole.


  //Setup of flag.
  fill(200 + mouseY/20, 0 + mouseY/20, 0 + mouseY/20);
  triangle(351, 710 - mouseX, 370, 720 - mouseX, 351, 730 - mouseX);

  //Setup of patch of grass which covers area under golf ball hole so flag will not be seen as it rises from under the visible scene.
  fill(0 + mouseY/20, 180 + mouseY/20, 20 + mouseY/20);
  rect(350, 380, 400, 400);

  //Setup of hole
  fill(40 + mouseY/20);
  ellipse(350, 380, 25, 15);

  //Setup of pole.
  fill(180 + mouseY/20);
  rect(349, 380, 351, 310);


  //Setup of the sun.


  //Setup of vertically moving sun 
  fill(175 + mouseY/10, 229 + mouseY/10, 16 + mouseY/10);
  ellipse(360, -mouseY/4 + 150, 80, 80);


  //Setup of clouds. There are five different clouds, each of which are different shapes and move at different speeds.


  // Setup of horizontally moving cloud #1
  fill(160 - -mouseY/10);
  ellipse(mouseX/7 + 100, 10, 70, 30);
  ellipse(mouseX/7 + 80, 30, 70, 30);
  ellipse(mouseX/7 + 105, 40, 70, 30);
  ellipse(mouseX/7 + 130, 35, 70, 30);
  ellipse(mouseX/7 + 135, 15, 70, 30);

  // Setup of horizontally moving cloud #2
  fill(160 - -mouseY/10);
  ellipse(mouseX/6 + 230, 30, 70, 30);
  ellipse(mouseX/6 + 210, 45, 70, 30);
  ellipse(mouseX/6 + 245, 50, 70, 30);
  ellipse(mouseX/6 + 270, 55, 70, 30);
  ellipse(mouseX/6 + 275, 30, 70, 30);

  // Setup of horizontally moving cloud #3
  fill(160 - -mouseY/10);
  ellipse(mouseX/8 + 240, 90, 70, 30);
  ellipse(mouseX/8 + 190, 105, 70, 30);
  ellipse(mouseX/8 + 215, 120, 70, 30);
  ellipse(mouseX/8 + 250, 115, 70, 30);
  ellipse(mouseX/8 + 255, 105, 70, 30);

  // Setup of horizontally moving cloud #4
  fill(160 - -mouseY/10);
  ellipse(mouseX/6 + 90, 90, 70, 30);
  ellipse(mouseX/6 + 60, 105, 70, 30);
  ellipse(mouseX/6 + 55, 120, 70, 30);
  ellipse(mouseX/6 + 80, 115, 70, 30);
  ellipse(mouseX/6 + 85, 105, 70, 30);

  // Setup of horizontally moving cloud #5
  fill(160 - -mouseY/10);
  ellipse(mouseX/8.5 + -40, 65, 70, 30);
  ellipse(mouseX/8.5 + -70, 80, 70, 30);
  ellipse(mouseX/8.5 + -55, 95, 70, 30);
  ellipse(mouseX/8.5 + -30, 90, 70, 30);
  ellipse(mouseX/8.5 + -25, 80, 70, 30);


  //Setup of the golf club.


  //Setup of golf club handle.
  fill(60 + mouseY/20);
  rect(80 +mouseX/4, 250, 90 +mouseX/4, 290);

  //Setup of golf club shaft.
  fill(150 + mouseY/20);
  rect(82 +mouseX/4, 290, 88 +mouseX/4, 370);

  //setup of golf club head.
  fill(150 + mouseY/20);
  quad(82 +mouseX/4, 365, 110 +mouseX/4, 375, 105 +mouseX/4, 385, 84 +mouseX/4, 380);
}


//Setup of lightning strikes.


//Setup of lightning bolt #1 which activates when the mouse is pressed. Background is set to black and framerate is lowered to 10 so the lightning appears onscreen for longer.
void mousePressed() {
  frameRate(10);
  background(0);

  //Change fill to yellow so lightning is yellow.
  fill(200, 200, 0);

  //Setup of the actual shapes which make up the lightning bolt. These shapes move with the cloud the lightning bolt comes from so the lightning will always strike from the clouds.
  quad(mouseX/6 + 55, 120, mouseX/6 + 80, 120, mouseX/6 + 105, 160, mouseX/6 + 80, 160);
  quad(mouseX/6 + 55, 200, mouseX/6 + 80, 200, mouseX/6 + 105, 160, mouseX/6 + 80, 160);
  quad(mouseX/6 + 55, 200, mouseX/6 + 80, 200, mouseX/6 + 105, 240, mouseX/6 + 80, 240);
  quad(mouseX/6 + 55, 280, mouseX/6 + 80, 280, mouseX/6 + 105, 240, mouseX/6 + 80, 240);

  //Prints "KABOOM!!" when lightning strikes.
  println("KABOOM!!");
}

//Setup of lightning bolt #2 which activates when a key is pressed. Background is set to black and framerate is lowered to 10 so the lightning appears onscreen for longer.
void keyPressed() {
  frameRate(10);
  background(0);

  //Change fill to yellow so lightning is yellow.
  fill(200, 200, 0);

  //Setup of the actual shapes which make up the lightning bolt. These shapes move with the cloud the lightning bolt comes from so the lightning will always strike from the clouds.
  quad(mouseX/8 + 215, 120, mouseX/8 + 240, 120, mouseX/8 + 265, 160, mouseX/8 + 240, 160);
  quad(mouseX/8 + 215, 200, mouseX/8 + 240, 200, mouseX/8 + 265, 160, mouseX/8 + 240, 160);
  quad(mouseX/8 + 215, 200, mouseX/8 + 240, 200, mouseX/8 + 265, 240, mouseX/8 + 240, 240);
  quad(mouseX/8 + 215, 280, mouseX/8 + 240, 280, mouseX/8 + 265, 240, mouseX/8 + 240, 240);

  //Prints "ZAP!!" when lightning strikes.
  println("ZAP!!");
}

//Coding End :)