From 5f517c1a47b640a7c7424e7465af135b3308b4dd Mon Sep 17 00:00:00 2001 From: dhain Date: Tue, 10 Jan 2023 10:35:52 +0100 Subject: [PATCH] score resets after first jump --- frontend/game.ts | 22 +++++++++++++++++----- frontend/model/Raspberry.ts | 6 +++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/frontend/game.ts b/frontend/game.ts index 3d679ad..994b8a3 100644 --- a/frontend/game.ts +++ b/frontend/game.ts @@ -8,8 +8,9 @@ let backgroundImage: any; let obstacles: Obstacle[] = []; let raspberry: Raspberry; let paused: boolean; -let score: number; -let hasAlreadyScored: boolean; +let score: number = 0; +let hasAlreadyScored: boolean = false; +let hasDied: boolean = false; /** * p5 setup function. @@ -32,8 +33,6 @@ function setup() { */ function setupGame() { paused = true; - score = 0; - hasAlreadyScored = false; raspberry = new Raspberry(RASPBERRY_IMAGE_PATH); Obstacle.distanceBetweenPipes = height / 2.5; @@ -65,6 +64,7 @@ function draw() { // Check for collisions with pipes and set score if (!paused) { if (obstacles[0].collides(raspberry)) { + hasDied = true; setupGame(); } checkRaspberryScore(); @@ -133,12 +133,24 @@ function checkRaspberryScore() { } } +/** + * Resets the score if game is started + */ +function resetScore(): void { + if(hasDied){ + hasDied = false; + score = 0; + hasAlreadyScored = false; + } +} + /** * Handler for key events. */ -function keyPressed() { +function keyPressed() { // Jump if (key.toLowerCase() == "k") { + resetScore(); raspberry.boost(); } diff --git a/frontend/model/Raspberry.ts b/frontend/model/Raspberry.ts index 36fb3a2..aaa6716 100644 --- a/frontend/model/Raspberry.ts +++ b/frontend/model/Raspberry.ts @@ -4,8 +4,8 @@ class Raspberry extends Entity { private _velocity: number = 0; private _image: any; + private static position: Position; private static readonly maxVelocity: number = 100; - private static POSITION: Position; private static readonly WIDTH: number = 180; private static readonly HEIGHT: number = 70; private static readonly FILL: number = 0; @@ -33,8 +33,8 @@ class Raspberry extends Entity { * Constructs the Raspberry with fixed sizes */ constructor(image: string) { - Raspberry.POSITION = new Position(width / 6, height / 2) - super(Raspberry.POSITION, Raspberry.WIDTH, Raspberry.HEIGHT, Raspberry.FILL); + Raspberry.position = new Position(width / 6, height / 2); + super(Raspberry.position, Raspberry.WIDTH, Raspberry.HEIGHT, Raspberry.FILL); this.image = image; }