Course 08 · Lesson 8 of 11
Games on Mobile
More than 60% of casual browser gaming happens on mobile a game that only works on desktop misses most of its potential audience
The Three Mobile Problems
Mobile game testing reveals the same three problems in almost every project.
No touch controls means keyboard-based games like Snake and Breakout use arrow keys but mobile phones have no arrow keys so the game cannot be played at all.
Layout breaks on small screens means canvas games sized for desktop overflow the viewport on mobile and players have to scroll sideways or the canvas gets clipped.
Tap accidentally triggers hover states means CSS hover effects designed for mouse cursors sometimes fire once on touch and stay stuck in the hover state permanently.
Each has a fix. Include the relevant fix in your prompt upfront because retrofitting touch controls is harder than designing them in from the start.
Fix 1: Touch Controls for Directional Games
For Snake, Breakout, Pong, and any game using arrow keys add a swipe-to-control system. Add this to your game prompt:
- Add swipe gesture controls for mobile. Detect touch events on the canvas element.
- touchstart: record startX = e.touches[0].clientX and startY = e.touches[0].clientY
- touchend: record endX = e.changedTouches[0].clientX and endY = e.changedTouches[0].clientY
- Calculate delta: dx = endX minus startX, dy = endY minus startY
- Determine direction: if Math.abs(dx) is greater than Math.abs(dy) it is a horizontal swipe
- where dx greater than 0 equals Right and dx less than 0 equals Left.
- Otherwise it is a vertical swipe where dy greater than 0 equals Down and dy less than 0 equals Up.
- Minimum swipe distance: 30px — ignore smaller movements as accidental taps.
- Apply the same direction-change logic as keyboard input.
- Also add visible on-screen directional buttons below the canvas for players who
- prefer tapping over swiping.
- Four arrow buttons in a d-pad layout where each button calls the same direction-change function.
- Size: 60 by 60px, centered, with 8px gaps.
- Show these buttons only on touch devices using: if ('ontouchstart' in window)
Fix 2: Responsive Canvas Sizing
For canvas games the canvas size should adapt to the screen. Add this to your prompt:
- Make the canvas responsive on mobile.
- On desktop at 600px or wider screen width: canvas is fixed at [YOUR CANVAS SIZE e.g. 400 by 400].
- On mobile below 600px screen width: canvas size equals Math.min(window.innerWidth minus 32, 400)
- which fills screen width with 16px margin each side up to a maximum of 400px.
- All game coordinates scale proportionally. Define a scale variable equal to
- canvasActualSize divided by CANVAS_LOGICAL_SIZE. Multiply all drawing coordinates
- by scale in the render function. The logical game grid stays 20 by 20 and only the
- visual rendering scales.
- Add a resize event listener on window that recalculates scale and redraws.
Fix 3: Touch Targets for Card and Word Games
Card games and word games use clickable elements. On mobile these need to be large enough to tap accurately.
The minimum tap target size is 44 by 44px. Apple's HIG and Google's Material Design both specify this as the minimum for comfortable tapping.
Add this to DOM-based game prompts:
- All interactive elements including cards, keyboard keys, and buttons must be at
- least 44 by 44px on mobile.
On screens under 480px width:
- Memory Match cards: reduce to 70 by 70px and tighten gaps to 6px.
- Word game tiles: reduce to 52 by 52px.
- On-screen keyboard keys: ensure minimum height of 48px.
- All tap targets add cursor: pointer which is helpful even on touch devices.
Preventing Common Mobile Issues
Prevent scroll during swipe
When a player swipes to control the game the browser also interprets it as a page scroll. Fix with this addition to your prompt:
On the game canvas add e.preventDefault() inside touchmove and touchstart event handlers.
Also add passive: false to the event listener options:
canvas.addEventListener('touchstart', handler, { passive: false })
Prevent double-tap zoom
Double-tapping fast on mobile zooms the page which breaks canvas games. Fix with this:
- Add to the meta viewport tag: minimum-scale=1, maximum-scale=1, user-scalable=no.
- Also add touch-action: none on the canvas element.
Prevent text selection on long press
On mobile, long-pressing often triggers text selection UI. Fix with this:
- Add user-select: none and -webkit-user-select: none to the game container CSS
- to prevent selection interference during gameplay.
The Mobile Game Meta Tag
Every game's index.html should have this viewport meta tag for correct mobile scaling:
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
Without it mobile browsers apply a default zoom that makes the game look like a tiny desktop site on a huge screen. Include this in every game prompt automatically because it costs nothing and prevents the most common mobile layout problem.
The Mobile Testing Checklist
After building any game test on mobile before sharing the link.
Open Chrome and press F12. Click the phone icon at the top of the DevTools panel. Select iPhone 12 Pro from the dropdown. Refresh the game.
Then check these things. The game fits within the screen width with no horizontal scroll. All text is legible and not tiny. Buttons and cards are tappable and not too small. Swipe controls work for Snake and arcade games. On-screen buttons are visible and functional. No accidental scrolling happens when swiping to play. Win and lose overlays are readable on a small screen.
For the Word Game specifically check that all 26 letter keys are reachable and not too small, that the Enter and Backspace keys are wide and obvious, and that no key is hidden behind the mobile browser's address bar.
Adding a Play on Mobile Mode
For games that are genuinely designed for desktop first with complex keyboard controls or a large canvas, add a mobile mode detection with a friendly notice. Add this to your prompt:
- On page load detect mobile using: if (window.innerWidth < 480 || 'ontouchstart' in window)
- If mobile is detected and the game has no touch controls implemented, show a centred
- overlay that says: "This game is best played on desktop. Rotate your device to
- landscape for a better experience." with a "Play Anyway" button that dismisses the overlay.
- This is more honest than serving a broken experience silently.
Do it with other builders
Post your homework. Get unstuck.
Every course ends with homework. Post yours in Claude Codex Mastery, the community this course comes from, and get feedback from Yar and 400+ people building the same way.
Join the community · $29/mo