Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
Ball ball = new Ball(); // Creates an Consructor for the class ball
Ellipse[] ellipseSpawn = new Ellipse[10]; // Creates an array for the class ellipse and sets the size to 11
color randomColor = color(random(255), random(255), random(255)); // Creates a variable to create a random color 

public void setup() 
{  
  size(600, 600); //Creates the canvas and sets the size for the window 
}


public void draw() 
{
  background(randomColor, randomColor, randomColor); //Sets the background to become a random color 
  ellipseSpawns();

  for (int i=0; i< ellipseSpawn.length; i++) // if i is less then the size of the array , add one to i  
  {
    ellipseSpawn[i].drawEllipse(); // Spawn ellipse from array 
  }
  ball.draw(); // Calls Ball's draw class 
}


public void ellipseSpawns() 
{
  for (int i = 0; i < 10; i++)  //Loop 11 times 
  {
    ellipseSpawn[i] = new Ellipse(); // Spawns the ellipse 
  }
}
class Ball
{
  PVector ballP = new PVector (200, 300);
  float randomColor;
  float gravity = 1;
  float speed = 3;
  float v = 20;
  boolean up = false;
  boolean down; 


  void draw()
  { 
    ballMovement();
    drawBall();
    ballCol();
  }

  public void drawBall()
  { 
    ellipse(300, ballP.y, 100, 100); //Creates an ellipse 
    strokeWeight(3); // Sets stroke weight 
  }

  public void ballMovement()
  {    
    if (keyCode == UP) //If the user presses the up key
    { 
      keyCode = ' ';  //Sets the key input to blank/space
      speed = speed*1.3; // Math to create the ball to jump
      fill(random(255), random(255), random(255)); // Changes the color 
    }
  }

  void ballCol()
  {
    speed += gravity;  

    if (speed > v)
    {
      speed = v;   //Sets the players speed by limiting it to v 
    }   

    if (ballP.y >= 550) // If the ball hits the bottom of the screen 
    {
      speed += 1; // add one to speed 
      speed = -speed/1.1; // Let the ball bounce 
    }
    
    if (ballP.y <= 30) //If the ball hits the top of the screen 
    {
      speed = -speed*2; // blocks the ball from passing the top 
    }

    ballP.y += speed; // Applys the varibale speed to the balls position 
  }
}//End of class 
class Ellipse
{
  float randomNumber = random(0, 600) ;
  
  void draw()
  { 
    drawEllipse();
  }

  public void drawEllipse()
  { 
    ellipse(randomNumber, randomNumber, randomNumber, randomNumber); //Creates an random ellipse 
    strokeWeight(3); // Sets the stroke weight 
  }
}//end of class