-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLizard.html
More file actions
331 lines (285 loc) · 11.2 KB
/
Lizard.html
File metadata and controls
331 lines (285 loc) · 11.2 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lizard Flick</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Custom styles for the game elements and animation */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap');
:root {
--lizard-color: #4CAF50; /* Green */
--lizard-border: #388E3C; /* Darker Green */
--tongue-color: #F44336; /* Red */
--fly-color: #212121; /* Dark Grey */
--catch-distance: 80px; /* How far the tongue reaches */
}
body {
font-family: 'Inter', sans-serif;
background-color: #121212; /* Dark background */
}
.game-area {
position: relative;
width: 100%;
max-width: 400px;
height: 500px;
background-color: #2e7d32; /* Jungle green background */
border-radius: 1rem;
box-shadow: 0 10px 20px rgba(0,0,0,0.5);
overflow: hidden;
touch-action: manipulation; /* Prevents double-tap zoom on touch devices */
margin-top: 1rem;
}
/* --- LIZARD STYLING --- */
.lizard-head {
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 100px; /* Wider */
height: 50px; /* Taller */
background-color: var(--lizard-color);
border-top-left-radius: 50px;
border-top-right-radius: 50px;
z-index: 10;
border: 3px solid var(--lizard-border); /* Defined border */
box-shadow: 0 -5px 15px rgba(0,0,0,0.4);
display: flex;
justify-content: space-around;
align-items: center;
padding-top: 10px;
}
.lizard-eye {
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
border: 1px solid #222;
position: relative;
top: -10px; /* Position slightly above the curve */
}
.lizard-pupil {
position: absolute;
top: 2px;
left: 2px;
width: 5px;
height: 5px;
background-color: #222;
border-radius: 50%;
}
/* -------------------------- */
.tongue {
position: absolute;
bottom: 50px; /* Starts just above the new, larger head */
left: 50%;
width: 4px;
height: 0; /* Initial height */
background-color: var(--tongue-color);
transform: translateX(-50%);
transition: height 0.05s ease-out; /* Fast extension */
z-index: 9;
border-radius: 2px;
}
.tongue.flicked {
height: var(--catch-distance);
}
.fly {
position: absolute;
width: 16px;
height: 16px;
background-color: var(--fly-color);
border-radius: 50%;
transition: all 0.3s ease-in-out;
box-shadow: 0 0 8px rgba(0,0,0,0.5);
cursor: pointer;
z-index: 8;
}
.message-box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 1rem 2rem;
background-color: rgba(0, 0, 0, 0.9);
color: white;
border-radius: 10px;
text-align: center;
font-size: 1.25rem;
font-weight: bold;
display: none;
z-index: 20;
box-shadow: 0 0 20px rgba(255, 255, 255, 0.2);
}
.button-glow {
transition: background-color 0.1s, transform 0.1s;
}
.button-glow:active {
background-color: #1565C0; /* Darker blue on press */
transform: scale(0.98);
}
</style>
</head>
<body class="p-4 flex flex-col items-center min-h-screen">
<!-- Tailwind Config for Inter font -->
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
sans: ['Inter', 'sans-serif'],
},
}
}
}
</script>
<header class="text-center mb-4 p-2 w-full max-w-sm">
<h1 class="text-3xl font-bold text-white mb-2">Lizard Flick!</h1>
<p class="text-sm text-gray-400">Catch the fly when it's right above you!</p>
<div id="scoreDisplay" class="text-xl text-yellow-400 mt-2 p-1 bg-gray-800 rounded-lg">Score: 0</div>
</header>
<div id="gameArea" class="game-area">
<!-- The Fly (Target) -->
<div id="fly" class="fly"></div>
<!-- The Lizard Head with Eyes -->
<div class="lizard-head">
<div class="lizard-eye"><div class="lizard-pupil"></div></div>
<div class="lizard-eye"><div class="lizard-pupil"></div></div>
</div>
<!-- The Tongue -->
<div id="tongue" class="tongue"></div>
<!-- Game Message Box (for Start/Catch messages) -->
<div id="messageBox" class="message-box">
Tap to Start!
</div>
</div>
<!-- Control Button - Acts as the flick trigger on mobile -->
<button id="flickButton" class="mt-6 w-full max-w-xs p-4 bg-blue-600 hover:bg-blue-700 text-white font-bold rounded-xl shadow-lg button-glow uppercase tracking-wider">
Flick Tongue
</button>
<script type="module">
// Game Constants and State
const TONGUE_FLICK_DURATION_MS = 100; // How long the tongue stays extended
const FLY_MOVEMENT_INTERVAL_MS = 1500; // How often the fly moves
const CATCH_DISTANCE = 80; // Must match the CSS var(--catch-distance)
const LIZARD_HEAD_HEIGHT = 50; // The new, fixed height of the lizard head
const LIZARD_X_TOLERANCE = 30; // How far off-center the fly can be caught
let score = 0;
let gameRunning = false;
let flyInterval;
// DOM Elements
const gameArea = document.getElementById('gameArea');
const scoreDisplay = document.getElementById('scoreDisplay');
const flickButton = document.getElementById('flickButton');
const tongue = document.getElementById('tongue');
const fly = document.getElementById('fly');
const messageBox = document.getElementById('messageBox');
// Initial setup for fly position
let flyX = 0;
let flyY = 0;
/**
* Utility to display a temporary message in the center of the game area.
* @param {string} text - The message to display.
* @param {number} duration - Duration in milliseconds (default 1000).
*/
function showMessage(text, duration = 1000) {
messageBox.textContent = text;
messageBox.style.display = 'block';
// Hide the message after the duration
setTimeout(() => {
if (gameRunning) {
messageBox.style.display = 'none';
}
}, duration);
}
/**
* Sets a new random position for the fly within the game area boundaries.
*/
function moveFly() {
const areaWidth = gameArea.clientWidth;
const areaHeight = gameArea.clientHeight;
const flySize = fly.clientWidth;
// Avoid placing the fly inside the lizard head area (bottom LIZARD_HEAD_HEIGHT)
const playableHeight = areaHeight - LIZARD_HEAD_HEIGHT - 20;
// Random X position (from 10px to width - 10px - flySize)
flyX = Math.random() * (areaWidth - flySize - 20) + 10;
// Random Y position (from 10px to playableHeight)
flyY = Math.random() * (playableHeight - 10) + 10;
fly.style.left = `${flyX}px`;
fly.style.top = `${flyY}px`;
}
/**
* Checks if the extended tongue has collided with the fly.
*/
function checkCollision() {
// Lizard fixed position:
const lizardTopY = gameArea.clientHeight - LIZARD_HEAD_HEIGHT;
const lizardCenterX = gameArea.clientWidth / 2;
// Fly center position
const flyCenterX = flyX + (fly.clientWidth / 2);
const flyCenterY = flyY + (fly.clientHeight / 2);
// 1. Check if the fly is within the horizontal tolerance (must be above the head)
const distanceX = Math.abs(lizardCenterX - flyCenterX);
const isDirectlyAbove = distanceX < LIZARD_X_TOLERANCE;
if (!isDirectlyAbove) return;
// 2. Check if the fly is within the vertical reach of the tongue
// Distance Y is measured from the top of the lizard head to the fly's center
const distanceY = lizardTopY - flyCenterY;
if (distanceY >= 0 && distanceY <= CATCH_DISTANCE) {
// SUCCESS: Collision detected!
score++;
scoreDisplay.textContent = `Score: ${score}`;
showMessage("Caught!", 700);
// Move the fly immediately
moveFly();
}
}
/**
* Handles the main game action: flicking the tongue.
*/
function flickTongue() {
if (!gameRunning) return startGame();
// 1. Extend the tongue (CSS transition handles the speed)
tongue.classList.add('flicked');
// 2. Check for collision immediately after extension
checkCollision();
// 3. Retract the tongue after the duration
setTimeout(() => {
tongue.classList.remove('flicked');
}, TONGUE_FLICK_DURATION_MS);
}
/**
* Starts the game and initializes state.
*/
function startGame() {
gameRunning = true;
score = 0;
scoreDisplay.textContent = `Score: ${score}`;
messageBox.style.display = 'none';
flickButton.textContent = 'FLICK TONGUE';
// Start the fly movement loop
moveFly();
fly.style.display = 'block';
if (flyInterval) clearInterval(flyInterval);
flyInterval = setInterval(moveFly, FLY_MOVEMENT_INTERVAL_MS);
}
/**
* Event Listeners
*/
// Use the button for desktop click/mobile tap
flickButton.addEventListener('click', flickTongue);
// Allow tapping anywhere on the game area or screen for mobile responsiveness
gameArea.addEventListener('click', flickTongue);
// Initial call to set up the start screen
fly.style.display = 'none';
showMessage("Tap or Click to Play!", 99999); // Show until the first tap
// Add a safety check for canvas resizing (re-centering)
window.addEventListener('resize', () => {
// Re-run moveFly to ensure the current fly position is within the new bounds
if(gameRunning) {
moveFly();
}
});
</script>
</body>
</html>