forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalo.js
142 lines (125 loc) · 3.07 KB
/
Kalo.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
@title: Kalo
@author: Fatiha
@tags: []
@addedOn: 2023-07-15
*/
const player = "p";
let promptText = null;
let scoreText = null;
let gameOverText = null;
let timeLeftText = null;
const promptTime = 2000; // Time limit in milliseconds
const promptDelay = 1000; // Delay between prompts in milliseconds
const characters = "ASDWIJKL";
let sequenceLength = 1;
let currentSequence = "";
let timer = null;
let score = 0;
let timeLeft = promptTime;
setLegend(
[ player, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222` ]
);
const level = map`
ppppp
ppppp
ppppp
ppppp`;
setMap(level);
onInput("a", () => handleInput("A"));
onInput("s", () => handleInput("S"));
onInput("d", () => handleInput("D"));
onInput("w", () => handleInput("W"));
onInput("i", () => handleInput("I"));
onInput("j", () => handleInput("J"));
onInput("k", () => handleInput("K"));
onInput("l", () => handleInput("L"));
displaySequence();
// Generate a random character from the available characters
function getRandomCharacter() {
return characters[Math.floor(Math.random() * characters.length)];
}
// Generate a random sequence of characters
function generateSequence(length) {
let sequence = "";
for (let i = 0; i < length; i++) {
sequence += getRandomCharacter();
}
return sequence;
}
// Display the current prompt sequence to the user
function displaySequence() {
currentSequence = generateSequence(sequenceLength);
clearText();
promptText = addText(currentSequence, { x: 2, y: 2 });
scoreText = addText(`Score: ${score}`, { x: 2, y: 4 });
timeLeftText = addText(`Time left: ${timeLeft / 1000}s`, { x: 2, y: 6 });
}
// Handle user input
function handleInput(key) {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (key === currentSequence[0]) {
currentSequence = currentSequence.slice(1);
if (currentSequence.length === 0) {
// User entered the sequence correctly
sequenceLength++;
score++;
timeLeft += 50;
displaySequence();
}
} else {
// User entered the wrong key, reset the game
sequenceLength = 1;
displayGameOver();
}
// Start the timer for the next prompt
timer = setTimeout(displaySequence, promptDelay);
}
// Display game over text
function displayGameOver() {
clearTimeout(timer);
timer = null;
clearText();
gameOverText = addText("Game Over", { x: 2, y: 2 });
scoreText = addText(`Final Score: ${score}`, { x: 2, y: 4 });
score = 0;
timeLeft = 1000;
timeLeftText = addText(`Timer: ${timeLeft / 1000}s`, { x: 2, y: 6 });
}
// Update time left
function updateTimeLeft() {
timeLeft -= 100;
if (timeLeft <= 0) {
clearTimeout(timer);
timer = null;
displayGameOver();
} else {
timer = setTimeout(updateTimeLeft, 100);
}
}
// Set up game initialization and input handling
function setupGame() {
displaySequence();
updateTimeLeft();
}
// Start the game
setupGame();