Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
Adam Wilczewski
 Wall Tennis
 October 2nd 2017
 
 17 user defined variables
 6 user designed functions
 
 
 Play Tennis with the wall by bouncing a ball against a paddle, each time the paddle hits the ball 
 the ball gets smaller and faster. If the ball misses your paddle and hits the left side of the screen the
 game prints your score and restarts.
 */

//paddle variables
float paddleX = 10;
float paddleY = mouseY;
float paddleWidth= 15;
float paddleHeight = 120;
//ball variables
float ballX = 200;
float ballY = 200;
float ballRadius = 32;
float ballXspeed= 5;
float ballYspeed = 2.3;
int score = 0;


void setup() {
  size(400, 400);
  background(0);
  //reset original ball location, speed, and score
  ballX = 200;
  ballY= 200;
  ballRadius = 32;
  ballXspeed = 5;
  score = 0;
}

void draw() {

  drawBackground();
  drawPaddle();
  // (Colour 1, Colour 2, Colour 3, Alpha)
  drawBall(0, 0, random(150, 255), random(150, 255));
  constrainPaddle();
  moveBall();
  bounchBall();
}

//Functions//

//draw background
void drawBackground() {
  background(0);
  //get distance from the middle of the screen to the ball
  float distColour = dist(width/2, height/2, ballX, ballY);
  fill(distColour, 150, 0);
  // draw rectangles in a loop for the background
  for (int y = 0; y<height; y = y+25) {
    for (int x = 0; x < width; x = x+ 25) {     
      rect(x, y, 30, 30);
    }
  }
}







//Draw Paddle
void drawPaddle() {
  //draw white paddle
  fill(255);
  rect( paddleX, paddleY, paddleWidth, paddleHeight);
}

//Draw Ball
void drawBall(float c1, float c2, float c3, float alpha) {
  //draw white ball (var ballX, ballY, and ballRadius)
  stroke(0);
  fill(c1, c2, c3, alpha);
  ellipseMode(RADIUS);
  ellipse(ballX, ballY, ballRadius, ballRadius);
} 

//Constrain Paddle
void constrainPaddle() {
  //if the mouse Y is less than 60 make paddle y = 0 
  if (mouseY < 60) {
    paddleY = 0;
    //if the mouse Y is greater than 340 make paddle y = 280
  } else if (mouseY > height - 60) {
    paddleY = height- paddleHeight;
    // if not the other conditions paddle follows Mouse y with an offset of 60
  } else {
    paddleY = mouseY - 60;
  }
}


//Move Ball
void moveBall() {

  ballX = ballX+=ballXspeed;
  ballY= ballY+=ballYspeed;
}


//Bounch Ball
void bounchBall() {
  // if ball touches right side of screen bounce back
  if (ballX> width) {
    ballXspeed = ballXspeed * -1;
  }
  //if ball touches left side of screen reset game
  if (ballX + ballRadius <0) {
    ballXspeed = ballXspeed * -1;
    println("You bounced the ball " +score+" times");
    setup();
  }
  //if ball touches either top or bottom of the screen bounch back down
  if (ballY> 400|| ballY <0) {
    ballYspeed = ballYspeed * -1;
  }
  //if ball touches paddle bouches off and adds one to the score 
  if (ballX  < paddleX + paddleWidth && ballX  > paddleX && ballY < paddleY + paddleHeight && ballY > paddleY) {
    ballXspeed = ballXspeed * -1;
    score = score += 1;

    // if the balls radius isgreater than 10 make the balls radius smaller and make the ball faster 
    if (ballRadius > 10) {
      ballRadius = ballRadius -= 1;
      ballXspeed = ballXspeed += 0.5;
    }
  }
}