Skip to content

Commit

Permalink
Add score in game over screen
Browse files Browse the repository at this point in the history
  • Loading branch information
noofreuuuh committed Apr 14, 2024
1 parent d4527a6 commit 79f1194
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Games/TutoPhaser/src/game/scenes/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class Game extends Scene {
super("Game");
}

init() {
this.score = 0;
}

create(): void {
this.gameOver = false;

Expand Down Expand Up @@ -176,7 +180,7 @@ export class Game extends Scene {
player.setTint(0xff0000);
player.anims.play("turn");
this.gameOver = true;
this.changeScene("GameOver");
this.changeScene("GameOver", this.score);
}

private collectStar(player: GameObjectWithBody | Tile, star: GameObjectWithBody | Tile): void {
Expand Down Expand Up @@ -216,9 +220,9 @@ export class Game extends Scene {
}
}

changeScene(scene?: string) {
changeScene(scene?: string, score?: number): void {
if (scene) {
this.scene.start(scene);
this.scene.start(scene, { score });
} else {
this.scene.start("Boot");
}
Expand Down
18 changes: 18 additions & 0 deletions Games/TutoPhaser/src/game/scenes/GameOver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ export class GameOver extends Scene {
camera: Phaser.Cameras.Scene2D.Camera;
background: Phaser.GameObjects.Image;
gameOverText: Phaser.GameObjects.Text;
gameOverSubText: Phaser.GameObjects.Text;
score: number = 0;

constructor() {
super("GameOver");
}

init(data: { score?: number }) {
this.score = data?.score || 0;
}

create() {
this.camera = this.cameras.main;
this.camera.setBackgroundColor(0xff0000);
Expand All @@ -29,6 +35,18 @@ export class GameOver extends Scene {
.setOrigin(0.5)
.setDepth(100);

this.gameOverSubText = this.add
.text(400, 360, `Score: ${this.score}`, {
fontFamily: "Arial Black",
fontSize: 44,
color: "#ffffff",
stroke: "#000000",
strokeThickness: 8,
align: "center",
})
.setOrigin(0.5)
.setDepth(100);

EventBus.emit("current-scene-ready", this);
}

Expand Down

0 comments on commit 79f1194

Please sign in to comment.