Skip to contentGo to cookie choices
Skip to docs navigation

Build a minimal game in 15 minutes

https://docs.zokogames.com/getting-started/first-game

This page gives you a complete, small game in two files, index.html and game.js. It uses plain JavaScript with no build step. You can upload it as it is: the validator accepts it with no errors or warnings, and every required item of the Preview checklist turns green. Use it to learn the SDK, or copy the parts you need into your own game.

Before you start you need a browser and a way to serve a folder over HTTP (the commands below use Node.js with npx). You only need a studio for step 5; Your first game live in 30 minutes shows how to get one. Words such as build and environment are explained in the glossary.

What the game does#

"Target Tap": the player taps a circle as often as possible in 15 seconds. Each part of the game uses one SDK feature:

In the game SDK call Checklist line it turns green
Page opens SDK.init() SDK initialised
Menu is ready SDK.game.loadingStart(), SDK.game.loadingStop() loadingStop called when the game is ready
Player presses Play SDK.game.gameplayStart() gameplayStart called; First gameplayStart within 20 seconds
Round ends, or the player clicks outside the game SDK.game.gameplayStop() gameplayStop called on pause, menu and game over
Break after each round SDK.ad.requestAd("midgame", ...) A midgame ad was requested; gameplayStop before each ad request
Player presses Play again after the ad SDK.game.gameplayStart() Play resumed after an ad finished
Watch an ad button SDK.ad.requestAd("rewarded", ...) A rewarded ad was requested
New best score SDK.data.setItem(), SDK.game.happytime() Data module used for saves; happytime called at a great moment
Portal mute button SDK.game.settings, SDK.game.addSettingsChangeListener() none; reviewers check it

The lines are worded as on the Preview and QA tab of your game in the console. The public Preview tool uses shorter words for some of them (loadingStop called, Midgame ad requested, Data module used) and has no lines for the ad-break rules; the testing checklist lists both.

1. Create index.html#

Create a folder named target-tap and save this file in it as index.html:

HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Target Tap</title>
  <!-- The SDK first, then your own code. -->
  <script src="https://sdk.zokogames.com/zokogames-sdk-v1.js"></script>
  <style>
    html, body { margin: 0; height: 100%; overflow: hidden; background: #14161f; color: #fff; font: 18px sans-serif; }
    #game { position: relative; height: 100%; touch-action: none; user-select: none; }
    #hud { padding: 12px; }
    #target { position: absolute; width: 64px; height: 64px; border: 0; border-radius: 50%; background: #ffc531; }
    #panel { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; }
    [hidden] { display: none !important; }
    button { font: inherit; padding: 10px 20px; }
  </style>
</head>
<body>
  <div id="game">
    <div id="hud">Score <b id="score">0</b> | Time <b id="time">15</b> | Best <b id="best">0</b></div>
    <button id="target" aria-label="Target" hidden></button>
    <div id="panel">
      <h1>Target Tap</h1>
      <p>Tap the circle as often as you can in 15 seconds.</p>
      <button id="play" disabled>Play</button>
      <button id="reward" hidden>Watch an ad: +5 seconds next round</button>
      <p id="msg"></p>
    </div>
  </div>
  <script src="game.js"></script>
</body>
</html>

The SDK script is loaded in the <head>, before game.js, and every path is relative. The Play button starts disabled and is enabled when the SDK is ready.

2. Create game.js#

Save this file next to index.html as game.js:

JavaScript
// Target Tap: hit the circle as often as you can in 15 seconds.
const SDK = window.ZokoGames && window.ZokoGames.SDK; // undefined if the script was blocked
const $ = (id) => document.getElementById(id);
let ready = false, playing = false, running = false, muted = false, audio = null;
let score = 0, best = 0, timeLeft = 0, bonus = 0, timer = 0;

// Every SDK call goes through here, so the game still runs when the SDK is missing,
// disabled or a call fails. SDK errors are plain { code, message } objects.
async function sdk(call) {
  if (!ready) return undefined;
  try {
    return await call();
  } catch (e) {
    console.warn("SDK:", e.code, e.message);
    return undefined;
  }
}

async function boot() {
  if (SDK) {
    await SDK.init(); // never rejects
    ready = SDK.environment !== "disabled"; // "zokogames", "local" or "disabled"
  }
  await sdk(() => SDK.game.loadingStart());
  best = Number((await sdk(() => SDK.data.getItem("best"))) || 0);
  if (ready) {
    muted = SDK.game.settings.muteAudio; // the portal's mute button
    SDK.game.addSettingsChangeListener((settings) => { muted = settings.muteAudio; });
  }
  $("best").textContent = best;
  $("reward").hidden = !ready;
  await sdk(() => SDK.game.loadingStop()); // the menu is ready
  $("play").disabled = false;
}

function beep() {
  if (muted) return;
  audio = audio || new AudioContext(); // created inside a tap, as browsers require
  const osc = audio.createOscillator();
  osc.connect(audio.destination);
  osc.start();
  osc.stop(audio.currentTime + 0.05);
}

// Starts or stops the round clock and tells the portal. Safe to call twice.
function setRunning(on) {
  if (on === running) return undefined;
  running = on;
  if (on) timer = setInterval(tick, 1000);
  else clearInterval(timer);
  return sdk(() => (on ? SDK.game.gameplayStart() : SDK.game.gameplayStop()));
}

function startRound() {
  score = 0;
  timeLeft = 15 + bonus;
  bonus = 0;
  playing = true;
  $("panel").hidden = true;
  $("target").hidden = false;
  moveTarget();
  render();
  setRunning(true); // input is live: gameplayStart
}

function tick() {
  timeLeft -= 1;
  render();
  if (timeLeft <= 0) endRound();
}

async function endRound() {
  playing = false;
  await setRunning(false); // gameplayStop, always before an ad
  $("target").hidden = true;
  $("panel").hidden = false;
  if (score > best) {
    best = score;
    $("best").textContent = best;
    await sdk(() => SDK.data.setItem("best", String(best))); // synchronous; throws on error
    await sdk(() => SDK.game.happytime());
  }
  await showAd("midgame"); // a natural break; the portal decides whether an ad plays
}

// Resolves true only when the ad played to the end.
async function showAd(type) {
  let finished = false;
  $("play").disabled = true;
  await sdk(() => SDK.ad.requestAd(type, {
    adStarted: () => audio && audio.suspend(), // silent while the ad plays
    adFinished: () => { finished = true; },
    adError: (e) => console.info("No ad:", e.code), // adsDisabled before full launch
  }));
  if (audio) audio.resume(); // after adFinished and after adError
  $("play").disabled = false;
  return finished;
}

$("reward").addEventListener("click", async () => {
  const earned = await showAd("rewarded");
  if (earned) bonus = 5; // the reward only after a finished ad
  $("msg").textContent = earned ? "+5 seconds next round" : "No ad right now";
});

$("target").addEventListener("pointerdown", (event) => {
  event.preventDefault();
  if (!running) return;
  score += 1;
  beep();
  moveTarget();
  render();
});

// Clicking outside the game or leaving the tab pauses the round; it continues on return.
window.addEventListener("blur", () => { if (playing) setRunning(false); });
window.addEventListener("focus", () => { if (playing) setRunning(true); });

function moveTarget() {
  const box = $("game").getBoundingClientRect();
  $("target").style.left = `${Math.random() * (box.width - 64)}px`;
  $("target").style.top = `${48 + Math.random() * (box.height - 112)}px`;
}

function render() {
  $("score").textContent = score;
  $("time").textContent = timeLeft;
}

$("play").addEventListener("click", startRound);
boot();

3. Run it locally#

Bash
cd target-tap && npx http-server . -p 8080
# open http://localhost:8080/?sdk_debug=true

On localhost the SDK runs in local mode: it answers every call itself, with no portal involved. You should see [ZokoGames SDK] lines in the browser console for game.loadingStart, game.loadingStop, and then game.gameplayStart when you press Play. After 15 seconds a three-second placeholder labelled Local ad (midgame) covers the page. A second round within three minutes gets no midgame ad (adCooldown in the console), which is expected. See testing for the switches that force other answers.

4. Turn the Preview checklist green#

  1. Open the Preview tool, type http://localhost:8080/ and press Load game.
  2. Within 20 seconds, press Play and tap the circle until the round ends.
  3. Let the placeholder ad finish.
  4. Press Watch an ad: +5 seconds next round and let that ad finish too.
  5. Press Play again.

You should see every item of the Requirements checklist marked done except Banner requested; this game has no banner, and banners are optional. Once the game is uploaded (step 5), the same play-through on the game's Preview and QA tab in the console shows every required item as Passing: SDK initialised, gameplayStart called, First gameplayStart within 20 seconds, gameplayStop called on pause, menu and game over, A midgame ad was requested, gameplayStop before each ad request and Play resumed after an ad finished. The testing checklist explains every item.

5. Zip and upload#

Bash
cd target-tap && zip ../target-tap.zip index.html game.js
unzip -l ../target-tap.zip

Both files must be at the root of the zip. Upload target-tap.zip on the Build step of a new game, as described in Your first game live in 30 minutes. You should see the build pass validation with no errors and no warnings, with the SDK script found and an init call detected.

How the code keeps the rules#

  • The game runs without the SDK. sdk() skips every call when the script was blocked or the environment is "disabled", and catches every failure. init() itself never rejects.
  • Play time is measured correctly. gameplayStart is sent when Play is pressed, not at page load. gameplayStop is sent when the round ends and when the player clicks outside the game or switches tab, so the round pauses there too.
  • Ads only at a break. The midgame ad is requested after gameplayStop, at the end of a round. Play resumes only when the player presses Play, so gameplayStart follows every ad. The portal decides whether an ad really plays (cooldowns, ad blockers, ad-free members); every outcome continues the game.
  • Rewards only after a finished ad. bonus is granted only when adFinished ran. On adError the player sees "No ad right now" and keeps playing.
  • Sound obeys the player. No sound plays before the first tap, the portal's mute setting silences the game at start and whenever it changes, and the audio is suspended while an ad plays. See audio, pausing and ads.
  • The save is small. One short string under one key, far below the 1,048,576-byte limit. getItem and setItem are synchronous and throw on error, which sdk() catches.
  • It works on phones. Pointer events cover mouse and touch, touch-action: none stops the page from scrolling, and the layout fills any frame size.
Important

Ads only run for games in full launch. In basic launch the real portal answers with adsDisabled (and unfilled when no ad is available), and your adError branch runs. Locally you see a three-second placeholder instead. The code path is the same.

Next steps#

Something unclear, wrong or missing on this page? Tell us through support · Test your game in the preview tool