single-file-html5-games / template.html
boundbomb's picture
Rename 2-template.html to template.html
8412b77 verified
Raw History Blame Contribute Delete
3.2 kB
<!DOCTYPE html>
<!--
single-file HTML5 canvas mini-game starter template
====================================================
One file. No build step, no dependencies, no images, no fonts.
Save as index.html, zip it, and it runs anywhere a browser does
(including itch.io HTML5 uploads).
How to use:
1. Change CONFIG below (title, colors, speed).
2. Write your game rules in update() and draw().
3. Keep everything inside this one file.
License: MIT (do whatever you want; see README.md)
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MY GAME</title>
<style>
html,body{margin:0;height:100%;background:#0b0f1a;display:flex;align-items:center;justify-content:center}
canvas{background:#10182b;border-radius:8px;max-width:100vw;max-height:100vh}
</style>
</head>
<body>
<canvas id="game" width="640" height="360"></canvas>
<script>
"use strict";
/* ===== CONFIG: tweak these first ===== */
const CONFIG = {
title: "MY GAME",
bg: "#10182b",
fg: "#e8ecf4",
accent: "#ffd166",
playerSpeed: 180, // pixels per second
};
/* ===== boilerplate: canvas, input, loop ===== */
const cv = document.getElementById("game");
const ctx = cv.getContext("2d");
const keys = {};
addEventListener("keydown", e => { keys[e.key.toLowerCase()] = true; });
addEventListener("keyup", e => { keys[e.key.toLowerCase()] = false; });
const mouse = { x: 0, y: 0, down: false };
cv.addEventListener("mousemove", e => {
const r = cv.getBoundingClientRect();
mouse.x = (e.clientX - r.left) * (cv.width / r.width);
mouse.y = (e.clientY - r.top) * (cv.height / r.height);
});
cv.addEventListener("mousedown", () => mouse.down = true);
cv.addEventListener("mouseup", () => mouse.down = false);
/* ===== game state: this is the part you design ===== */
const state = {
player: { x: cv.width / 2, y: cv.height / 2, r: 10 },
score: 0,
};
function update(dt) {
// dt = seconds since last frame. Move things, check collisions, score points.
const p = state.player;
if (keys["arrowleft"] || keys["a"]) p.x -= CONFIG.playerSpeed * dt;
if (keys["arrowright"] || keys["d"]) p.x += CONFIG.playerSpeed * dt;
if (keys["arrowup"] || keys["w"]) p.y -= CONFIG.playerSpeed * dt;
if (keys["arrowdown"] || keys["s"]) p.y += CONFIG.playerSpeed * dt;
p.x = Math.max(p.r, Math.min(cv.width - p.r, p.x));
p.y = Math.max(p.r, Math.min(cv.height - p.r, p.y));
}
function draw() {
ctx.fillStyle = CONFIG.bg;
ctx.fillRect(0, 0, cv.width, cv.height);
// player
ctx.fillStyle = CONFIG.accent;
ctx.beginPath();
ctx.arc(state.player.x, state.player.y, state.player.r, 0, Math.PI * 2);
ctx.fill();
// hud
ctx.fillStyle = CONFIG.fg;
ctx.font = "14px monospace";
ctx.textAlign = "left";
ctx.fillText(CONFIG.title + " | arrows/WASD to move | score " + state.score, 12, 22);
}
/* ===== main loop (fixed-ish timestep, keep as is) ===== */
let last = performance.now();
function frame(now) {
const dt = Math.min(0.05, (now - last) / 1000);
last = now;
update(dt);
draw();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>