Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Walk Play Web App: Build a Simple Walking Game

· 4 min read · Home Tech

In this article
  1. What Is a Walk‑Play Web App
  2. Core Technologies
  3. Building the App
  4. Worked Example
  5. Common Pitfalls
  6. Next Steps

A walk‑play web app is a browser‑based application that tracks a user’s walking motion and turns it into interactive gameplay. It works as long as the device supports geolocation or motion sensors and runs in a standard web browser.

What Is a Walk‑Play Web App

It combines two functions: measuring physical movement (steps, distance, speed) and using that data to control game elements such as a character, obstacles, or scores. The app does not require installation from an app store; users open a URL and the browser handles sensor access.

Core Technologies

The essential web APIs are:

  • Geolocation API – provides latitude, longitude, and optional accuracy for outdoor walking.
  • DeviceMotion and DeviceOrientation APIs – give acceleration and rotation data for indoor walking or treadmill use.
  • Canvas or WebGL – renders the game graphics in real time.
  • Web Audio – adds sound effects that react to step events.
  • Service Workers – enable offline caching so the game works without a constant internet connection.

All of these APIs are part of modern browsers on desktop, Android, and iOS. Permissions are requested once per session; the user can revoke them at any time.

Building the App

Follow these steps to create a minimal walk‑play prototype.

  1. Create an HTML file with a <canvas> element sized to the viewport.
  2. Write JavaScript that asks for geolocation permission using navigator.geolocation.watchPosition. Set maximumAge to 0 and enableHighAccuracy to true for the most precise step count.
  3. When a new position arrives, calculate the distance from the previous point using the haversine formula. Convert the distance to steps by assuming an average step length of 0.78 m (about 2.5 ft).
  4. Increment a step counter and update the game state. For example, move a sprite forward by 5 px per step.
  5. Render the updated scene on the canvas each animation frame with requestAnimationFrame.
  6. Optionally, add a sound cue with new Audio('step.mp3') each time the counter increases.
  7. Register a Service Worker that caches index.html, the JavaScript file, and any assets so the app loads instantly on repeat visits.

All code runs client‑side; no server is required unless you want to store high scores.

Worked Example

Below is a complete, runnable snippet that tracks a 10‑minute walk and moves a ball across the screen. The example assumes a step length of 0.78 m and a target of 1 km (about 1,282 steps).

/* HTML */
<canvas id="game" width="400" height="200"></canvas>

/* JavaScript */
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
let prevPos = null;
let steps = 0;
const stepLength = 0.78; // metres per step
const targetSteps = 1282; // roughly 1 km

function drawBall(x) {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();
  ctx.arc(x, canvas.height/2, 15, 0, Math.PI*2);
  ctx.fillStyle = '#4A90E2';
  ctx.fill();
}

function updatePosition(position) {
  if (prevPos) {
    const R = 6371000; // Earth radius in metres
    const dLat = (position.coords.latitude - prevPos.latitude) * Math.PI/180;
    const dLon = (position.coords.longitude - prevPos.longitude) * Math.PI/180;
    const a = Math.sin(dLat/2)**2 + Math.cos(prevPos.latitude*Math.PI/180) * Math.cos(position.coords.latitude*Math.PI/180) * Math.sin(dLon/2)**2;
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    const distance = R * c; // metres travelled since last update
    const newSteps = Math.floor(distance / stepLength);
    steps += newSteps;
    const progress = Math.min(steps/targetSteps, 1);
    drawBall(progress * canvas.width);
    console.log(`Steps: ${steps}`);
  }
  prevPos = {
    latitude: position.coords.latitude,
    longitude: position.coords.longitude
  };
}

if ('geolocation' in navigator) {
  navigator.geolocation.watchPosition(updatePosition, console.error, {
    enableHighAccuracy: true,
    maximumAge: 0,
    timeout: 10000
  });
} else {
  alert('Geolocation not supported on this device');
}

When you run the snippet on a mobile device and walk outdoors, the ball moves from left to right. After roughly 1 km, the ball reaches the right edge, indicating the goal is met.

Common Pitfalls

Misconception: You must build a native app to read accelerometer data. Reality: Modern browsers expose DeviceMotionEvent, so a web page can receive acceleration values without leaving the browser.

Other frequent issues:

  • Permission denial: If the user blocks location, the game cannot track steps. Always provide a fallback message and a button to retry permission.
  • Inaccurate step length: Using a fixed 0.78 m works for average adults but may over‑ or under‑estimate for children or tall individuals. Offer a settings field where users can input their own step length.
  • Battery drain: High‑frequency geolocation updates consume power. Limit watchPosition to a reasonable maximumAge (e.g., 5 seconds) and stop tracking when the game ends.
  • Background throttling: Some browsers pause timers when the tab is hidden. Use the Page Visibility API to pause the game gracefully.

Next Steps

After you have a working prototype, consider these enhancements:

  • Add a leaderboard stored in a simple Firebase Realtime Database or a static JSON file on a CDN.
  • Introduce power‑ups that appear after every 500 steps, encouraging longer walks.
  • Implement a night‑mode color scheme that switches automatically based on the device’s prefers-color-scheme setting.
  • Test on multiple browsers and devices. Use the navigator.userAgent string to detect unsupported browsers and show a clear message.
  • Publish the app via GitHub Pages or any static‑host service. The URL can be shared directly, and the Service Worker will keep it usable offline.

When you deploy, verify that the HTTPS requirement for sensor APIs is met; most browsers block geolocation on insecure origins. If you need to debug sensor data, open the browser console and watch the position.coords object for latitude, longitude, and accuracy values.

By following the steps above, you can turn a simple walk into an engaging interactive experience without writing native code. The core idea is to map real‑world motion to on‑screen movement, then layer game mechanics that reward continued activity.