Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////AVOID THE SUN - BY YANI W.///////////////////////////
///////Avoid the sun particles!! Use "A" and "D" to move from side to side!!//////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////


float snowManX, snowManY;
int halfHeight;
float sunX;
float sunY;
float ray1X, ray1Y;
float ray2X, ray2Y;
float ray3X, ray3Y;
boolean left, right;
boolean gameOver;
float speed1, speed2, speed3;

void setup () {
  size (600, 800);
  //announce functions
  setupSnowMan();
  setupSun();
  setupRay1();
  setupRay2();
  setupRay3();
}

void draw () {
  background (0, 153, 255); //blue background

  //draw mountains in the background

  noStroke(); //draw snowy land
  fill (255);
  rectMode (CENTER);
  rect (300, 800, 600, 100);

  if (gameOver==false) {
    for (int i = 0; i < 800; i += 40) {
      drawMountains(i);
    }
    snowManMove();
    drawSnowMan();
    drawSun();
    drawRay1();
    drawRay2();
    drawRay3();
  } else {
    background (0);
    textAlign (CENTER, CENTER);
    textSize (50);
    text("Game Over! Try again..", width/2, height/2);
  }
}
void drawMountains(float x) {
  if (gameOver == false) {
    ellipseMode(CENTER);
    fill(255);

    noStroke();
    ellipse(x, height - 50, 50, 50);
  }
}

//SETUP ALL THE FUNCTIONS
void setupSnowMan () {
  snowManX=300;
  snowManY=620;
  left = false;
  right = false;
  halfHeight = 80;
}

void setupSun () {
  sunX=300;
  sunY=0;
}

void setupRay1 () {
  ray1X = random(50, width-50);
  ray1Y = 0;
  speed1 = random(1, 5);
}

void setupRay2 () {
  ray2X = random(50, width-50);
  ray2Y = 0;
  speed2 = random(1, 5);
}

void setupRay3 () {
  ray3X = random(50, width-50);
  ray3Y = 0;
  speed3 = random(1, 5);
}

//DRAW ALL THE FUNCTIONS
void drawSnowMan () {
  if (snowManY - ray1Y <= 60&& snowManY - ray1Y <=60 && snowManX - ray1X >= -35 && snowManX - ray1X <= 35) {
    gameOver=true;
  }
  if (snowManY - ray2Y <= 60 && snowManY - ray1Y <=60 && snowManX - ray2X >= -35 && snowManX - ray2X <= 35) {
    gameOver=true;
  }
  if (snowManY - ray3Y <= 60 && snowManY - ray1Y <=60 && snowManX - ray3X >= -35 && snowManX - ray3X <= 35) {
    gameOver=true;
  }

  //snowman body
  ellipse (snowManX, snowManY+80, 100, 100);
  ellipse (snowManX, snowManY, 70, 70);
  ellipse (snowManX, snowManY-60, 50, 50);
  //snowman eyes
  noStroke ();
  fill (0);
  ellipse (snowManX-10, snowManY-65, 10, 10);
  ellipse (snowManX+10, snowManY-65, 10, 10);
  //carrot nose
  noStroke ();
  fill (255, 153, 51);
  triangle (snowManX, snowManY-60, snowManX-35, snowManY-50, snowManX, snowManY-50);
}

void drawSun () {
  //sun 
  noStroke ();
  fill (255, 204, 0);
  ellipse (sunX, sunY, 200, 200);
}
//sun particles 
void drawRay1 () {
  rect (ray1X, ray1Y, 20, 20);
  ray1Y += speed1;
  if (ray1Y > height+25) {
    setupRay1();
  }
}

void drawRay2 () {
  rect (ray2X, ray2Y, 20, 20);
  ray2Y+=speed2;
  if (ray2Y > height+25) {
    setupRay2();
  }
}

void drawRay3 () {
  rect (ray3X, ray3Y, 20, 20);
  ray3Y +=speed3;
  if (ray3Y > height+25) {
    setupRay3 ();
  }
}
void snowManMove() {
  if (right == true) {
    snowManX+=5;
  } else if (left == true) {
    snowManX-=5;
  }
}
void keyPressed () {
  if (key == 'd'|| key =='D') {
    right = true;
  }
  if (key == 'a' || key =='A') {
    left = true;
  }
}
void keyReleased() {
  if (key == 'd' || key == 'D') {
    right = false;
  }
  if (key == 'a' || key == 'A') {
    left = false;
  }
}