xxxxxxxxxx
176
//Variáveis da bolinha
let xBolinha = 300;
let yBolinha = 200;
let diametro = 13;
let raio = diametro / 2;
//Velocidade da bolinha
let velocidadeXBolinha = 6;
let velocidadeYBolinha = 6;
let raqueteComprimento = 10;
let raqueteAltura = 90;
//variáveis da raquete
let xRaquete = 5;
let yRaquete = 150;
//variáveis do oponente
let xRaqueteOponente = 585;
let yRaqueteOponente = 150;
let velocidadeYOponente;
//variáveis do placar
let meusPontos = 0;
let pontosDoOponente = 0;
let xPlacar = 170;
let yPlacar = 26;
let xPlacarDoOponente = 470;
let yPlacarDoOponente = 26
//sons
let raquetada;
let ponto;
let trilha;
//chance de errar
let chanceDeErrar = 0;
function preload(){
trilha = loadSound("trilha.mp3")
ponto = loadSound ("ponto.mp3")
raquetada = loadSound ("raquetada.mp3")
}
function setup() {
createCanvas(600, 400);
trilha.loop()
}
function draw() {
background(0); //Fundo preto
mostraBolinha(); //Mostra a bolinha
movimentaBolinha(); //Movimenta a bolinha
verificaColisaoBorda(); //Verifica a colisão com as bordas
mostraRaquete(xRaquete, yRaquete);
movimentaMinhaRaquete();
verificaColisaoRaquete();
mostraRaquete(xRaqueteOponente, yRaqueteOponente);
movimentaRaqueteOponente();
verificaColisaoRaqueteOponente ();
incluiPlacar ();
marcaPonto();
calculaChanceDeErrar();
bolinhaNaoFicaPresa()
}
function mostraBolinha(){
circle (xBolinha, yBolinha, diametro);
}
function movimentaBolinha (){
xBolinha += velocidadeXBolinha;
yBolinha += velocidadeYBolinha;
}
function verificaColisaoBorda(){
if (xBolinha + raio > width ||
xBolinha - raio < 0){
velocidadeXBolinha *= -1
}
if (yBolinha + raio > height ||
yBolinha - raio <0){
velocidadeYBolinha *= -1
}
}
function mostraRaquete(x, y){
rect(x, y, raqueteComprimento, raqueteAltura)
}
function movimentaMinhaRaquete(){
if (keyIsDown(87)){
yRaquete -= 10
if (yRaquete < - raqueteAltura){
yRaquete = height;
}
}
if (keyIsDown(83)){
yRaquete += 10
if(yRaquete > height){
yRaquete = - raqueteAltura
}
}
}
function verificaColisaoRaquete(){
if (xBolinha - raio < xRaquete + raqueteComprimento
&& yBolinha - raio < yRaquete + raqueteAltura &&
yBolinha + raio > yRaquete)
{
velocidadeXBolinha *= -1;
raquetada.play();
}
}
function verificaColisaoRaqueteOponente(){
if (xBolinha + raio > xRaqueteOponente
&& yBolinha - raio < yRaqueteOponente + raqueteAltura
&& yBolinha + raio > yRaqueteOponente){
velocidadeXBolinha *= -1;
raquetada.play();
}
}
function movimentaRaqueteOponente(){
velocidadeYOponente = yBolinha -yRaqueteOponente -
raqueteComprimento / 2 - 30;
yRaqueteOponente += velocidadeYOponente + chanceDeErrar
calculaChanceDeErrar ()
}
function incluiPlacar(){
stroke (255)
textAlign (CENTER)
textSize(16)
fill(255,0,0)
rect (150, 10, 40, 20)
fill (225)
text(meusPontos, xPlacar, yPlacar)
fill(255,0,0)
rect (450, 10, 40, 20)
fill (225)
text(pontosDoOponente, xPlacarDoOponente, yPlacarDoOponente)
}
function marcaPonto(){
if (xBolinha > 590){
meusPontos += 1;
ponto.play()
}
if (xBolinha < 10){
pontosDoOponente += 1;
ponto.play()
}
}
function calculaChanceDeErrar(){
if (pontosDoOponente >= meusPontos){
chanceDeErrar += 1
if (chanceDeErrar >=39) {
chanceDeErrar = 45
}
} else {
chanceDeErrar -= 1
if (chanceDeErrar <= 35){
chanceDeErrar = 35
}
}
}
function bolinhaNaoFicaPresa(){
if (xBolinha - raio < 0){
xBolinha = 26
}
if (xBolinha + raio > 600){
xBolinha = 557
}
}