////////Declare Variables/////////
//Paddle//
float paddlePositionY=300;
float paddlePositionX=100;
float paddleSpeed=5;
float paddlePositionXmove = 0;
//Ball//
float ballWidth = 60;
float ballHeight = 60;
float ballPositionX = 400;
float ballPositionY = 0;
float ballSpeedX = 1;
float ballSpeedY = 1;
//Wallpaper//
//colours
color leftWallColor= color (255);
color rightWallColor= color (0);
//start point for loop
float dottedLinePositionX;
void setup() {
size(400, 400);
println("Keep the ball up! Move hands using A & D and <- & ->");
println("Don't worry if you miss!");
}
void draw() {
background(0);
drawWallpaper();
updateWallpaper();
drawPaddle();
updatePaddle();
drawBall();
updateBall();
}
void drawWallpaper() {
rectMode (CORNERS);
/////WALLPAPER BASE//////
///Left Side///
noStroke();
fill(leftWallColor);
rect(0, 0, 200, 400);
///Right Side///
fill(rightWallColor);
rect(200, 0, 400, 400);
}
void updateWallpaper() {
/////BASE COLOUR changes if ball hits bottom of screen/////
if (ballPositionY > 399) {
leftWallColor = color (random(200, 255), random(200, 255), random(200, 255));
rightWallColor = color (random(0, 55), random(0, 55), random(0, 55));
}
/////DOTTED LINE////////
rectMode(CORNER);
///Resets Start position of Dotted Line every time///
dottedLinePositionX = 0;
//makes the dashes loop across the screen//
while (dottedLinePositionX <= 450) {
fill (150, 0, 0);
rect(dottedLinePositionX, 340, 20, 4);
dottedLinePositionX += 30;
}
}
void drawPaddle() {
rectMode (CENTER);
if (paddlePositionX>200) {
fill (255);
} else {
fill(0);
}
rect (paddlePositionX, paddlePositionY, 60, 10);
}
void keyPressed() {
///MOVES RIGHT PADDLE///
if (key == 'j') {
paddlePositionXmove = -1;
}
if (key == 'l') {
paddlePositionXmove= 1;
}
///MOVES LEFT PADDLE//
if (key == 'a') {
paddlePositionXmove = -1;
}
if (key == 'd') {
paddlePositionXmove= 1;
}
}
void keyReleased() {
///STOPS PADDLE///
if (key == 'a') {
paddlePositionXmove = 0;
} else if (key == 'd') {
paddlePositionXmove = 0;
}
}
void updatePaddle() {
paddlePositionX += paddlePositionXmove*paddleSpeed;
}
void updateBall() {
//////BALL BOUNDS////////
///creates bounderies for the ball///
if (ballPositionX < 0 || ballPositionX > width || ballPositionY > 500) {
ballSpeedX = -ballSpeedX;
}
/////BALL BOUNCE/////////
///if a ball hits a hand it "bounces" back up////
float distance = abs(paddlePositionX - ballPositionX);
if (distance < 60 && ballPositionY > 300) {
ballSpeedY = ballSpeedY * -1;
ballSpeedX = random(-5, 5);
println("hit");
}
////if ball hits bottom of screen it's speed gets reversed as well///
else if (ballPositionY > 400) {
ballSpeedY = ballSpeedY * -1;
ballSpeedX = random(-5, 5);
} else
ballSpeedY +=.5;
ballPositionX += ballSpeedX;
ballPositionY += ballSpeedY;
}
void drawBall() {
noStroke();
///Change Colour of Ball based on X pos///
if (ballPositionX>200) {
fill (255);
} else {
fill(0);
}
ellipse(ballPositionX, ballPositionY, ballWidth, ballHeight);
}