File size: 3,198 Bytes
2ccd98f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!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>