//Load Backround for end of Game
PImage bg_img;
void setup()
{
size(800, 500);
bg_img = loadImage("vintage.jpg");
}
// Create size of Game
size(800, 500);
background(0, 0, 0);
var PADDLE_WIDTH = 60;
var PADDLE_HEIGHT = 20;
var PADDLE_TOP = 475;
var BALL_SIZE = 20;
var BALL_SPEED = 4; // Declare Variable for Ball Speed
/** Variable for number of Plays till end of Game **/
var playerLives = 3;
var paddleLeft;
var ballDirectionX = "right";
var ballDirectionY = "down";
var ballPositionX = 40;
var ballPositionY = 200;
var score = 0;
// 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 > 799 - BALL_SIZE / 2){
ballDirectionX = "left"; // Reverse Direction
}
// If it hits the Left side of Screen
if(ballPositionX < BALL_SIZE / 2){
ballDirectionX = "right"; // Forward Direction
}
/** Detect Collision with Paddle */
// If the Ball reaches the same level as Paddle Top...
if(ballPositionY > PADDLE_TOP - BALL_SIZE / 2 &&
ballPositionY < PADDLE_TOP + PADDLE_HEIGHT){
// If Ball Position X is between Paddle Left & Paddle Right
if(ballPositionX > paddleLeft &&
ballPositionX < paddleLeft + PADDLE_WIDTH){
// Deflect
ballDirectionY = "up";
}
}
/** If the Ball goes Offscreen */
if(ballPositionY > 520){
playerLives -= 1; // Subtract 1 from Number of Plays
ballPositionY = 30; // Reposition Ball for next serve
}
// 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);
};
draw = function(){ // Begin Draw Loop
background(0); // Clear the screen Black
// Call Ball Functions
drawBall();
updateBall();
// Call Paddle Functions
updatePaddle();
drawPaddle();
/** Print the playerLives Variable to the screen **/
text("Lives left: " + playerLives, 40, 50);
score++;
text("Ponts earned = " + score, 650, 50);
//PAU GAME
if(playerLives === 0) {
//background(0, 0, 255);
image(bg_img);
fill(0, 0, 255);
textSize(36);
textAlign(CENTER);
text("Pau Game", 400, 200);
textSize(24);
textAlign(CENTER);
text("But you did Score: " + score, 400, 250);
noLoop();
}
}; // End Draw Loop
/* Tutorials in plain English by Dillinger © 2013
All code is owned by its respective author
and made available under an MIT license:
http://opensource.org/licenses/mit-license.php */