xxxxxxxxxx
243
// PLAY WITH THESE VARIABLES
//gravity acceleration
let a = 0.3;
//pipes velocity
let velocity = 2;
//width of pipes
let pipeWidth = 50;
//distance between pipes
let distanceBetweenPipes = 200; //se colocar menor que 150 da problema, pq aí o pipes[0] já passou da tela. Teria q usar pipes[1]
//velocity when you click mouse
let clickVelocity = 6.5;
let gapDist = 150;
let borderDist = 10;
let vo = 0;
let xo, yo;
let v;
let firstPipe = true;
let score = 0;
//bird size
let d = 20;
let r = d/2;
//timestep
let dt = 1;
let pipes = [];
let initialPipes = 5;
function setup() {
createCanvas(400, 400);
xo = width/4;
yo = 100;
getPipes();
circle(xo, yo, 10);
}
function draw() {
//background(0, 0, 255, 50);
background(0, 0, 255);
updateVelocityAndPosition();
updatePipes();
checkPipesArray();
checkFall();
checkCollision();
checkVelocity();
drawPipes();
drawBird();
drawScore();
}
function mouseClicked() {
vo = - abs(clickVelocity);
}
function keyPressed(){
if (key == ' ') {
vo = clickVelocity;
}
}
function checkVelocity() {
velocity = 2 + Math.floor(score/10);
if (velocity > 5) {
velocity = 5;
}
}
function checkCollision() {
let nextPipe = null;
// if(firstPipe) {
// nextPipe = pipes[0];
// if( (xo + r >= nextPipe.upperPipe.x) && (xo - r <= (nextPipe.upperPipe.x + pipeWidth))) {
// if(yo + r > nextPipe.lowerPipe.y || yo - r < nextPipe.upperPipe.height) {
// console.log("bateu");
// restart();
// }
// }
// if( xo - r > pipes[0].upperPipe.x + pipeWidth) {
// firstPipe = false;
// score++;
// }
// } else {
nextPipe = pipes[0]; //se a distancia for menor que 150, usar pipes[1]
if( (xo + r >= nextPipe.upperPipe.x) && (xo - r <= (nextPipe.upperPipe.x + pipeWidth))) {
if(yo + r > nextPipe.lowerPipe.y || yo - r < nextPipe.upperPipe.height) {
console.log("bateu");
restart();
}
}
//tentar usar >=
if( xo - r == nextPipe.upperPipe.x + pipeWidth) {
score++;
}
// }
}
function checkFall() {
if(yo > height) {
restart();
}
}
function restart() {
pipes = [];
yo = 100;
a = 0.3;
velocity = 2;
vo = 0;
score = 0;
getPipes();
circle(xo, yo, 10);
}
function checkPipesArray() {
if(pipes[0].upperPipe.x < - pipes[0].upperPipe.width) {
//menor que o negativo do cumprimento, ou seja, todo pipe saiu da tela
pipes.shift();
let lastX = pipes[pipes.length - 1].upperPipe.x;
upper = new Pipe(
lastX + distanceBetweenPipes,
0,
pipeWidth,
randomInteger(borderDist, height-borderDist-gapDist));
lower = new Pipe(
lastX + distanceBetweenPipes,
upper.height + gapDist,
pipeWidth,
height);
tempPipes = new Pipes(upper, lower);
pipes.push(tempPipes);
}
}
function drawBird() {
fill(255, 0, 0);
circle(xo, y, d);
}
function updatePipes() {
for(let i=0; i<pipes.length; i++) {
pipes[i].upperPipe.x -= velocity;
pipes[i].lowerPipe.x -= velocity;
}
}
function drawPipes() {
for(let i=0; i<pipes.length; i++) {
pipes[i].upperPipe.drawPipe();
pipes[i].lowerPipe.drawPipe();
}
}
function updateVelocityAndPosition() {
v = vo + a*dt;
y = yo + v*dt;
vo = v;
yo = y;
//velocity += increaseRate;
}
function getPipes() {
for(let i = 1; i <= initialPipes; i++) {
upper = new Pipe(
width/2+distanceBetweenPipes*i,
0,
pipeWidth,
randomInteger(borderDist, height-borderDist-gapDist));
lower = new Pipe(
width/2+distanceBetweenPipes*i,
upper.height + gapDist,
pipeWidth,
height);
tempPipes = new Pipes(upper, lower);
pipes.push(tempPipes);
}
}
class Pipe {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height
}
drawPipe() {
fill(0, 255, 0);
strokeWeight(2);
rect(this.x, this.y, this.width, this.height);
}
drawNextPipe() {
fill(0);
strokeWeight(2);
rect(this.x, this.y, this.width, this.height);
}
}
class Pipes {
constructor(upperPipe, lowerPipe) {
this.upperPipe = upperPipe;
this.lowerPipe = lowerPipe;
}
}
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function drawScore() {
fill(255);
rect(width*0.45, height*0.05, width*0.1, width*0.1);
textAlign(CENTER, TOP);
fill(0);
textSize(30);
text(score, width*0.5, height*0.07);
}