-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
193 lines (180 loc) · 6.9 KB
/
index.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!DOCTYPE html>
<html>
<head>
<title>HAHAHAHAHAHAHAHA Game</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Press Start 2P', cursive;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
background-color: black; /* Default background color */
color: white; /* Default text color */
}
#startMessage {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.2rem;
text-align: center;
}
#gameDisplay {
color: #00FF00;
flex-grow: 1;
padding: 20px;
font-size: 2rem;
overflow-y: auto;
outline: none; /* Remove blue border on focus */
word-wrap: break-word;
}
#gameDisplay.error {
color: red; /* Text color when game is over */
opacity: 0.5;
}
#footer {
padding: 10px;
text-align: center;
position: sticky;
bottom: 0;
font-size: 1.5rem
}
#result {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 2rem;
text-align: center;
display: none;
}
.error {
color: red;
}
#tryAgainMessage {
color: magenta;
font-weight: bold;
padding-bottom: 1rem;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div id="startMessage">Start typing <br><br> <span style="color: magenta">hahahahahahahaha</span><br><br> and keep going as fast as you can</div>
<div id="gameDisplay" tabindex="0"></div> <!-- tabindex for focus -->
<div id="footer"></div>
<div id="result">
<div id="gameOverMessage" class="hidden">Game Over!</div>
<div id="tryAgainMessage" class="hidden">START TYPING AGAIN!</div>
<div id="scoreMessage" class="hidden"></div>
</div>
<script>
const highScoreText = localStorage.getItem('highScore') || '0';
let highScore = parseInt(highScoreText);
let startTime, timerInterval, gameEnded = true, canStart = true;
const gameDisplay = document.getElementById('gameDisplay');
const footer = document.getElementById('footer');
const startMessage = document.getElementById('startMessage');
const resultDisplay = document.getElementById('result');
const gameOverMessage = document.getElementById('gameOverMessage');
const tryAgainMessage = document.getElementById('tryAgainMessage');
const scoreMessage = document.getElementById('scoreMessage');
document.body.addEventListener('keydown', function(event) {
if (!canStart) return; // Ignore keydown when game is not ready to start
if (gameEnded) {
if (canStart && event.key === 'h') {
resetGame();
startGame('h');
}
return; // Ignore other keys when game is over
}
if (event.key === 'h' || event.key === 'a') {
if (gameDisplay.textContent.length === 0 && event.key !== 'h') {
endGame(); // End game if first character is not 'h'
return;
}
gameDisplay.textContent += event.key;
updateFooter(Math.floor(gameDisplay.textContent.length / 2), (new Date() - startTime) / 1000);
}
});
function startGame(initialInput = '') {
gameDisplay.focus();
startTime = new Date();
timerInterval = setInterval(updateTime, 100);
gameDisplay.textContent = initialInput;
startMessage.classList.add('hidden');
footer.style.display = 'block';
gameDisplay.classList.remove('error');
gameEnded = false;
}
function updateTime() {
updateFooter(Math.floor(gameDisplay.textContent.length / 2), (new Date() - startTime) / 1000);
}
function updateFooter(has, time) {
footer.textContent = `${has} ha's in ${time.toFixed(2)}s`;
}
function endGame() {
clearInterval(timerInterval);
gameEnded = true;
canStart = false;
gameDisplay.classList.add('error');
footer.style.display = 'none';
const timeTaken = (new Date() - startTime) / 1000;
const score = calculateScore(gameDisplay.textContent.length, timeTaken);
let scoreText = `Score ${score}<br><br>High Score ${highScore}`
if (score > highScore) {
highScore = score;
scoreText = `<br><span style="color: yellow">New High Score! ${score}</span>`;
localStorage.setItem('highScore', score);
}
scoreMessage.innerHTML = `${Math.floor((gameDisplay.textContent.length - 1) / 2)} ha's<br><br>${timeTaken.toFixed(2)}s<br><br>${scoreText}`;
scoreMessage.classList.remove('hidden');
resultDisplay.style.display = 'block';
setTimeout(function() {
gameOverMessage.classList.add('hidden');
tryAgainMessage.classList.remove('hidden');
canStart = true;
}, 2000); // Delay showing TRY AGAIN for 2 seconds
}
function calculateScore(has, time) {
return Math.floor(has * (1 + (has / time)));
}
function resetGame() {
clearInterval(timerInterval);
gameDisplay.textContent = '';
footer.style.display = 'none';
resultDisplay.style.display = 'none';
gameOverMessage.classList.add('hidden');
tryAgainMessage.classList.add('hidden');
scoreMessage.classList.add('hidden');
gameDisplay.classList.remove('error');
startTime = null; // Reset start time
}
// Check for valid game input and end game if invalid
setInterval(function() {
const text = gameDisplay.textContent;
if (!gameEnded && startTime && (text.length > 1 && !alternateCheck(text))) {
endGame();
}
}, 100);
function alternateCheck(str) {
for (let i = 1; i < str.length; i++) {
if (str[i] === str[i - 1]) return false;
}
return true;
}
// Initial focus
window.onload = function() {
gameDisplay.focus();
};
</script>
</body>
</html>