Skip to content

Commit 2666937

Browse files
Merge pull request #110 from EiJackGH/palette/mario-game-scrolling-fix-5245249649879977107
🎨 Palette: Prevent page scrolling when playing Mario game
2 parents c6cd15e + 1ef5319 commit 2666937

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

.Jules/palette.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
## 2024-05-20 - Dynamic Progress Bar for Quiet CLI Tasks
99
**Learning:** For long-running CLI processes that suppress verbose logging (e.g., `--quiet`), users lose system status visibility.
1010
**Action:** Implemented a dynamic progress bar using `\r` and `flush=True`, conditional on `sys.stdout.isatty()`, to provide status feedback without polluting non-interactive logs.
11+
12+
## 2025-03-23 - Game Key Scrolling
13+
**Learning:** Browsers natively scroll the page when users press Space or Arrow keys. When building a web-based game, this creates a frustrating UX where the game viewport jumps around while playing.
14+
**Action:** Always call `e.preventDefault()` on keydown events for typical game controls ("Space", "ArrowUp", etc.) when the focus is on a game container or the body.

src/views/mario-game.njk

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@
7272
let score = 0;
7373
7474
// Jump mechanic
75-
document.addEventListener("keydown", () => {
75+
document.addEventListener("keydown", (e) => {
76+
// Prevent default page scrolling for typical game keys
77+
if (["Space", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.code)) {
78+
e.preventDefault();
79+
}
80+
81+
// Only jump on Space or Up Arrow
82+
if (e.code !== "Space" && e.code !== "ArrowUp") return;
83+
7684
if (jumping) return;
7785
7886
jumping = true;

0 commit comments

Comments
 (0)