// ball variables
let xBall = 300;
let yBall = 200;
let diameter = 20;
let lightning = diameter / 2;
// ball speed
let speedXBall = 6;
let speedYBall = 6;
// racket variables
let xRacket = 5;
let yRacket = 150;
let heightRacket = 90;
let widthRacket = 10;
// opponent's variables - computer
let xRacketComputer = 585;
let yRacketComputer = 150;
let speedYComputer;
// game score
let myPoints = 0;
let pointsComputer = 0;
function setup() {
createCanvas(600, 400);
}
function draw() {
background(0);
showMeBall();
moveBall();
checksCollisionEdge();
showMeRacket(xRacket, yRacket);
moveRacket();
checkCollisionMyRacket();
showMeRacket(xRacketComputer, yRacketComputer);
moveRacketComputer();
checkCollisionRacketComputer();
includeScoreBoard();
scorePoint();
}
function showMeBall() {
circle(xBall, yBall, diameter);
}
function moveBall() {
xBall += speedXBall;
yBall += speedYBall;
}
function checksCollisionEdge() {
if (xBall + lightning > width || xBall - lightning < 0) {
speedXBall *= -1;
}
if (yBall + lightning > height || yBall - lightning < 0) {
speedYBall *= -1;
}
}
function showMeRacket(x, y) {
rect(x, y, widthRacket, heightRacket);
}
function moveRacket() {
if (keyIsDown(UP_ARROW)) {
yRacket -= 10;
}
if (keyIsDown(DOWN_ARROW)) {
yRacket += 10;
}
}
function checkCollisionMyRacket() {
if( xBall - lightning < xRacket + widthRacket && yBall - lightning < yRacket + heightRacket && yBall + lightning > yRacket) {
speedXBall *= -1;
}
}
function checkCollisionRacketComputer() {
if( xBall + lightning > xRacketComputer && yBall + lightning < yRacketComputer + heightRacket && yBall + lightning > yRacketComputer) {
speedXBall *= -1;
}
}
function moveRacketComputer() {
speedYComputer = yBall - yRacketComputer - widthRacket / 2 - 30;
yRacketComputer += speedYComputer;
}
function includeScoreBoard() {
fill(255);
text(myPoints, 278, 26);
text(pointsComputer, 321, 26);
}
function scorePoint() {
if (xBall > 590) {
myPoints += 1;
}
if (xBall < 10) {
pointsComputer += 1;
}
}