Meu código:
//variables of Ball
let xBall = 300;
let yBall = 200;
let diameterBall = 20;
let radiusBall = diameterBall/2;
let speedXBall= 6;
let speedYBall= 6;
//variables myRacquet
let xMyRacquet = 5;
let yMyRacquet = 150;
let widthMyRacquet = 7;
let heightMyRacquet = 80;
//variables oponnentRacquet
let xOpponentRacquet = 588;
let yOpponentRacquet = 150;
let widthOpponentRacquet = 7;
let heightOpponentRacquet = 80;
let speedYOpponentRacquet;
function setup() {
createCanvas(600, 400);
}
function showBall() {
circle(xBall,yBall,diameterBall);
}
function moveBall() { xBall = xBall + speedXBall;
yBall = yBall + speedYBall;
}
function verificationTouchEdge(){
if (xBall+radiusBall > width || xBall-radiusBall < 0) {
speedXBall *= -1; }
if (yBall -radiusBall < 0 || yBall+radiusBall > height) {
speedYBall *= -1;
}
}
function showRacquet(x,y){
rect( x, y, widthMyRacquet, heightMyRacquet);
}
function moveMyRacquet() {
if (keyIsDown(UP_ARROW)) {
yMyRacquet -= 10;
}
if (keyIsDown(DOWN_ARROW)) {
yMyRacquet += 10;
}
}
function collideMyRacquet () {
if (xBall - radiusBall < xMyRacquet + widthMyRacquet &&
yBall + radiusBall > yMyRacquet && yBall - radiusBall < yMyRacquet + heightMyRacquet ) {
speedXBall *= -1
}
}
function moveOpponentRacquet () {
speedYOpponentRacquet = yBall - yOpponentRacquet - heightOpponentRacquet / 2 - 30;
yOpponentRacquet += speedYOpponentRacquet ;
}
function draw() {
background(0);
showBall();
moveBall();
verificationTouchEdge();
showRacquet(xMyRacquet, yMyRacquet);
showRacquet(xOpponentRacquet, yOpponentRacquet);
moveMyRacquet();
collideMyRacquet();
moveOpponentRacquet();
}