-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUI.js
More file actions
44 lines (44 loc) Β· 1.57 KB
/
UI.js
File metadata and controls
44 lines (44 loc) Β· 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class UI {
constructor(game) {
this.game = game;
this.fontSize = 25;
this.fontFamily = 'Bangers';
this.color = 'white';
}
draw(context) {
context.save();
context.fillStyle = this.color;
context.shadowOffsetX = 2;
context.shadowOffsetY = 2;
context.shadowColor = 'black';
context.font = this.fontSize + 'px ' + this.fontFamily;
// score
context.fillText('Score: ' + this.game.score, 20, 40);
// timer
const formattedTime = (this.game.gameTime * 0.001).toFixed(1);
context.fillText('Timer: ' + formattedTime, 20, 100);
// game over messages
if (this.game.gameOver) {
context.textAlign = 'center';
let message1;
let message2;
if (this.game.isWin()) {
message1 = 'Most Wondrous!';
message2 = 'Well done explorer!';
} else {
message1 = 'Blazes!';
message2 = 'Get my repair kit and try again!';
}
context.font = '70px ' + this.fontFamily;
context.fillText(message1, this.game.width * 0.5, this.game.height * 0.5 - 20);
context.font = '25px ' + this.fontFamily;
context.fillText(message2, this.game.width * 0.5, this.game.height * 0.5 + 20);
}
// ammo
if (this.game.player.powerUp) context.fillStyle = '#ffffbd';
for (let i = 1; i < this.game.ammo; i++) {
context.fillRect(20 + 5 * i, 50, 3, 20);
}
context.restore();
}
}