›Platformer

Build a platformer

A platformer is the best first "real" game: movement you can feel within a minute, and a structure — loop, physics, collision, pickups — that every later game reuses. This guide ends with a complete working game in one page you can paste into Studio right now.

Cudic Cookbook · works in Studio and as a published game

The whole game, then the parts

Open Studio, delete the sample code in index.html, and paste this. Press Run — you have a platformer.

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>My platformer</title>
<style>html,body{margin:0;height:100%;background:#12232e;overflow:hidden}
canvas{display:block;width:100vw;height:100vh}</style></head>
<body><canvas id="c"></canvas><script>
var c = document.getElementById('c'), x = c.getContext('2d');
c.width = 960; c.height = 540;

var plats = [
  {x:0,   y:500, w:960, h:40},   // floor
  {x:170, y:395, w:150, h:18},
  {x:430, y:320, w:170, h:18},
  {x:670, y:250, w:170, h:18}
];
var coins = [{x:245,y:355},{x:510,y:280},{x:750,y:210},{x:120,y:460},{x:880,y:460}];
var p = {x:60, y:440, w:34, h:44, vx:0, vy:0, ground:false};
var keys = {}, got = 0, t0 = performance.now();

addEventListener('keydown', function (e) { keys[e.key] = true;
  if (['ArrowLeft','ArrowRight','ArrowUp',' '].indexOf(e.key) >= 0) e.preventDefault(); });
addEventListener('keyup', function (e) { keys[e.key] = false; });

function hit(a, b) {
  return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
}

function frame(now) {
  var dt = Math.min((now - t0) / 1000, 0.033); t0 = now;
  var dir = (keys.ArrowRight || keys.d ? 1 : 0) - (keys.ArrowLeft || keys.a ? 1 : 0);
  p.vx = dir * 260;
  p.vy += 1500 * dt;                       // gravity

  p.x += p.vx * dt;                        // walls: stop and slide
  for (var i = 0; i < plats.length; i++) if (hit(p, plats[i])) {
    p.x = p.vx > 0 ? plats[i].x - p.w : plats[i].x + plats[i].w;
  }
  p.ground = false;
  p.y += p.vy * dt;                        // tops and bottoms
  for (var j = 0; j < plats.length; j++) if (hit(p, plats[j])) {
    if (p.vy > 0) { p.y = plats[j].y - p.h; p.ground = true; }
    p.vy = 0;
  }
  if (p.ground && (keys[' '] || keys.ArrowUp)) p.vy = -620;
  p.x = Math.max(0, Math.min(960 - p.w, p.x));

  for (var k = 0; k < coins.length; k++) if (coins[k]) {       // pickups
    var dx = (p.x + p.w / 2) - coins[k].x, dy = (p.y + p.h / 2) - coins[k].y;
    if (dx * dx + dy * dy < 26 * 26) { got++; coins[k] = null; }
  }

  x.fillStyle = '#12232e'; x.fillRect(0, 0, 960, 540);
  x.fillStyle = '#1f4b5f';
  for (var w = 0; w < plats.length; w++) x.fillRect(plats[w].x, plats[w].y, plats[w].w, plats[w].h);
  x.fillStyle = '#ffd166';
  for (var ci = 0; ci < coins.length; ci++) if (coins[ci]) x.fillRect(coins[ci].x - 7, coins[ci].y - 7, 14, 14);
  x.fillStyle = '#ff8c1a'; x.fillRect(p.x, p.y, p.w, p.h);
  x.fillStyle = '#fff'; x.fillText(got + ' / ' + coins.length + ' coins', 14, 22);

  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script></body></html>

Paste it into index.html in Studio, press Run in the preview bar, and you're playing. Delete style.css from this project — everything needed is inline.

Why each piece matters

The loop runs on the display's clock

requestAnimationFrame fires up to 60 times a second and pauses when the tab is hidden — setInterval games keep simulating in hidden tabs, which feels broken when you come back. We compute dt (seconds since the last frame) and cap it at 0.033: if the player tabs away for five seconds, physics doesn't teleport them through the floor in one giant step.

Gravity is one line

p.vy += 1500 * dt is "speed increases every second". The -620 jump speed and 1500 gravity are the two numbers to tune if your game feels floaty or sticky — jump height is roughly jump² / (2 × gravity), here about 128px. That's why platforms sit 70–75px apart.

Collision: separate the axes

The trick is moving on X first, checking, then Y, checking again. If you move both axes at once you can't tell landing from wall-touching, and the player sticks to walls. Resolve listing:

  1. Move X → if overlapping a platform, push out sideways.
  2. Move Y → if overlapping and falling, snap to the platform top, set ground = true.
  3. Jump only when ground is true — the anti-fly rule.

Coins with zero art

A pickup is a distance check: if the player's centre is within 26px of a coin, it's collected. Squared distance (dx*dx + dy*dy < 26*26) avoids the sqrt — cheap, and at 60fps it adds up.

Play it on a phone (CudicTouch)

Most people who open your game link do it on a phone, where there are no arrow keys. CudicTouch draws an on-screen D-pad and action buttons over your game and turns taps into ordinary key events, so this platformer works unedited. Add one script tag to your index.html:

<script src="/cudic-touch.js?v=2" data-cudic-touch="auto"></script>

On a laptop nothing changes (the pad only mounts on touch-first screens), on a phone you get the D-pad (arrow keys) plus an A button mapped to Space — which is exactly the jump key this game listens for. Customize it if your game uses different keys:

<script src="/cudic-touch.js?v=2"
        data-cudic-touch="auto:A=Space,B=z,D=wasd"></script>

A/B set button keys (label comes first, the value is the key name — Space, Enter, A, z…), and D=wasd switches the D-pad to WASD for games that use letters instead of arrows. dpad=false hides the D-pad for click-only games. Want an analog feel instead? Add J=on — a drag-anywhere joystick replaces the D-pad, steering through 8 compass sectors so diagonals (up-right, down-left…) are real key pairs, and lifting your finger releases every key. In JavaScript you can also control it yourself:

// mount only on touch devices (same as "auto")
CudicTouch.auto();
// force it on with a custom set
CudicTouch.mount({ buttons: [{ key: 'Space', label: 'JUMP', accent: 'a' }] });
CudicTouch.hide(); CudicTouch.show(); CudicTouch.unmount();

Buttons repeat no auto-fire: a press sends one keydown, a release sends one keyup, exactly like a real key. That's the anti-fly rule at work too — holding A makes the plumber jump once per press, just like Space on a keyboard.

Where to take it next

  • Camera. Make the level wider than the screen and subtract a cam offset when drawing everything.
  • Coyote time. Let jumping work for 100ms after leaving a ledge — it makes controls feel fair.
  • Spikes and resets. Rectangles that reset p to the start, plus a small timer for best-time.
  • Sounds. One Audio object with a short .play() on coin pickup changes the feel completely.

When it plays well: Save, then Publish in Studio — the game goes live in the gallery. Share your build in the forum's Showcase and see if anyone beats your level.