score resets after first jump

This commit is contained in:
dhain 2023-01-10 10:35:52 +01:00
parent 6927ba9e85
commit 5f517c1a47
2 changed files with 20 additions and 8 deletions

View file

@ -8,8 +8,9 @@ let backgroundImage: any;
let obstacles: Obstacle[] = []; let obstacles: Obstacle[] = [];
let raspberry: Raspberry; let raspberry: Raspberry;
let paused: boolean; let paused: boolean;
let score: number; let score: number = 0;
let hasAlreadyScored: boolean; let hasAlreadyScored: boolean = false;
let hasDied: boolean = false;
/** /**
* p5 setup function. * p5 setup function.
@ -32,8 +33,6 @@ function setup() {
*/ */
function setupGame() { function setupGame() {
paused = true; paused = true;
score = 0;
hasAlreadyScored = false;
raspberry = new Raspberry(RASPBERRY_IMAGE_PATH); raspberry = new Raspberry(RASPBERRY_IMAGE_PATH);
Obstacle.distanceBetweenPipes = height / 2.5; Obstacle.distanceBetweenPipes = height / 2.5;
@ -65,6 +64,7 @@ function draw() {
// Check for collisions with pipes and set score // Check for collisions with pipes and set score
if (!paused) { if (!paused) {
if (obstacles[0].collides(raspberry)) { if (obstacles[0].collides(raspberry)) {
hasDied = true;
setupGame(); setupGame();
} }
checkRaspberryScore(); 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. * Handler for key events.
*/ */
function keyPressed() { function keyPressed() {
// Jump // Jump
if (key.toLowerCase() == "k") { if (key.toLowerCase() == "k") {
resetScore();
raspberry.boost(); raspberry.boost();
} }

View file

@ -4,8 +4,8 @@ class Raspberry extends Entity {
private _velocity: number = 0; private _velocity: number = 0;
private _image: any; private _image: any;
private static position: Position;
private static readonly maxVelocity: number = 100; private static readonly maxVelocity: number = 100;
private static POSITION: Position;
private static readonly WIDTH: number = 180; private static readonly WIDTH: number = 180;
private static readonly HEIGHT: number = 70; private static readonly HEIGHT: number = 70;
private static readonly FILL: number = 0; private static readonly FILL: number = 0;
@ -33,8 +33,8 @@ class Raspberry extends Entity {
* Constructs the Raspberry with fixed sizes * Constructs the Raspberry with fixed sizes
*/ */
constructor(image: string) { constructor(image: string) {
Raspberry.POSITION = new Position(width / 6, height / 2) Raspberry.position = new Position(width / 6, height / 2);
super(Raspberry.POSITION, Raspberry.WIDTH, Raspberry.HEIGHT, Raspberry.FILL); super(Raspberry.position, Raspberry.WIDTH, Raspberry.HEIGHT, Raspberry.FILL);
this.image = image; this.image = image;
} }