Skip to content

Commit

Permalink
add high score
Browse files Browse the repository at this point in the history
  • Loading branch information
fdegiovanni committed Jun 5, 2024
1 parent ca8268c commit 1baceb8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const config = {
physics: {
default: "arcade",
arcade: {
debug: true,
debug: false,
},
},
scene: [Preload, Game],
Expand Down
17 changes: 10 additions & 7 deletions scenes/Game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gameOptions, playSound, stopSound } from "../utils/gameOptions.js";
import { gameOptions, playSound, stopSound, setHighScore, getHighScore } from "../utils/gameOptions.js";

export default class Game extends Phaser.Scene {
constructor() {
Expand Down Expand Up @@ -52,33 +52,36 @@ export default class Game extends Phaser.Scene {

if (this.player.y > this.game.config.height || this.player.y < 0) {
this.gameOver = true;
const highScore = getHighScore();
this.cameras.main.shake(200, 0.01);
playSound(this.sounds.death, { delay: 0.5, volume: 0.5});
// pausar juego
this.physics.pause();
// show game over title
this.add
.text(this.centerX, this.centerY, "Game Over", {
fontSize: "32px",
fontSize: "56px",
fill: "#fff",
})
.setOrigin(0.5);
// show final score
this.add
.text(this.centerX, this.centerY + 50, `Score: ${this.score}`, {
fontSize: "32px",
.text(this.centerX, this.centerY + 80, `
Score: ${this.score}
High Score: ${highScore}
${this.score>highScore ? "😀" : "😞"}`, {
fontSize: "44px",
fill: "#fff",
})
.setOrigin(0.5);
// show restart message
this.add
.text(this.centerX, this.centerY + 100, "Click to restart", {
.text(this.centerX, this.centerY + 200, "Click to restart", {
fontSize: "32px",
fill: "#fff",
})
.setOrigin(0.5)
.setInteractive()
.on("pointerdown", () => {
setHighScore(this.score);
this.scene.restart();
});
return;
Expand Down
15 changes: 15 additions & 0 deletions utils/gameOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ export function stopSound(sound) {
sound.stop();
}

export function setHighScore(score) {
if (score > localStorage.getItem("highScore")) {
localStorage.setItem("highScore", score);
}
}

export function getHighScore() {
let highScore = localStorage.getItem("highScore");
if (highScore == null || highScore == undefined) {
highScore = 0;
} else {
highScore = parseInt(highScore);
}
return highScore;
}

0 comments on commit 1baceb8

Please sign in to comment.