Add simple web-based snake game#72
Conversation
Co-Authored-By: ahlback.emil+coggitgrant <me@emil.zip>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Co-Authored-By: ahlback.emil+coggitgrant <me@emil.zip>
| function showOverlay(msg) { | ||
| overlayEl.classList.add('active'); | ||
| overlayText.textContent = msg; | ||
| restartBtn.style.display = 'inline-block'; | ||
| } |
There was a problem hiding this comment.
🔴 Overlay is mispositioned because viewport-relative coordinates are used for an absolutely-positioned child
The #overlay is position: absolute inside a position: relative parent (snake-game.html:106), so its left/top are interpreted relative to that parent. However, showOverlay uses canvas.getBoundingClientRect() which returns viewport-relative coordinates and assigns them directly as left and top. 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).
| function showOverlay(msg) { | |
| overlayEl.classList.add('active'); | |
| overlayText.textContent = msg; | |
| restartBtn.style.display = 'inline-block'; | |
| } | |
| overlayEl.style.left = '0px'; | |
| overlayEl.style.top = '0px'; | |
| overlayEl.style.width = rect.width + 'px'; | |
| overlayEl.style.height = rect.height + 'px'; |
Was this helpful? React with 👍 or 👎 to provide feedback.
Debug
There was a problem hiding this comment.
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.
Testing ResultsTested the snake game end-to-end using Playwright via CDP against Chrome, served locally at All 4 tests passed
Playwright test output |
Summary
Adds a self-contained HTML file (
snake-game.html) implementing a classic snake game using Canvas 2D. No build step or dependencies required — just open the file in a browser.Features:
Updates since last revision
getBoundingClientRect()-based JS positioning with pure CSS (top: 0; left: 0; width: 100%; height: 100%relative to parent). The overlay now reliably centers on the canvas regardless of scroll or viewport position.display: none/display: flexthrough the.activeclass.Review & Testing Checklist for Human
snake-game.htmlin a browser and verify movement, food collection, score increment, wall/self collision, and restart all work correctly.Notes
Link to Devin session: https://app.beta.devin.ai/sessions/16acd778c7554d11bf12629942b0fa3b
Requested by: @FOLLGAD