// Global Variables
float pad1y = 300;
float pad1x = 75;
float padWidth = 25;
float padHeight = 125;
float pad2y = 300;
float pad2x = 725;
// these variables control motion
boolean pad1Up = false;
boolean pad1Down = false;
boolean pad2Up = false;
boolean pad2Down = false;
// these variables store the puck's x and y position
float puckX;
float puckY;
float puckSize = 25;
// these variables determine the speed of the puck's motion
float yvel = -3;
float xvel = -1;
int p1score = 0;
int p2score = 0;
// 3. change the value of this variable based on the desired difficulty
// 1 is the easiest
// 2 is medium
// 3 is the hardest
int difficulty = 1;
int paddleError; // gives the paddle a delay so it's not always moving towards the puck
void setup() {
size(800, 600);
rectMode(CENTER);
textAlign(CENTER, CENTER);
puckX = width/2;
puckY = height/2;
// set the value of paddleError based on the value of difficulty
if (difficulty == 1) {
paddleError = 20;
}
if (difficulty == 2) {
paddleError = 12;
}
if (difficulty == 3) {
paddleError = 5;
}
}
void draw() {
background(0);
fill(255, 255, 255);
pad1Controller();
pad2Controller();
puckController();
checkScore();
textSize(30);
text(p1score, 200, height/6);
text(p2score, 600, height/6);
}
void pad1Controller() {
rect(pad1x, pad1y, padWidth, padHeight);
if(pad1Up == true) {
pad1y -= 5;
}
if (pad1Down == true) {
pad1y += 5;
}
if (pad1y < padHeight/2) {
pad1y = padHeight/2;
}
if (pad1y > height - (padHeight/2)) {
pad1y = height - (padHeight/2);
}
}
// paddle 2 will be the AI
void pad2Controller() {
rect(pad2x, pad2y, padWidth, padHeight);
if (pad2Up == true) {
pad2y -= 5;
}
if (pad2Down == true) {
pad2y += 5;
}
// 1. makes the paddle unbeatable
// pad2y = puckY;
// 2. the paddle moves all the time,
// but at a slower speed than the puck is moving
// if (puckY > 0) {
// pad2y += yvel/2;
// } else {
// pad2y -= yvel/2;
// }
// 3. use the paddleError variable to add a delay to the paddle
if (pad2y + padHeight/2 + paddleError < puckY) {
if (yvel > 0) {
pad2y += yvel;
}
}
if (pad2y - paddleError - padHeight/2> puckY ) {
if (yvel < 0) {
pad2y += yvel;
}
}
// if (pad2y < padHeight/2) {
// pad2y = padHeight/2;
// }
// if (pad2y > height - (padHeight/2)) {
// pad2y = height - (padHeight/2);
// }
}
void puckController() {
ellipse(puckX, puckY, puckSize, puckSize);
puckX += xvel;
puckY += yvel;
if(puckY > (height - puckSize/2) || puckY < puckSize/2){
yvel = -yvel;
}
// 1. Add conditionals to check if the puck is touching each paddle.
// If it is, reverse vx.
if(abs(puckX - pad2x) < (25 + padWidth)/2){
if(abs(puckY - pad2y)<(25 + padHeight)/2){
xvel = -xvel;
yvel = -yvel;
}
}
if(abs(puckX - pad1x) < (25 + padWidth)/2){
if(abs(puckY - pad1y)<(25 + padHeight)/2){
xvel = -xvel;
yvel = -yvel;
}
}
}
void keyPressed() {
if (key == 'w') {
pad1Up = true;
}
if (key == 's') {
pad1Down = true;
}
if (keyCode == UP) {
pad2Up = true;
}
if (keyCode == DOWN) {
pad2Down = true;
}
}
void keyReleased() {
if (key == 'w') {
pad1Up = false;
}
if (key == 's') {
pad1Down = false;
}
if (keyCode == UP) {
pad2Up = false;
}
if (keyCode == DOWN) {
pad2Down = false;
}
}
void checkScore() {
if(puckX < 0){
p2score += 1;
respawnPuck();
}
if(puckX > width){
p1score += 1;
respawnPuck();
}
}
void respawnPuck() {
puckX = width/2;
puckY = height/2;
xvel = random(-3, 3);
yvel = random(-3, 3);
}