Spaces:
Running
Running
File size: 16,351 Bytes
d5eb47d 76dd671 d5eb47d fa8ab60 d5eb47d 3420d40 e53222b 1db5e79 e53222b 1db5e79 c11f9dd 1db5e79 fa8ab60 1db5e79 d40fa2e dfc0c3a c11f9dd dfc0c3a e53222b dfc0c3a e53222b dfc0c3a e53222b dfc0c3a 1db5e79 c11f9dd e53222b 1db5e79 e53222b 1db5e79 d09f5b9 1db5e79 13dfc7c 1db5e79 e53222b 1db5e79 d40fa2e 1db5e79 c11f9dd 1db5e79 fa8ab60 1db5e79 dfc0c3a 1db5e79 d40fa2e 1db5e79 e53222b dfc0c3a 1db5e79 fa8ab60 c11f9dd 1db5e79 dfc0c3a 1db5e79 dfc0c3a 1db5e79 e53222b c11f9dd 1db5e79 d40fa2e 1db5e79 e53222b 13dfc7c 1db5e79 fa8ab60 dfc0c3a e53222b 13dfc7c 1db5e79 d40fa2e 1db5e79 dfc0c3a e53222b fa8ab60 1db5e79 d40fa2e 1db5e79 dfc0c3a 1db5e79 d40fa2e 1db5e79 d5eb47d d40fa2e 1db5e79 e53222b 1db5e79 dfc0c3a e53222b 1db5e79 dfc0c3a 1db5e79 e53222b 1db5e79 d40fa2e 1db5e79 dfc0c3a fa8ab60 1db5e79 e53222b fa8ab60 dfc0c3a 7ed10fc fa8ab60 dfc0c3a fa8ab60 e53222b 1db5e79 d5eb47d dfc0c3a 76dd671 dfc0c3a e53222b dfc0c3a d5eb47d 3420d40 76dd671 d5eb47d 3420d40 37913c3 1db5e79 37913c3 3420d40 37913c3 3420d40 37913c3 3420d40 37913c3 3420d40 796f391 3420d40 37913c3 3420d40 37913c3 3420d40 37913c3 3420d40 76dd671 3420d40 37913c3 7ed10fc 1db5e79 80cb31b 37913c3 3420d40 1db5e79 3420d40 1db5e79 37913c3 3420d40 37913c3 1db5e79 3420d40 1db5e79 37913c3 3420d40 37913c3 3420d40 d09f5b9 37913c3 3420d40 d09f5b9 3420d40 d09f5b9 3420d40 d40fa2e 3420d40 37913c3 3420d40 d09f5b9 37913c3 1db5e79 3420d40 7ed10fc 37913c3 3420d40 7ed10fc 3420d40 37913c3 3420d40 d40fa2e 37913c3 d40fa2e 3420d40 d09f5b9 37913c3 13dfc7c 37913c3 13dfc7c e53222b 3420d40 13dfc7c 37913c3 3420d40 37913c3 3420d40 37913c3 d5eb47d e53222b 37913c3 3420d40 e53222b 3420d40 37913c3 3420d40 37913c3 3420d40 37913c3 3420d40 e53222b 3420d40 e53222b 3420d40 e53222b 37913c3 3420d40 cc1776b 3420d40 d5eb47d 37913c3 3420d40 37913c3 3420d40 37913c3 3420d40 1e1990f 3420d40 796f391 37913c3 3420d40 cc1776b 37913c3 3420d40 d5eb47d |
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🍎 Apple Merge</title>
<style>
body {
font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
overflow: hidden;
user-select: none;
}
#header {
display: flex;
justify-content: center;
align-items: center;
width: 90vw;
max-width: 90vh;
padding: 10px;
margin-bottom: 10px;
}
#score-display {
font-size: 1.75rem;
font-weight: 600;
color: #333;
margin: 0;
}
#combinable-pairs {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.25rem;
font-weight: 600;
color: #555;
z-index: 20;
}
#game-container {
position: relative;
width: 90vw;
height: 90vw;
max-width: 90vh;
max-height: 90vh;
background-color: #a7d9ed;
border: 2px solid #333;
border-radius: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
touch-action: none;
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(10, 1fr);
gap: 3px;
}
.grid-cell {
width: 100%;
height: 100%;
box-sizing: border-box;
position: relative;
border-radius: 8px;
}
#selection-box {
position: absolute;
border: 2px dashed blue;
background-color: rgba(0, 0, 255, 0.2);
display: none;
z-index: 10;
pointer-events: none;
border-radius: 7px;
}
.apple {
width: 85%;
height: 85%;
display: flex;
align-items: center;
justify-content: center;
transition: transform 0.1s ease-out, filter 0.1s ease-out;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 50%;
box-sizing: border-box;
font-size: calc(2em + 1.5vw);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 2;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.apple span {
font-size: 0.5em;
color: #000;
font-weight: bold;
z-index: 3;
pointer-events: none;
user-select: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-shadow: 0px 1px 3px #fff;
}
.apple.selected {
transform: translate(-50%, -50%) scale(1.1);
filter: brightness(1.2);
z-index: 4;
}
.apple.removing {
animation: confettiExplode 0.6s ease-out forwards;
}
@keyframes confettiExplode {
0% { opacity: 1; transform: scale(1) rotate(0); }
100% {
opacity: 0;
transform: scale(var(--random-scale))
translateX(calc(var(--random-x) * 40px))
translateY(calc(var(--random-y) * 40px))
rotate(calc(var(--random-rotate) * 360deg));
}
}
#game-over-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: none; /* Initially hidden */
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
font-size: 42px;
z-index: 30;
border-radius: 15px;
}
#game-over-screen button {
margin-top: 25px;
padding: 12px 24px;
font-size: 24px;
cursor: pointer;
background-color: #007aff; /* Apple blue */
color: white;
border: none;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
transition: background-color: 0.2s ease;
}
#game-over-screen button:hover {
background-color: #0062cc;
}
</style>
</head>
<body>
<div id="header">
<div id="score-display">Score: 0</div>
</div>
<div id="combinable-pairs">Pairs: 0</div>
<div id="game-container">
<div id="selection-box"></div>
<div id="game-over-screen">
<div>No More Moves!</div>
<button onclick="restartGame()">Restart</button>
</div>
</div>
<script>
const gameContainer = document.getElementById('game-container');
const selectionBox = document.getElementById('selection-box');
const scoreDisplay = document.getElementById('score-display');
const gameOverScreen = document.getElementById('game-over-screen');
const combinablePairsDisplay = document.getElementById('combinable-pairs');
const gridSize = 10;
let startCell = null;
let isSelecting = false;
let selectedApples = [];
let score = 0;
let isMouseDown = false;
let lastTouchEnd = 0;
// Helper function to get relative coordinates, handling both mouse and touch events.
function getRelativeCoordinates(event, element) {
const rect = element.getBoundingClientRect();
const clientX = event.clientX || (event.touches && event.touches[0].clientX);
const clientY = event.clientY || (event.touches && event.touches[0].clientY);
if (clientX === undefined || clientY === undefined) {
return null;
}
return { x: clientX - rect.left, y: clientY - rect.top };
}
// Helper function to get the row and column of a cell from coordinates.
function getCellFromCoordinates(coords) {
if (!coords) return null;
const cellSize = gameContainer.offsetWidth / gridSize;
const col = Math.floor(coords.x / cellSize);
const row = Math.floor(coords.y / cellSize);
if (row >= 0 && row < gridSize && col >= 0 && col < gridSize) {
return { row, col };
}
return null;
}
// Creates an apple with a random number (1-9).
function createApple(row, col) {
const apple = document.createElement('div');
apple.classList.add('apple');
const randomNumber = Math.floor(Math.random() * 9) + 1;
apple.innerHTML = `🍎<span>${randomNumber}</span>`;
apple.dataset.value = randomNumber;
apple.dataset.row = row;
apple.dataset.col = col;
return apple;
}
// Populates the grid with apples.
function populateGrid() {
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
const cell = document.createElement('div');
cell.classList.add('grid-cell');
cell.dataset.row = row;
cell.dataset.col = col;
cell.addEventListener('touchstart', preventZoom, { passive: false }); // Prevent zoom on double-tap
const apple = createApple(row, col);
cell.appendChild(apple);
gameContainer.appendChild(cell);
}
}
updateCombinablePairs();
}
// Prevent page zoom on touch devices
function preventZoom(e) {
if (e.touches && e.touches.length > 1) {
e.preventDefault(); return;
}
if (e.timeStamp - lastTouchEnd < 300) { e.preventDefault(); }
}
// Handles the start of a selection (mousedown or touchstart).
function handleSelectionStart(event) {
if (event.type === 'touchend') { lastTouchEnd = event.timeStamp; }
const coords = getRelativeCoordinates(event, gameContainer);
const cell = getCellFromCoordinates(coords);
if (!cell) return;
// Ensure we're starting on an apple.
const elementAtPoint = document.elementFromPoint(
event.clientX || (event.touches && event.touches[0].clientX),
event.clientY || (event.touches && event.touches[0].clientY)
);
if (!elementAtPoint || !elementAtPoint.classList.contains('apple')) {
return;
}
isMouseDown = true;
startCell = cell;
isSelecting = true;
deselectAllApples();
// Set initial size and position of the selection box.
const cellSize = gameContainer.offsetWidth / gridSize;
selectionBox.style.left = `${startCell.col * cellSize}px`;
selectionBox.style.top = `${startCell.row * cellSize}px`;
selectionBox.style.width = `${cellSize}px`;
selectionBox.style.height = `${cellSize}px`;
selectionBox.style.display = 'block';
selectApplesInBox();
event.preventDefault(); // Prevent other events like scrolling.
}
// Handles mouse/touch movement during selection.
function handleSelectionMove(event) {
if (!isSelecting || !isMouseDown) return;
const coords = getRelativeCoordinates(event, gameContainer);
if (!coords) return;
const currentCell = getCellFromCoordinates(coords);
if (!currentCell) return;
const cellSize = gameContainer.offsetWidth / gridSize;
// Calculate top-left and bottom-right corners of the selection box.
const top = Math.min(startCell.row, currentCell.row);
const left = Math.min(startCell.col, currentCell.col);
const bottom = Math.max(startCell.row, currentCell.row);
const right = Math.max(startCell.col, currentCell.col);
// Update selection box size and position.
selectionBox.style.top = `${top * cellSize}px`;
selectionBox.style.left = `${left * cellSize}px`;
selectionBox.style.width = `${(right - left + 1) * cellSize}px`;
selectionBox.style.height = `${(bottom - top + 1) * cellSize}px`;
selectApplesInBox();
event.preventDefault();
}
// Handles the end of a selection (mouseup or touchend).
function handleSelectionEnd(event) {
if (event.type === 'touchend') { lastTouchEnd = event.timeStamp; }
if (!isSelecting) return;
isMouseDown = false;
isSelecting = false;
selectionBox.style.display = 'none'; // Hide selection box.
checkSum(); // Check the sum of selected apples.
startCell = null; // Reset startCell.
event.preventDefault();
}
function selectApplesInBox() {
const apples = document.querySelectorAll('.apple');
const selectionRect = selectionBox.getBoundingClientRect(); // Selection box rect
apples.forEach(apple => {
const appleRect = apple.getBoundingClientRect();
// Calculate the center of the apple.
const appleCenterX = appleRect.left + appleRect.width / 2;
const appleCenterY = appleRect.top + appleRect.height / 2;
// Check if the apple's center is within the selection box.
const isInside = appleCenterX >= selectionRect.left &&
appleCenterX <= selectionRect.right &&
appleCenterY >= selectionRect.top &&
appleCenterY <= selectionRect.bottom;
if (isInside) {
if (!selectedApples.includes(apple)) {
apple.classList.add('selected');
selectedApples.push(apple);
}
} else {
apple.classList.remove('selected');
selectedApples = selectedApples.filter(a => a !== apple);
}
});
}
// Deselects all apples.
function deselectAllApples() {
selectedApples.forEach(apple => apple.classList.remove('selected'));
selectedApples = [];
}
// Checks if the sum of selected apples is 10. If so, removes them and updates the score.
function checkSum() {
let sum = 0;
selectedApples.forEach(apple => sum += parseInt(apple.dataset.value));
if (sum === 10) {
score += selectedApples.length;
scoreDisplay.textContent = `Score: ${score}`;
selectedApples.forEach(apple => {
apple.classList.add('removing');
// Add random animation properties
apple.style.setProperty('--random-x', Math.random() * 2 - 1); // -1 to 1
apple.style.setProperty('--random-y', Math.random() * 2 - 1);
apple.style.setProperty('--random-rotate', Math.random() * 2 - 1);
apple.style.setProperty('--random-scale', Math.random() * 1 + 0.5); // 0.5 to 1.5
// Remove the apple after the animation finishes.
apple.addEventListener('animationend', () => {
const parentCell = apple.parentElement;
if (parentCell && parentCell.classList.contains('grid-cell')) {
parentCell.removeChild(apple);
updateCombinablePairs(); // Check for game over after removing apples.
}
});
});
selectedApples = []; // Clear the selection.
} else {
deselectAllApples(); // Deselect if the sum is not 10.
}
}
// Checks if there are any combinable pairs left on the board.
function hasCombinablePairs() {
const apples = document.querySelectorAll('.apple');
const values = Array.from(apples).map(apple => parseInt(apple.dataset.value));
for (let i = 0; i < values.length; i++) {
for (let j = i + 1; j < values.length; j++) {
if (values[i] + values[j] === 10) {
return true; // Found a pair that sums to 10.
}
}
}
return false; // No pairs found.
}
// Updates the display of combinable pairs and checks for game over.
function updateCombinablePairs() {
const apples = document.querySelectorAll('.apple');
const values = Array.from(apples).map(apple => parseInt(apple.dataset.value));
let count = 0;
for (let i = 0; i < values.length; i++) {
for (let j = i + 1; j < values.length; j++) {
if (values[i] + values[j] === 10) {
count++;
}
}
}
combinablePairsDisplay.textContent = `Pairs: ${count}`;
// Check for game over (no apples left or no combinable pairs).
if (count === 0 && apples.length > 0) {
gameOverScreen.style.display = "flex";
}
}
// Starts a new game.
function startGame() {
populateGrid();
}
// Restarts the game.
function restartGame() {
score = 0;
scoreDisplay.textContent = "Score: 0";
// Clear existing apples.
while (gameContainer.firstChild) {
gameContainer.removeChild(gameContainer.firstChild);
}
gameOverScreen.style.display = "none";
startGame();
}
// Event listeners for mouse and touch events.
gameContainer.addEventListener('mousedown', handleSelectionStart);
gameContainer.addEventListener('mousemove', handleSelectionMove);
gameContainer.addEventListener('mouseup', handleSelectionEnd);
gameContainer.addEventListener('touchstart', handleSelectionStart);
gameContainer.addEventListener('touchmove', handleSelectionMove);
gameContainer.addEventListener('touchend', handleSelectionEnd);
// Start the game when the page loads.
startGame();
</script>
</body>
</html> |