Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ronieremarques/fd7256bccca38904042a475115d4adf9 to your computer and use it in GitHub Desktop.
Save ronieremarques/fd7256bccca38904042a475115d4adf9 to your computer and use it in GitHub Desktop.
//variáveis da bolinha
let xBolinha = 100;
let yBolinha = 200;
let diametro = 20;
let raio = diametro / 2;
//variáveis do oponente
let xRaqueteOponente = 585;
let yRaqueteOponente = 150;
//velocidade da bolinha
let velocidadeXBolinha = 6;
let velocidadeYBolinha = 6;
//variáveis da raquete
let xRaquete = 5;
let yRaquete = 150;
let raqueteComprimento = 10;
let raqueteAltura = 90;
//placar do jogo
let meusPontos = 0;
let pontosDoOponente = 0;
let colidiu = false;
function setup() {
createCanvas(600, 400);
}
function draw() {
background(0);
mostraBolinha();
movimentaBolinha();
verificaColisaoBorda();
mostraRaquete(xRaquete, yRaquete);
movimentaMinhaRaquete();
verificaColisaoRaquete(xRaquete, yRaquete);
verificaColisaoRaquete(xRaqueteOponente, yRaqueteOponente);
mostraRaquete(xRaqueteOponente, yRaqueteOponente);
movimentaRaqueteOponente();
incluiPlacar()
marcaPonto();
}
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;
}
}
function verificaColisaoRaquete(x, y) {
colidiu = collideRectCircle(x, y, raqueteComprimento, raqueteAltura, xBolinha, yBolinha, raio);
if (colidiu){
velocidadeXBolinha *= -1;
}
}
function movimentaRaqueteOponente() {
velocidadeYOponente = yBolinha - yRaqueteOponente - raqueteComprimento / 2 - 30;
yRaqueteOponente += velocidadeYOponente
}
function incluiPlacar(){
stroke(255)
textAlign(CENTER);
textSize(16);
fill(color(255,140, 0));
rect(150, 10, 40, 20);
fill(255);
text(meusPontos, 170, 26);
fill(color(255,140, 0));
rect(450, 10, 40, 20);
fill(255);
text(pontosDoOponente, 470, 26);
}
function marcaPonto() {
if (xBolinha > 580) {
meusPontos += 1;
}
if (xBolinha < 10) {
pontosDoOponente += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment