Skip to content

Commit

Permalink
Add colisão, som colisão, reload tela inicial
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCMello committed Jul 30, 2020
1 parent 07c9db2 commit 7972c04
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 56 deletions.
Binary file added assets/sounds/caiu.wav
Binary file not shown.
Binary file added assets/sounds/hit.wav
Binary file not shown.
Binary file added assets/sounds/ponto.wav
Binary file not shown.
Binary file added assets/sounds/pulo.wav
Binary file not shown.
144 changes: 88 additions & 56 deletions jogo.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
console.log( "[John's Flappy Bird]");


const colisionSound = new Audio()
colisionSound.src = './assets/sounds/hit.wav'
const sprites = new Image();
sprites.src = './assets/sprites.png';

const canvas = document.querySelector('canvas');
const contexto = canvas.getContext('2d');

//[StartScreen]
const startScreen = {
spriteX: 134,
spriteY: 0,
width: 174,
height: 152,
canvasX: ( canvas.width / 2 ) - 174 / 2,
canvasY: 50,
print(){
contexto.drawImage(//(image(sprites), sx.spriteX, sy.spriteY, sWidth.width, sHeight.height, dx.vancasX, dy.canvasY, dWidth.width, dHeight.height);
sprites,
startScreen.spriteX, startScreen.spriteY, //inicio do recorte X>,Y^;
startScreen.width, startScreen.height, //width, height recorte na sprite;
startScreen.canvasX, startScreen.canvasY, // onde vai ser printdo no canvas;
startScreen.width, startScreen.height, //width, height, dentro do canvas;
);
}
}

//[Background]
const background = {
spriteX: 390,
Expand All @@ -38,11 +22,11 @@ const background = {
contexto.fillRect(0,0,canvas.width,canvas.height); //ctx.fillRect(x, y, width, height);

contexto.drawImage(//(image(sprites), sx.spriteX, sy.spriteY, sWidth.width, sHeight.height, dx.vancasX, dy.canvasY, dWidth.width, dHeight.height);
sprites,
background.spriteX, background.spriteY, //inicio do recorte X>,Y^;
background.width, background.height, //width, height recorte na sprite;
background.canvasX, background.canvasY, // onde vai ser printdo no canvas;
background.width, background.height, //width, height, dentro do canvas;
sprites,
background.spriteX, background.spriteY, //inicio do recorte X>,Y^;
background.width, background.height, //width, height recorte na sprite;
background.canvasX, background.canvasY, // onde vai ser printdo no canvas;
background.width, background.height, //width, height, dentro do canvas;
);
contexto.drawImage(
sprites,
Expand All @@ -53,34 +37,6 @@ const background = {
);
}
}

//[Player]
const flappyBird = {
spriteX: 0,
spriteY: 0,
width: 33,
height: 24,
canvasX: 10,
canvasY: 50,
gravidade: 0.25,
velocidade: 0,
reload(){
flappyBird.velocidade = flappyBird.velocidade + flappyBird.gravidade;
console.log(flappyBird.velocidade);
flappyBird.canvasY = flappyBird.canvasY + flappyBird.velocidade;
},
print(){
contexto.drawImage(//(image(sprites), sx.spriteX, sy.spriteY, sWidth.width, sHeight.height, dx.vancasX, dy.canvasY, dWidth.width, dHeight.height);
sprites,
flappyBird.spriteX, flappyBird.spriteY, //inicio do recorte X>,Y^;
flappyBird.width, flappyBird.height, //width, height recorte na sprite;
flappyBird.canvasX, flappyBird.canvasY, // onde vai ser printdo no canvas;
flappyBird.width, flappyBird.height, //width, height, dentro do canvas;
);
}
}


//[Floor]
const floor = {
spriteX: 0,
Expand All @@ -106,20 +62,94 @@ const floor = {
);
}
}
//[Colision]
function colidesFloor(flappyBird, floor){
const flappybirdY = flappyBird.canvasY + flappyBird.height;
const floorLimit = floor.canvasY;

if(flappybirdY >= floorLimit){
return true;
}
return false;
}
//[Player] - BIRD
function newBird(){
const flappyBird = {
spriteX: 0,
spriteY: 0,
width: 33,
height: 24,
canvasX: 10,
canvasY: 50,
flap: 4.5,
fly(){
flappyBird.speed = - flappyBird.flap;
},
gravity: 0.25,
speed: 0,
reload(){
if(colidesFloor(flappyBird,floor)) {
colisionSound.play();
setTimeout(()=>{

changeScreen(screens.START);
}, 300)
return
}
flappyBird.speed = flappyBird.speed + flappyBird.gravity;
flappyBird.canvasY = flappyBird.canvasY + flappyBird.speed;
},
print(){
contexto.drawImage(//(image(sprites), sx.spriteX, sy.spriteY, sWidth.width, sHeight.height, dx.vancasX, dy.canvasY, dWidth.width, dHeight.height);
sprites,
flappyBird.spriteX, flappyBird.spriteY, //inicio do recorte X>,Y^;
flappyBird.width, flappyBird.height, //width, height recorte na sprite;
flappyBird.canvasX, flappyBird.canvasY, // onde vai ser printdo no canvas;
flappyBird.width, flappyBird.height, //width, height, dentro do canvas;
);
},
}
return flappyBird;
}
//[StartScreen]
const startScreen = {
spriteX: 134,
spriteY: 0,
width: 174,
height: 152,
canvasX: ( canvas.width / 2 ) - 174 / 2,
canvasY: 50,
print(){
contexto.drawImage(//(image(sprites), sx.spriteX, sy.spriteY, sWidth.width, sHeight.height, dx.vancasX, dy.canvasY, dWidth.width, dHeight.height);
sprites,
startScreen.spriteX, startScreen.spriteY, //inicio do recorte X>,Y^;
startScreen.width, startScreen.height, //width, height recorte na sprite;
startScreen.canvasX, startScreen.canvasY, // onde vai ser printdo no canvas;
startScreen.width, startScreen.height, //width, height, dentro do canvas;
);
}
}
//[Screens]
const global = {};
let activeScreen = {};

function changeScreen(newScreen){
activeScreen = newScreen;

if(activeScreen.restart){
activeScreen.restart();
}
}

const screens = {
START:{
restart(){
global.flappyBird = newBird();
},
print(){
background.print();
floor.print();
flappyBird.print();
global.flappyBird.print();
startScreen.print();
},
click(){
Expand All @@ -135,11 +165,13 @@ screens.GAME = {
print(){
background.print();
floor.print();
flappyBird.print();
global.flappyBird.print();
},
click(){
global.flappyBird.fly();
},
reload(){
flappyBird.reload();

global.flappyBird.reload();
}
};

Expand Down

0 comments on commit 7972c04

Please sign in to comment.