-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
90 lines (76 loc) · 2.35 KB
/
script.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Create a new Phaser game instance
const config = {
type: Phaser.AUTO,
width: 2560,
height: 1440,
scene: {
preload,
create,
update
}
};
const game = new Phaser.Game(config);
// Timer variables
let timer = 300; // Start with 5 minutes (300 seconds)
let timerText;
let countdownActive = false;
let countdownAccumulator = 0;
function preload() {
this.load.image('background', 'assets/Untitled_design.png');
}
function create() {
const bg = this.add.image(1280, 720, 'background');
bg.setOrigin(0.5);
bg.setDisplaySize(this.cameras.main.width, this.cameras.main.height);
// Display timer
timerText = this.add.text(1280, 720, formatTime(timer), {
fontSize: '216px',
color: '#100c08',
fontFamily: 'Impact',
fontStyle: 'bold'
}).setOrigin(0.5);
// Keyboard input
this.input.keyboard.on('keydown', (event) => {
if (event.code === 'ArrowUp') {
timer = Math.min(timer + 30, 3599); // Add 30 seconds, max 59:59
updateTimerText();
} else if (event.code === 'ArrowDown') {
timer = Math.max(timer - 30, 0); // Subtract 30 seconds, min 0
updateTimerText();
} else if (event.code === 'ArrowRight') {
countdownActive = true; // Start timer
} else if (event.code === 'ArrowLeft') {
resetTimer(); // Reset timer
}
});
}
function update(time, delta) {
if (countdownActive && timer > 0) {
countdownAccumulator += delta;
// Update timer every 1000ms (1 second)
if (countdownAccumulator >= 1000) {
countdownAccumulator -= 1000;
timer -= 1;
if (timer <= 0) {
timer = 0;
countdownActive = false; // Stop countdown when it reaches zero
}
updateTimerText();
}
}
}
function updateTimerText() {
timerText.setText(formatTime(timer));
}
function resetTimer() {
timer = 300; // Reset to 5 minutes
countdownActive = false;
countdownAccumulator = 0; // Reset accumulated delta
updateTimerText();
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
}
// Countdown Timer by doubletrouble83