Beta funcional
This commit is contained in:
parent
5e2f525522
commit
1042050308
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"liveServer.settings.port": 5501
|
||||||
|
}
|
||||||
Binary file not shown.
135
game.js
135
game.js
|
|
@ -11,35 +11,8 @@ const states = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const images = {};
|
const images = {};
|
||||||
|
|
||||||
const audiosFx = {};
|
const audiosFx = {};
|
||||||
|
|
||||||
|
|
||||||
// function cargarImagenes(images, callback) {
|
|
||||||
// const nombresImagenes = [
|
|
||||||
// "bg",
|
|
||||||
// "suelo",
|
|
||||||
// "tuberias",
|
|
||||||
// "flappybird",
|
|
||||||
// "logo",
|
|
||||||
// "ready",
|
|
||||||
// "gameover",
|
|
||||||
// "taptap",
|
|
||||||
// ];
|
|
||||||
// let imagenesCargadas = 0;
|
|
||||||
// nombresImagenes.forEach((nombre) => {
|
|
||||||
// const img = new Image();
|
|
||||||
// img.src = `./assets/${nombre}.png`;
|
|
||||||
// img.onload = () => {
|
|
||||||
// imagenesCargadas++;
|
|
||||||
// if (imagenesCargadas === nombresImagenes.length) {
|
|
||||||
// callback(); // Llamar al callback una vez que todas las imágenes estén cargadas
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// images[nombre] = img;
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
function cargarRecursos(images, audioFx, callback) {
|
function cargarRecursos(images, audioFx, callback) {
|
||||||
const recursos = [
|
const recursos = [
|
||||||
{ nombre: "bg", tipo: "imagen" },
|
{ nombre: "bg", tipo: "imagen" },
|
||||||
|
|
@ -52,7 +25,7 @@ function cargarRecursos(images, audioFx, callback) {
|
||||||
{ nombre: "taptap", tipo: "imagen" },
|
{ nombre: "taptap", tipo: "imagen" },
|
||||||
{ nombre: "gameover", tipo: "audio" },
|
{ nombre: "gameover", tipo: "audio" },
|
||||||
{ nombre: "coin", tipo: "audio" },
|
{ nombre: "coin", tipo: "audio" },
|
||||||
{ nombre: "flap", tipo: "audio" }
|
{ nombre: "flap", tipo: "audio" },
|
||||||
];
|
];
|
||||||
let recursosCargados = 0;
|
let recursosCargados = 0;
|
||||||
|
|
||||||
|
|
@ -81,14 +54,6 @@ function cargarRecursos(images, audioFx, callback) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Llama a la función cargarRecursos para cargar imágenes y audios
|
|
||||||
cargarRecursos(images, audiosFx, iniciarJuego);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class State {
|
class State {
|
||||||
constructor(state) {
|
constructor(state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
|
|
@ -125,9 +90,9 @@ class StateRunnig extends State {
|
||||||
this.bird.nFramesAnim = 3;
|
this.bird.nFramesAnim = 3;
|
||||||
this.tuberias.forEach((tuberia) => {
|
this.tuberias.forEach((tuberia) => {
|
||||||
tuberia.inicializa();
|
tuberia.inicializa();
|
||||||
tuberia.velocidad = 1;
|
tuberia.velocidad = 1.5;
|
||||||
});
|
});
|
||||||
this.suelo.velocidad = 1;
|
this.suelo.velocidad = 1.5;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -206,7 +171,7 @@ class Suelo {
|
||||||
}
|
}
|
||||||
update() {
|
update() {
|
||||||
this.posX -= this.velocidad;
|
this.posX -= this.velocidad;
|
||||||
if (this.posX + this.sueloImg.width === 0) this.posX = 0;
|
if (this.posX + this.sueloImg.width <= 0) this.posX = 0;
|
||||||
}
|
}
|
||||||
draw() {
|
draw() {
|
||||||
ctx.drawImage(this.sueloImg, this.posX, this.posY);
|
ctx.drawImage(this.sueloImg, this.posX, this.posY);
|
||||||
|
|
@ -226,7 +191,7 @@ class Tuberia {
|
||||||
}
|
}
|
||||||
|
|
||||||
inicializa() {
|
inicializa() {
|
||||||
this.separacion = 100;
|
this.separacion = 120;
|
||||||
this.velocidad = 0;
|
this.velocidad = 0;
|
||||||
this.posX = this.initialPosX;
|
this.posX = this.initialPosX;
|
||||||
this.posY =
|
this.posY =
|
||||||
|
|
@ -247,6 +212,7 @@ class Tuberia {
|
||||||
update() {
|
update() {
|
||||||
this.posX -= this.velocidad;
|
this.posX -= this.velocidad;
|
||||||
if (this.posX <= 0 - this.width) {
|
if (this.posX <= 0 - this.width) {
|
||||||
|
this.aumentaDificultad();
|
||||||
this.generaPos();
|
this.generaPos();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -276,24 +242,31 @@ class Tuberia {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
detectarColision(bird) {
|
||||||
|
const xA = bird.posX;
|
||||||
|
const yA = Math.floor(bird.posY);
|
||||||
|
const wA = bird.width;
|
||||||
|
const hA = bird.height;
|
||||||
|
|
||||||
colision(bird){
|
const xB = this.posX;
|
||||||
if (
|
const yB = this.posY;
|
||||||
bird.posX + bird.width < xB || // Condición 1
|
const wB = this.width;
|
||||||
bird.posX > this.posX + this.width || // Condición 2
|
const hB = this.height;
|
||||||
bird.posY + hA < yB || // Condición 3
|
|
||||||
bird.posY > yB + hB // Condición 4
|
const xC = this.posX;
|
||||||
) {
|
const yC = this.posY + this.height + this.separacion;
|
||||||
// No hay colisión
|
const wC = this.width;
|
||||||
return false;
|
const hC = this.height;
|
||||||
} else {
|
|
||||||
// Hay colisión
|
const colisionConTuberia1 =
|
||||||
return true;
|
xA < xB + wB && xA + wA > xB && yA < yB + hB && yA + hA > yB;
|
||||||
}
|
|
||||||
|
const colisionConTuberia2 =
|
||||||
|
xA < xC + wC && xA + wA > xC && yA < yC + hC && yA + hA > yC;
|
||||||
|
|
||||||
|
return colisionConTuberia1 || colisionConTuberia2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
superada(bird) {
|
superada(bird) {
|
||||||
if (bird.posX > this.posX + this.width && this.puntuada === false) {
|
if (bird.posX > this.posX + this.width && this.puntuada === false) {
|
||||||
this.puntuada = true;
|
this.puntuada = true;
|
||||||
|
|
@ -301,6 +274,9 @@ class Tuberia {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
aumentaDificultad() {
|
||||||
|
if (this.separacion >= 80) this.separacion--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Bird {
|
class Bird {
|
||||||
|
|
@ -379,21 +355,15 @@ class Bird {
|
||||||
if (this.posY + this.height >= suelo.posY) {
|
if (this.posY + this.height >= suelo.posY) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (let i = 0; i < tuberias.length; i++) {
|
|
||||||
const tuberia = tuberias[i];
|
if (
|
||||||
if (
|
tuberias[0].detectarColision(this) ||
|
||||||
this.posX + this.width > tuberia.posX &&
|
tuberias[1].detectarColision(this)
|
||||||
this.posX < tuberia.posX + tuberia.width &&
|
) {
|
||||||
(this.posY < tuberia.posY + tuberia.height ||
|
return true;
|
||||||
this.posY + this.height >
|
|
||||||
tuberia.posY + tuberia.height + tuberia.separacion)
|
|
||||||
) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pasaTuberia(tuberias) {
|
pasaTuberia(tuberias) {
|
||||||
if (tuberias[0].superada(this) || tuberias[1].superada(this)) {
|
if (tuberias[0].superada(this) || tuberias[1].superada(this)) {
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -403,6 +373,7 @@ class Bird {
|
||||||
}
|
}
|
||||||
|
|
||||||
function iniciarJuego() {
|
function iniciarJuego() {
|
||||||
|
let bucle;
|
||||||
let puntuacion = 0;
|
let puntuacion = 0;
|
||||||
ctx.font = "24px 'Press Start 2P'";
|
ctx.font = "24px 'Press Start 2P'";
|
||||||
ctx.fillStyle = "white";
|
ctx.fillStyle = "white";
|
||||||
|
|
@ -421,7 +392,7 @@ function iniciarJuego() {
|
||||||
document.addEventListener("keydown", function (event) {
|
document.addEventListener("keydown", function (event) {
|
||||||
if (event.code === "Space") {
|
if (event.code === "Space") {
|
||||||
if (state.state === "IDLE" || state.state === "GAMEOVER") {
|
if (state.state === "IDLE" || state.state === "GAMEOVER") {
|
||||||
puntuacion=0;
|
puntuacion = 0;
|
||||||
state = new StateRunnig(sprites);
|
state = new StateRunnig(sprites);
|
||||||
state.enter();
|
state.enter();
|
||||||
}
|
}
|
||||||
|
|
@ -430,20 +401,9 @@ function iniciarJuego() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function gameLoop(timeStamp) {
|
function gameLoop() {
|
||||||
ctx.clearRect(0, 0, width, height);
|
ctx.clearRect(0, 0, width, height);
|
||||||
ctx.drawImage(images["bg"], 0, 0, width, height);
|
ctx.drawImage(images["bg"], 0, 0, width, height);
|
||||||
|
|
||||||
if (
|
|
||||||
sprites[0].detectarColisiones([sprites[1], sprites[2]], sprites[3]) &&
|
|
||||||
state.state !== "GAMEOVER"
|
|
||||||
) {
|
|
||||||
audiosFx["gameover"].play();
|
|
||||||
console.log('paso');
|
|
||||||
state = new StateGameOver(sprites);
|
|
||||||
state.enter();
|
|
||||||
}
|
|
||||||
|
|
||||||
sprites.forEach((sprite) => {
|
sprites.forEach((sprite) => {
|
||||||
sprite.update();
|
sprite.update();
|
||||||
sprite.draw();
|
sprite.draw();
|
||||||
|
|
@ -453,11 +413,20 @@ function iniciarJuego() {
|
||||||
puntuacion++;
|
puntuacion++;
|
||||||
}
|
}
|
||||||
ctx.fillText(puntuacion, 4, 30);
|
ctx.fillText(puntuacion, 4, 30);
|
||||||
requestAnimationFrame(gameLoop);
|
const colision = sprites[0].detectarColisiones(
|
||||||
|
[sprites[1], sprites[2]],
|
||||||
|
sprites[3]
|
||||||
|
);
|
||||||
|
if (colision && state.state !== "GAMEOVER") {
|
||||||
|
state = new StateGameOver(sprites);
|
||||||
|
state.enter();
|
||||||
|
audiosFx["gameover"].play();
|
||||||
|
window.cancelAnimationFrame(bucle);
|
||||||
|
}
|
||||||
|
bucle = requestAnimationFrame(gameLoop);
|
||||||
}
|
}
|
||||||
|
|
||||||
gameLoop();
|
gameLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
//cargarImagenes(images, iniciarJuego);
|
cargarRecursos(images, audiosFx, iniciarJuego);
|
||||||
cargarRecursos(images,audiosFx,iniciarJuego);
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@
|
||||||
<title>Flappy bird</title>
|
<title>Flappy bird</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<canvas id="lienzo"></canvas>
|
<canvas id="lienzo"></canvas> <br> <br>
|
||||||
|
<a id="repositorioGit" href="https://git.marklogo.duckdns.org/" target="_blank"> <i class="ti ti-brand-github"></i> Repositorio Git</a>
|
||||||
</body>
|
</body>
|
||||||
<script src="./game.js"></script>
|
<script src="./game.js"></script>
|
||||||
</html>
|
</html>
|
||||||
13
style.css
13
style.css
|
|
@ -21,4 +21,15 @@ body {
|
||||||
background-color: #2c5364;
|
background-color: #2c5364;
|
||||||
width: auto;
|
width: auto;
|
||||||
height: 80%;
|
height: 80%;
|
||||||
}
|
border: 4px solid white;
|
||||||
|
}
|
||||||
|
#repositorioGit {
|
||||||
|
display: inline-block;
|
||||||
|
background-color: #333;
|
||||||
|
color: #fff;
|
||||||
|
padding: 10px 20px;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
transition: background-color 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user