xxxxxxxxxx
171
//variaveis bolinha
let xBolinha = 300;
let yBolinha = 200;
let diametro= 20;
let raio = diametro /2 ;
//variaveis velocidade
let velocidadexBolinha = 6;
let velocidadeyBolinha = 6;
//variaveis da minha raquete
let xRaquete = 5;
let yRaquete = 150;
let raqueteComprimento = 10;
let raqueteAltura = 80;
let colidiu = false;
//variáveis do oponente
let xRaqueteOponente = 585;
let yRaqueteOponente = 150;
let velocidadeyOponente;
let chanceDeErrar = 0;
//Placar do jogo
let meusPontos = 0;
let pontosOponente = 0;
function setup() {
createCanvas(600, 400);
trilha.loop();
}
//sons do jogo
let raquetada;
let ponto;
let trilha;
function draw() {
background(0);
mostraBolinha();
movimentaBolinha();
verificaColisaoBorda();
mostraRaquete(xRaquete,yRaquete);
movimentaMinhaRaquete();
//verificaColisaoRaquete();
verificaColisaoRaquete(xRaquete, yRaquete);
mostraRaquete(xRaqueteOponente,yRaqueteOponente);
movimentaRaqueteOponente();
verificaColisaoRaquete(xRaqueteOponente, yRaqueteOponente);
incluiPlacar();
marcaPonto();
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(UP_ARROW)){
yRaquete -= 10
}
if (keyIsDown(DOWN_ARROW)){
yRaquete += 10
}
}
function verificaColisaoRaquete(){
if (xBolinha - raio < xRaquete + raqueteComprimento
&& yBolinha - raio < yRaquete + raqueteAltura
&& yBolinha + raio > yRaquete){
velocidadexBolinha *= -1
raquetada.play();
}
}
function verificaColisaoRaquete(x, y){
colidiu =
collideRectCircle(x, y, raqueteComprimento, raqueteAltura, xBolinha, yBolinha, raio);
if (colidiu){
velocidadexBolinha *= -1
raquetada.play()
}
}
function movimentaRaqueteOponente(){
velocidadeyOponente = yBolinha - yRaqueteOponente - raqueteComprimento /2 -30
yRaqueteOponente += velocidadeyOponente + chanceDeErrar
calculaChanceDeErrar()
}
function incluiPlacar(){
stroke(255)
textFont(fonte)
textAlign (CENTER);
textSize (70);
fill (255);
text(meusPontos, 150, 70);
text(pontosOponente, 450, 70);
}
function marcaPonto(){
if (xBolinha > 590){
meusPontos += 1
ponto.play()
}
if (xBolinha < 10){
pontosOponente += 1
ponto.play()
}
}
function preload(){
trilha = loadSound("trilha.mp3");
ponto = loadSound ("ponto.mp3");
raquetada = loadSound ("raquetada.mp3")
fonte = loadFont ("ARCADECLASSIC.TTF")
}
function calculaChanceDeErrar() {
if (pontosOponente >= meusPontos + 1) {
chanceDeErrar += 8
if (chanceDeErrar >= 40){
chanceDeErrar = 54
}
} else {
if (pontosOponente <= meusPontos - 1){
chanceDeErrar -= 3
if (chanceDeErrar <= 36){
chanceDeErrar = 40
}
}
}
}
function bolinhaNaoFicaPresa(){
if (xBolinha - raio <= 1){
xBolinha = 30
}
if (xBolinha + raio >= 599){
xBolinha = 580
}
}