Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
float ballPositionX, ballPositionY;

float monsteronePositionX, monsteronePositionY;
float monstertwoPositionX, monstertwoPositionY;
float monsterthreePositionX, monsterthreePositionY;
float monsterfourPositionX, monsterfourPositionY;

float energyonePositionX, energyonePositionY;
float energytwoPositionX, energytwoPositionY;
float energythreePositionX, energythreePositionY;
float energyfourPositionX, energyfourPositionY;
float energyfivePositionX, energyfivePositionY;

float ballYSpeed, ballXSpeed;
float gravity=.01;

boolean isBallTouchCoinOne=false;
boolean isBallTouchCoinTwo=false;
boolean isBallTouchCoinThree=false;
boolean isBallTouchCoinFour=false;
boolean isBallTouchCoinFive=false;
boolean isBallTouchMonsterOne=false;
boolean isBallTouchMonsterTwo=false;
boolean isBallTouchMonsterThree=false;
boolean isBallTouchMonsterFour=false;

float score=0;
float scoreCount=1/15;

void setup(){
  size(400,400);
  frameRate(60);
  smooth();
  rectMode(CENTER);
  ellipseMode(CENTER);
  
  initializeBall();
}


void initializeBall(){
  ballPositionX=width/2;
  ballPositionY=300;
  
  monsteronePositionX=width/2;
  monsteronePositionY=170;
  
  monstertwoPositionX=60;
  monstertwoPositionY=80;
  
  monsterthreePositionX=340;
  monsterthreePositionY=265;
  
  monsterfourPositionX=60;
  monsterfourPositionY=305;
  
  energyonePositionX=100;
  energyonePositionY=270;
  
  energytwoPositionX=300;
  energytwoPositionY=230;
  
  energythreePositionX=160;
  energythreePositionY=150;
  
  energyfourPositionX=280;
  energyfourPositionY=50;
  
  energyfivePositionX=100;
  energyfivePositionY=60;
  
  ballXSpeed=0;
  ballYSpeed=0;
}

int y=0;
int rectY = -20;
void draw(){
  
//shining background
  noStroke();
  fill(random(150,255),random(150,255),random(150,255));
  rect(width/2,y,width,20);
  
  y+=20;
  rectY +=20;
  
  if (rectY ==800){
   rectY=0;
  }
  
  if (y ==400){
   y=0;
  }
  noStroke();
  fill(147,127,80);
   rect(width/2, rectY-400, width,30);
  
  //user-defined functions
  constrainBallToCanvasBounds();
  ballPosition();
  constrainBallToIslandBounds();
  ballTouch();

  
  //draw functions
  drawIsland();
  drawBall();
  drawMonster();
  drawEnergy();
  drawLand();
}

void constrainBallToCanvasBounds(){
  
  //set the boundaries of the canvas
  if(ballPositionY+10>height){
    ballYSpeed=0;
    ballXSpeed=0;
    ballPositionY=height-10;
  }
  else if(ballPositionY<0){
    ballYSpeed=0;
    ballXSpeed=0;
  }
  
  if(ballPositionX>width){
    ballXSpeed=0;
    ballPositionX=width;
  }
  else if(ballPositionX<0){
    ballXSpeed=0;
    ballPositionX=0;
  }
}

void constrainBallToIslandBounds(){
  
  //set the boundaries of the Islands
  if(ballPositionY-15<360 && ballPositionY+15>320 && ballPositionX-15<160 ){

    ballYSpeed=0;
    ballXSpeed=0;
    
    if(ballPositionY-15<360 && ballPositionX-15<160){
      gravity=.01;
    }
    else{
      gravity=0;
    }
     ballYSpeed+=gravity;
     
  }
  
  
  else if(ballPositionY-15<320 && ballPositionY+15>280 && ballPositionX-15<360 && ballPositionX+15>260){
    ballYSpeed=0;
    ballXSpeed=0;
    
    //println("hit");
    if(ballPositionY-15<320 && ballPositionX-15<360 && ballPositionX+15>260){
     // println("hit bottom");
      gravity=.01;
    }
    else{
     gravity=0; 
    }
    ballYSpeed+=gravity;
    
  }
  
  
  else if(ballPositionY-15<220 && ballPositionY+15>180 && ballPositionX-15<260 && ballPositionX+15>140){

    ballYSpeed=0;
    ballXSpeed=0;
    
    if(ballPositionY-15<220 && ballPositionX-15<260 && ballPositionX+15>140){
      gravity=.01;
    }
    else{
      gravity=0;
    }
      ballYSpeed+=gravity;
      
  }
  
  
  else if(ballPositionY-15<130 && ballPositionY+15>90 && ballPositionX-15<120 && ballPositionX+15>20){

    ballYSpeed=0;
    ballXSpeed=0;
    
    if(ballPositionY-15<130 && ballPositionX-15<120 && ballPositionX+15>20){
      gravity=.01;
    }
    else{
      gravity=0;
    }
   ballYSpeed+=gravity;
   
  }
  else if(ballPositionY-15<100 && ballPositionY+15>80 && ballPositionX-15<320 && ballPositionX+15>220){

    ballYSpeed=0;
    ballXSpeed=0;
    
    if(ballPositionY-15<100 && ballPositionX-15<320 && ballPositionX+15>220){
      gravity=.01;
    }
    else{
      gravity=0;
    }
    ballYSpeed+=gravity;

  }
  
  else{
    gravity = 0.01;
     ballYSpeed+=gravity;
  }
  
}

