score resets after first jump
This commit is contained in:
parent
6927ba9e85
commit
5f517c1a47
2 changed files with 20 additions and 8 deletions
|
|
@ -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() {
|
||||
// Jump
|
||||
if (key.toLowerCase() == "k") {
|
||||
resetScore();
|
||||
raspberry.boost();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue