-
Notifications
You must be signed in to change notification settings - Fork 6
Add simple web-based snake game #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
beta-devin-ai-integration
wants to merge
2
commits into
master
Choose a base branch
from
devin/1775584255-snake-game
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,302 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Snake Game</title> | ||
| <style> | ||
| * { | ||
| margin: 0; | ||
| padding: 0; | ||
| box-sizing: border-box; | ||
| } | ||
|
|
||
| body { | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| min-height: 100vh; | ||
| background: #1a1a2e; | ||
| font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | ||
| color: #eee; | ||
| } | ||
|
|
||
| h1 { | ||
| margin-bottom: 10px; | ||
| font-size: 2rem; | ||
| color: #00d2ff; | ||
| text-shadow: 0 0 10px rgba(0, 210, 255, 0.5); | ||
| } | ||
|
|
||
| #score-board { | ||
| display: flex; | ||
| gap: 30px; | ||
| margin-bottom: 10px; | ||
| font-size: 1.1rem; | ||
| } | ||
|
|
||
| #score-board span { | ||
| color: #00d2ff; | ||
| font-weight: bold; | ||
| } | ||
|
|
||
| canvas { | ||
| border: 2px solid #00d2ff; | ||
| border-radius: 4px; | ||
| box-shadow: 0 0 20px rgba(0, 210, 255, 0.3); | ||
| background: #16213e; | ||
| } | ||
|
|
||
| #controls { | ||
| margin-top: 15px; | ||
| font-size: 0.9rem; | ||
| color: #888; | ||
| text-align: center; | ||
| line-height: 1.6; | ||
| } | ||
|
|
||
| #overlay { | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| pointer-events: none; | ||
| background: rgba(0, 0, 0, 0.5); | ||
| border-radius: 4px; | ||
| display: none; | ||
| } | ||
|
|
||
| #overlay.active { | ||
| display: flex; | ||
| pointer-events: auto; | ||
| } | ||
|
|
||
| #overlay-text { | ||
| font-size: 1.8rem; | ||
| font-weight: bold; | ||
| color: #fff; | ||
| text-shadow: 0 0 15px rgba(0, 210, 255, 0.8); | ||
| } | ||
|
|
||
| #restart-btn { | ||
| margin-top: 12px; | ||
| padding: 10px 28px; | ||
| font-size: 1rem; | ||
| border: 2px solid #00d2ff; | ||
| background: transparent; | ||
| color: #00d2ff; | ||
| border-radius: 6px; | ||
| cursor: pointer; | ||
| display: none; | ||
| transition: background 0.2s, color 0.2s; | ||
| } | ||
|
|
||
| #restart-btn:hover { | ||
| background: #00d2ff; | ||
| color: #1a1a2e; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <h1>Snake Game</h1> | ||
| <div id="score-board"> | ||
| <div>Score: <span id="score">0</span></div> | ||
| <div>High Score: <span id="high-score">0</span></div> | ||
| </div> | ||
|
|
||
| <div style="position: relative; display: inline-block;"> | ||
| <canvas id="game" width="400" height="400"></canvas> | ||
| <div id="overlay"> | ||
| <div id="overlay-text"></div> | ||
| <button id="restart-btn">Play Again</button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div id="controls"> | ||
| Arrow keys or WASD to move<br> | ||
| Press Space or Enter to start / restart | ||
| </div> | ||
|
|
||
| <script> | ||
| const canvas = document.getElementById('game'); | ||
| const ctx = canvas.getContext('2d'); | ||
| const scoreEl = document.getElementById('score'); | ||
| const highScoreEl = document.getElementById('high-score'); | ||
| const overlayEl = document.getElementById('overlay'); | ||
| const overlayText = document.getElementById('overlay-text'); | ||
| const restartBtn = document.getElementById('restart-btn'); | ||
|
|
||
| const GRID = 20; | ||
| const TILE = canvas.width / GRID; | ||
| const TICK_MS = 120; | ||
|
|
||
| let snake, direction, nextDirection, food, score, highScore, running, gameOver, loopId; | ||
|
|
||
| highScore = parseInt(localStorage.getItem('snakeHighScore') || '0', 10); | ||
| highScoreEl.textContent = highScore; | ||
|
|
||
| function init() { | ||
| snake = [ | ||
| { x: 10, y: 10 }, | ||
| { x: 9, y: 10 }, | ||
| { x: 8, y: 10 }, | ||
| ]; | ||
| direction = { x: 1, y: 0 }; | ||
| nextDirection = { x: 1, y: 0 }; | ||
| score = 0; | ||
| scoreEl.textContent = score; | ||
| gameOver = false; | ||
| running = true; | ||
| placeFood(); | ||
| hideOverlay(); | ||
| clearInterval(loopId); | ||
| loopId = setInterval(tick, TICK_MS); | ||
| } | ||
|
|
||
| function placeFood() { | ||
| while (true) { | ||
| food = { | ||
| x: Math.floor(Math.random() * GRID), | ||
| y: Math.floor(Math.random() * GRID), | ||
| }; | ||
| if (!snake.some(s => s.x === food.x && s.y === food.y)) break; | ||
| } | ||
| } | ||
|
|
||
| function tick() { | ||
| if (!running) return; | ||
|
|
||
| direction = { ...nextDirection }; | ||
|
|
||
| const head = { | ||
| x: snake[0].x + direction.x, | ||
| y: snake[0].y + direction.y, | ||
| }; | ||
|
|
||
| // Wall collision | ||
| if (head.x < 0 || head.x >= GRID || head.y < 0 || head.y >= GRID) { | ||
| return endGame(); | ||
| } | ||
|
|
||
| // Self collision | ||
| if (snake.some(s => s.x === head.x && s.y === head.y)) { | ||
| return endGame(); | ||
| } | ||
|
|
||
| snake.unshift(head); | ||
|
|
||
| if (head.x === food.x && head.y === food.y) { | ||
| score++; | ||
| scoreEl.textContent = score; | ||
| if (score > highScore) { | ||
| highScore = score; | ||
| highScoreEl.textContent = highScore; | ||
| localStorage.setItem('snakeHighScore', highScore); | ||
| } | ||
| placeFood(); | ||
| } else { | ||
| snake.pop(); | ||
| } | ||
|
|
||
| draw(); | ||
| } | ||
|
|
||
| function draw() { | ||
| // Background | ||
| ctx.fillStyle = '#16213e'; | ||
| ctx.fillRect(0, 0, canvas.width, canvas.height); | ||
|
|
||
| // Grid lines (subtle) | ||
| ctx.strokeStyle = 'rgba(255,255,255,0.03)'; | ||
| for (let i = 0; i <= GRID; i++) { | ||
| ctx.beginPath(); | ||
| ctx.moveTo(i * TILE, 0); | ||
| ctx.lineTo(i * TILE, canvas.height); | ||
| ctx.stroke(); | ||
| ctx.beginPath(); | ||
| ctx.moveTo(0, i * TILE); | ||
| ctx.lineTo(canvas.width, i * TILE); | ||
| ctx.stroke(); | ||
| } | ||
|
|
||
| // Snake | ||
| snake.forEach((seg, i) => { | ||
| const ratio = 1 - i / snake.length; | ||
| const g = Math.round(180 + 75 * ratio); | ||
| ctx.fillStyle = i === 0 ? '#00ff88' : `rgb(0, ${g}, 100)`; | ||
| ctx.shadowColor = i === 0 ? '#00ff88' : 'transparent'; | ||
| ctx.shadowBlur = i === 0 ? 8 : 0; | ||
| ctx.fillRect(seg.x * TILE + 1, seg.y * TILE + 1, TILE - 2, TILE - 2); | ||
| }); | ||
| ctx.shadowBlur = 0; | ||
|
|
||
| // Food | ||
| ctx.fillStyle = '#ff4757'; | ||
| ctx.shadowColor = '#ff4757'; | ||
| ctx.shadowBlur = 10; | ||
| ctx.beginPath(); | ||
| ctx.arc(food.x * TILE + TILE / 2, food.y * TILE + TILE / 2, TILE / 2 - 2, 0, Math.PI * 2); | ||
| ctx.fill(); | ||
| ctx.shadowBlur = 0; | ||
| } | ||
|
|
||
| function endGame() { | ||
| running = false; | ||
| gameOver = true; | ||
| clearInterval(loopId); | ||
| showOverlay('Game Over!'); | ||
| } | ||
|
|
||
| function showOverlay(msg) { | ||
| overlayEl.classList.add('active'); | ||
| overlayText.textContent = msg; | ||
| restartBtn.style.display = 'inline-block'; | ||
| } | ||
|
|
||
| function hideOverlay() { | ||
| overlayEl.classList.remove('active'); | ||
| restartBtn.style.display = 'none'; | ||
| } | ||
|
|
||
| // Input | ||
| document.addEventListener('keydown', e => { | ||
| const key = e.key.toLowerCase(); | ||
|
|
||
| if ((key === ' ' || key === 'enter') && gameOver) { | ||
| e.preventDefault(); | ||
| init(); | ||
| return; | ||
| } | ||
|
|
||
| if (!running) return; | ||
|
|
||
| switch (key) { | ||
| case 'arrowup': case 'w': | ||
| if (direction.y === 0) nextDirection = { x: 0, y: -1 }; | ||
| break; | ||
| case 'arrowdown': case 's': | ||
| if (direction.y === 0) nextDirection = { x: 0, y: 1 }; | ||
| break; | ||
| case 'arrowleft': case 'a': | ||
| if (direction.x === 0) nextDirection = { x: -1, y: 0 }; | ||
| break; | ||
| case 'arrowright': case 'd': | ||
| if (direction.x === 0) nextDirection = { x: 1, y: 0 }; | ||
| break; | ||
| } | ||
| }); | ||
|
|
||
| restartBtn.addEventListener('click', () => init()); | ||
|
|
||
| // Initial draw & start | ||
| init(); | ||
| draw(); | ||
| </script> | ||
| </body> | ||
| </html> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Overlay is mispositioned because viewport-relative coordinates are used for an absolutely-positioned child
The
#overlayisposition: absoluteinside aposition: relativeparent (snake-game.html:106), so itsleft/topare interpreted relative to that parent. However,showOverlayusescanvas.getBoundingClientRect()which returns viewport-relative coordinates and assigns them directly asleftandtop. This offsets the overlay by the distance of the container from the viewport origin, causing the "Game Over" overlay and "Play Again" button to appear in the wrong position (shifted far away from the canvas).Was this helpful? React with 👍 or 👎 to provide feedback.
Debug
Playground
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already fixed in commit 202058d — removed the
getBoundingClientRect()approach entirely and replaced it with pure CSS positioning (top: 0; left: 0; width: 100%; height: 100%relative to the parent container). The overlay now centers correctly on the canvas.