void ballTouch(){
  
  //set the situation when ball touches coins or monsters
  if(ballPositionX>92.5 && ballPositionX<107.50 && ballPositionY>262.5 && ballPositionY<277.5 ){
    isBallTouchCoinOne=true;
    score++;
  }
  else if(ballPositionX>292.5 && ballPositionX<307.5 && ballPositionY>223.5 && ballPositionY<237.5){
    isBallTouchCoinTwo=true;
    score++;
  }
  else if(ballPositionX>152.5 && ballPositionX<167.5 && ballPositionY>142.5 && ballPositionY<157.5){
    isBallTouchCoinThree=true;
    score++;
  }
  else if(ballPositionX>272.5 && ballPositionX<287.5 && ballPositionY>42.5 && ballPositionY<57.5){
    isBallTouchCoinFour=true;
    score++;
  }
  else if(ballPositionX>92.5 && ballPositionX<107.5 && ballPositionY>52.5 && ballPositionY<67.5){
    isBallTouchCoinFive=true;
    score++;
  }
  else if(ballPositionX>190 && ballPositionX<210 && ballPositionY>160 && ballPositionY<180){
    isBallTouchMonsterOne=true;
    score--;
  }
  else if(ballPositionX>50 && ballPositionX<70 && ballPositionY>70 && ballPositionY<90){
    isBallTouchMonsterTwo=true;
    score--;
  }
  else if(ballPositionX>325 && ballPositionX<355 && ballPositionY>250 && ballPositionY<280){
    isBallTouchMonsterThree=true;
    score--;
  }
  else if(ballPositionX>45 && ballPositionX<75 && ballPositionY>290 && ballPositionY<320){
    isBallTouchMonsterFour=true;
    score--;
  }
  
  println(score);
}


void ballPosition(){

  //set the controls for ball movement
  if(mousePressed){
    ballYSpeed+=-.08;
    if(mouseX>ballPositionX){
      ballXSpeed+=.02;
    }
    else if(mouseX<ballPositionX){
      ballXSpeed+=-.02;
    }
  }
  
  ballPositionX+=ballXSpeed;
  ballPositionY+=ballYSpeed;
  
  ballPositionY=constrain(ballPositionY,0,380);
}


//draw the canvas
void drawBall(){
  noStroke();
  fill(245,163,10);
  ellipse(ballPositionX,ballPositionY,30,30);
}

void drawIsland(){
  fill(179,136,185);
  rect(80,340,160,40);
  rect(310,300,100,40);
  rect(200,200,120,40);
  rect(70,110,100,40);
  rect(270,90,100,20);
}

void drawMonster(){
  fill(207,114,129);
  if(isBallTouchMonsterOne == false){
  rect(monsteronePositionX,monsteronePositionY,20,20); 
  }
  if(isBallTouchMonsterTwo == false){
  rect(monstertwoPositionX,monstertwoPositionY,20,20);
  }
  if(isBallTouchMonsterThree == false){
  rect(monsterthreePositionX,monsterthreePositionY,30,30);
  }
  if(isBallTouchMonsterFour == false){
  rect(monsterfourPositionX,monsterfourPositionY,30,30);
  }
}

void drawEnergy(){
  fill(random(100,255));
  if(isBallTouchCoinOne == false){
  ellipse(energyonePositionX,energyonePositionY,15,15);
  }
  if(isBallTouchCoinTwo == false){
  ellipse(energytwoPositionX,energytwoPositionY,15,15);
  }
  if(isBallTouchCoinThree == false){
  ellipse(energythreePositionX,energythreePositionY,15,15);
  }
  if(isBallTouchCoinFour == false){
  ellipse(energyfourPositionX,energyfourPositionY,15,15);
  }
  if(isBallTouchCoinFive == false){
  ellipse(energyfivePositionX,energyfivePositionY,15,15);
  }
}

void drawLand(){
  fill(179,136,185);
  rect(200,400,400,16);
}