/** Declare PADDLE_CONSTANTS */
var PADDLE_WIDTH = 60;
var PADDLE_HEIGHT = 20;
var PADDLE_TOP = 360;
/** Declare BALL_CONSTANTS */
var BALL_SIZE = 20;
var BALL_SPEED = 4; // Declare Variable for Ball Speed
/** Declare paddleOperators */
var paddleLeft;
/** Declare ballOperators */
var ballDirectionX = "right";
var ballDirectionY = "down";
var ballPositionX = 40;
var ballPositionY = 200;
/** Create Paddle Functions */
var updatePaddle = function(){
paddleLeft = mouseX - PADDLE_WIDTH / 2;
};
var drawPaddle = function(){
rect(paddleLeft, PADDLE_TOP, PADDLE_WIDTH, PADDLE_HEIGHT);
};
/** Create Ball Functions */
var updateBall = function(){
// If it hits the Right side of Screen
if(ballPositionX > 399 - BALL_SIZE / 2){
ballDirectionX = "left"; // Reverse Direction
}
// If it hits the Left side of Screen
if(ballPositionX < BALL_SIZE / 2){
ballDirectionX = "right"; // Forward Direction
}
// The Bottom of the Screen
if(ballPositionY > 399 - BALL_SIZE / 2){
ballDirectionY = "up";
}
// The Top of Screen */
if(ballPositionY < BALL_SIZE / 2){
ballDirectionY = "down";
}
// Add BALL_SPEED to Ball Position X
if(ballDirectionX === "right"){
ballPositionX += BALL_SPEED;
}else{
ballPositionX -= BALL_SPEED;
}
// Add BALL_SPEED to Ball Position Y
if(ballDirectionY === "down"){
ballPositionY += BALL_SPEED;
}else{ ballPositionY -= BALL_SPEED; }
};
// Create the Draw Ball Function
var drawBall = function(){
ellipse(ballPositionX, ballPositionY, BALL_SIZE, BALL_SIZE);
};
//The all important: draw = function(){ };
draw = function(){ // Begin Draw Loop
background(0); // Clear the screen Black
size(400, 400); //Size of the game screen
/** Call Ball Functions */
drawBall();
updateBall();
/** Call Paddle Functions */
updatePaddle();
drawPaddle();
}; // End Draw Loop
/** Create the Ball Update Function **/ var updateBall = function(){ // If it hits the Right side of Screen if(ballPositionX > 399 - BALL_SIZE / 2){ ballDirectionX = "left"; // Reverse Direction } // If it hits the Left side of Screen if(ballPositionX < BALL_SIZE / 2){ ballDirectionX = "right"; // Forward Direction }