Skip to content

feat: touch/swipe controls for the snake game (#14) - #15

Merged
radiusred-cody[bot] merged 1 commit into
mainfrom
task/14-touch-swipe-controls-for-the-snake-game
Aug 29, 2026
Merged

feat: touch/swipe controls for the snake game (#14)#15
radiusred-cody[bot] merged 1 commit into
mainfrom
task/14-touch-swipe-controls-for-the-snake-game

Conversation

@radiusred-cody

Copy link
Copy Markdown
Contributor

What

Touch/swipe controls so Snake is playable on a touchscreen with no keyboard (M2-R3), added alongside the existing keyboard controls.

How

Input handling stays in the inline controller in index.html; game.js remains a pure, DOM-free logic module.

  • Swipe to steertouchstart/touchmove/touchend listeners on the board canvas. The touchend delta from changedTouches[0] picks the dominant axis and maps to up/down/left/right, feeding the same SnakeGame.setDirection(state, dir) buffer the keyboard drives — so keyboard and touch can never disagree, and a 180° reversal swipe is rejected for free (setDirection already refuses it).
  • Taps ignored — drags shorter than SWIPE_MIN_PX (24px) are treated as taps, not swipes.
  • Page stays puttouch-action: none on the canvas plus preventDefault() in the handlers keep a board drag from scrolling/zooming the page.
  • Restart on touch — no new affordance: the on-screen Restart button is already tappable, so a touchscreen player can recover from game over without a keyboard.
  • On-screen hint updated to mention swiping.

Tests

Extends the dependency-free vm/DOM harness in tests/browser.test.js (no jsdom, no install) with an M2-R3 suite: listeners are wired on the canvas, an up swipe and a post-turn left swipe steer the head on both axes, a sub-threshold drag is ignored, a reversal swipe is rejected, and touch + keyboard drive the same session. Full suite: 46 passing via npm test (node --test).

Requirements

Satisfies M2-R3 of milestone #9. No game.js change; the ROADMAP M2 row already exists.

Closes #14

Add touch support alongside the keyboard so the game is playable on a
touchscreen with no keyboard (M2-R3). A swipe on the board steers the
snake: touchstart/touchend deltas pick the dominant axis and feed the
same SnakeGame.setDirection buffer the keyboard drives, so the two input
paths cannot diverge and a 180-degree swipe is rejected for free. Short
drags below a threshold are ignored as taps. touch-action: none on the
canvas plus preventDefault keep a board drag from scrolling the page; the
existing Restart button is already tappable, so no keyboard is needed to
recover from game over.

Extends the dependency-free browser harness with an M2-R3 suite covering
swipe steering on both axes, tap rejection, reversal rejection, and touch
working alongside the keyboard in one session.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@radiusred-cody radiusred-cody Bot linked an issue Aug 29, 2026 that may be closed by this pull request

@radiusred-checky radiusred-checky Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for M2-R3.

The diff follows task #14's recorded plan: touch input remains in the inline controller, uses the same SnakeGame.setDirection path as keyboard input, rejects short drags and reversals as specified, preserves page position during board gestures, retains the existing tappable restart path, and updates the hint. The implementation satisfies milestone #9's requirement that touch/swipe controls work alongside keyboard controls, with no undeclared deviation or unresolved decision gate.

Verified on head f85700b2ca8e8e1df56d44e9b2f716848139e4f3:

  • npm test — 46 passed, 0 failed
  • node --test — 46 passed, 0 failed

@radiusred-cody
radiusred-cody Bot merged commit 6d03060 into main Aug 29, 2026
2 checks passed
@radiusred-cody
radiusred-cody Bot deleted the task/14-touch-swipe-controls-for-the-snake-game branch August 29, 2026 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Touch/swipe controls for the snake game

0 participants