//Declare the following variables to record the current location of the ball float x_ball; float y_ball; //Declare the following variables to record the velocity of the ball float x_vel; float y_vel; //****************************************************************************** //Declare the following variables to record the current location of the player float x_Player; float y_Player; //Declare the following variables to record the location of the goal area float x_goal; float y_goal; //Declare the following variables to record the score; int scores; //****************************************************************************** void setup() { //Set the canvas size size(800, 800); //Set the background color of the canvas background(192, 64, 0); //Set the stroke color as white stroke(255, 255, 255); x_ball = 20; // Center of screen y_ball = 20; x_vel = 8; y_vel = 2; x_Player = 100; y_Player = 100; x_goal = 400; y_goal = 400; scores = 0; //Set up the text font PFont f; f = createFont("Arial",16,true); textFont(f,32); } void draw() { //************************************************************************** //If no mouse botton is presed, then clear the canvas to leave no trace. //Otherwise, keep earlier drawing operations on to leave a trace. //************************************************************************** if (mousePressed == false) background(192, 64, 0); //************************************************************************** //Draw a ball in new position centered at (x_ball, y_ball). //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** fill(255, 0, 255); ellipse(x_ball, y_ball, 20, 20); // Update to the new position x_ball += x_vel; y_ball += y_vel; // Update the speed to show gravity y_vel += 0.1; // Bounce off left & right wall if ((x_ball<20) || (x_ball> 800 -20)) { x_vel = x_vel* -1; } // Bounce off floor & ceiling if ((y_ball<20) || (y_ball> 800 -20)) { y_vel = y_vel * -1; } //************************************************************************** // Move the player up, down, left, right using the the keys i, m, j, k //************************************************************************** if (keyPressed == true && key == 'i') { y_Player -= 2; } if (keyPressed == true && key == 'm') { y_Player += 2; } if (keyPressed == true && key == 'j') { x_Player -= 2; } if (keyPressed == true && key == 'k') { x_Player += 2; } //************************************************************************** // Draw the player as a short vertical wall of width 10 and height 100 //************************************************************************** fill(0, 255, 0); rect(x_Player-5, y_Player-50, 10, 100); //************************************************************ // Bounce off the player as a wall if the ball is very close //************************************************************ if (abs(x_ball - x_Player) < 10 && abs(y_ball - y_Player) < 50) { x_vel = x_vel* -1; } //************************************************************************** // Draw the the goal area around the center at (400, 400) //************************************************************************** fill(0, 0, 255); rect(x_goal-25, y_goal-25, 50, 50); //************************************************************ // Increase the score if the ball hit the goal. //************************************************************ if (abs(x_ball - x_goal) < 50 && abs(y_ball - y_goal) < 50) { scores = scores + 1; } //************************************************************************** // Draw the the scoring area around the center at (400, 400) //************************************************************************** text(scores, 450, 400) ; }