Use arrow keys to move up, down, or hold. Every time you hit an obstacle, the ball moves faster. Don't let it hit your side!
xxxxxxxxxx
var LEFTWALL, RIGHTWALL, TOPWALL, BOTTOMWALL;
var isplaying = true;
var score = 0;
var timer;
// ball variables
var ballX = 400; var ballY = 250;
var ballSpeed = 7;
var ballDX = 1; var ballDY = -1;
// player 1's paddle
var paddle;
var paddleX = 1400; var paddleY = 250;
var paddleW = 30; var paddleH = 100;
var paddleSpeed = 10;
//bot paddles
var bot1X = 200; var bot1Y = 250;
var bot1W = 30; var bot1H = 160;
var bot2X = 400; var bot2Y = 500;
var bot2W = 30; var bot2H = 250;
var bot3X = 340; var bot3Y = 150;
var bot3W = 30; var bot3H = 100;
function setup() {
createCanvas(windowWidth, windowHeight);
rectMode(CENTER);
textAlign(CENTER);
textSize(50);
}
function draw() {
background(0);
if(!isplaying) //STARTUP SCREEN
{
fill(random(255), random(255), random(255), 150);
frameRate(3);
textAlign(CENTER);
text('PRESS SPACE TO START', width/2, height/2-100);
text('SCORE FIVE GOALS AS FAST AS YOU CAN', width/2, height/2);
}
if(isplaying) // MAIN GAME
{
fill(255, 0, 255, 100);
timer = 0;
timer = int(millis()/1000);
text(timer + ' SECONDS' + ' SCORE: ' + score, width/2, height/2-300);
keyPressed();
frameRate(60);
fill(255, 0, 255, 100);
TOPWALL = rect(0, 0, 3500, 20);
BOTTOMWALL = rect(0, windowHeight, 3500, 20);
//draw ball
noStroke();
fill(random(255), random(255), random(255));
rect(ballX, ballY, 20, 20);
//draw paddle
noStroke();
fill(random(255), random(255), random(255));
rect(paddleX, paddleY, 30, 100);
//draw bot paddles
noStroke();
fill(random(255), random(255), random(255));
rect(bot1X, bot1Y, 30, 160);
//bot2
noStroke();
fill(random(255), random(255), random(255));
rect(bot2X, bot2Y, 30, 250);
//bot3
noStroke();
fill(random(255), random(255), random(255));
rect(bot3X, bot3Y, 30, 100);
//move ball
ballX = ballX+(ballDX*ballSpeed);
ballY = ballY+(ballDY*ballSpeed);
//BALL HITS PADDLE
if(ballX >= paddleX-paddleW/2 && ballX <= paddleX+paddleW/2 && ballY >= paddleY-paddleH/2 && ballY <= paddleY+paddleH/2){
//hit player so bounce
ballDX = ballDX*-1;
}
if(ballX >= bot1X-bot1W/2 && ballX <= bot1X+bot1W/2 && ballY >= bot1Y-bot1H/2 && ballY <= bot1Y+bot1H/2){
//hit player so bounce
ballDX = ballDX*-1;
ballSpeed*=1.01;
}
if(ballX >= bot2X-bot2W/2 && ballX <= bot2X+bot2W/2 && ballY >= bot2Y-bot2H/2 && ballY <= bot2Y+bot2H/2){
//hit player so bounce
ballDX = ballDX*-1;
ballSpeed*=1.01;
}
if(ballX >= bot3X-bot3W/2 && ballX <= bot3X+bot3W/2 && ballY >= bot3Y-bot3H/2 && ballY <= bot3Y+bot3H/2){
//hit player so bounce
ballDX = ballDX*-1;
ballSpeed*=1.01;
}
//WHEN BALL HITS WALL
if(ballX >= width) {
ballDX = ballDX*-1;
score--;
}
if(ballX <= 0) {
ballDX = ballDX*-1;
score++;
}
if(ballY >= height) {
ballDY = ballDY*-1;
}
if(ballY <= 0) {
ballDY = ballDY*-1;
}
if(score>=5){
background(0);
fill(random(255), random(255), random(255), 150);
frameRate(3);
textAlign(CENTER);
text('YOU WIN', width/2, height/2-50);
text('PRESS REFRESH TO PLAY AGAIN', width/2, height/2);
}
}
function keyPressed(){
if(key == ' '){
restartGame();
}
if(keyCode == UP_ARROW){
paddleY = paddleY-paddleSpeed; //move up
}
if(keyCode == DOWN_ARROW){
paddleY = paddleY+paddleSpeed; //move down
}
}
function restartGame(){
score = 0;
timer = 0;
isplaying = true;
}
}