> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nodeloc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速开始

> 用点点看模板做一个单人小游戏并在浏览器中试玩

点点看是最小的单人小游戏：10 秒内尽量多点按钮。它不连游戏服务，也不动游戏能量，演示每个游戏都会用到的 SDK 功能。源码在服务端仓库的 `games/hello`。

## 文件

```text theme={null}
hello/
├── manifest.json   包的清单
├── index.html
├── style.css
├── main.js
└── pack.sh         打包成 .nlgame
```

游戏包的 CSP 不允许内联脚本，脚本要放在单独的文件中。

## manifest.json

```json theme={null}
{
  "schema": 1,
  "id": "hello",
  "name": "点点看",
  "version": "0.1.0",
  "build": 1,
  "entry": "index.html",
  "orientation": "portrait",
  "background": "#10202a",
  "sdk": ">=1.0 <2",
  "scopes": ["game.identity", "game.energy.read", "game.haptics", "game.share"],
  "network": { "connect": [], "img": [] },
  "players": { "min": 1, "max": 1 }
}
```

`id` 要与论坛中应用的标识一致。字段说明见[清单、权限与游戏包](https://docs.nodeloc.com/minigames/manifest.md)。

## index.html

```html theme={null}
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no">
  <link rel="stylesheet" href="style.css">
  <script src="nl-game-sdk.js"></script>
  <script src="main.js" defer></script>
</head>
<body>
  <header><img id="avatar" alt=""><b id="name">…</b><small id="energy"></small></header>
  <main><button id="target" type="button">开始</button><p id="score"></p></main>
  <footer>
    <button id="share" type="button" hidden>叫朋友来玩</button>
    <p id="signin" hidden><button id="login" type="button">登录</button>后显示你的名字和游戏能量</p>
  </footer>
</body>
</html>
```

## main.js 的关键部分

```javascript theme={null}
async function boot() {
  nl.setLoadingProgress(0.5);
  try {
    // 右上角是宿主的「退出」胶囊，标题栏要避开它
    const cap = await nl.ui.getCapsuleRect(); // {x, y, width, height}
    if (cap.width > 0) document.querySelector("header").style.paddingRight = `${innerWidth - cap.x + 8}px`;
  } catch {}
  await nl.ready(); // 关闭宿主加载页，越早越好
  loadUser();
}

async function loadUser() {
  try {
    const me = await nl.auth.getUser();
    $("name").textContent = me.name || me.username;
    if (me.avatar) $("avatar").src = me.avatar;
  } catch (e) {
    if (e.code === "AUTH_REQUIRED") { $("signin").hidden = false; return; } // 没登录不是网络错误
  }
  try {
    const w = await nl.energy.getWallet();
    $("energy").textContent = `游戏能量 ${w.balance}`;
  } catch {} // 权限没批时 SCOPE_DENIED
}

// 登录必须在点击事件里调用
$("login").addEventListener("click", () => nl.auth.login().then(loadUser, () => {}));
$("target").addEventListener("click", () => { /* 计数 */ nl.haptics.impact("light"); });
$("share").addEventListener("click", () => nl.share.invite({ title: "点点看", text: "来点点看" }));
nl.lifecycle.on("pause", () => { /* 切到后台时作废这一局 */ });
boot();
```

## 打包与试玩

`pack.sh` 把文件和 SDK 一起打成 zip：

```bash theme={null}
games/hello/pack.sh 2        # 生成 games/hello/dist/hello-0.1.0-2.nlgame
```

上传后在浏览器打开 `https://www.nodeloc.com/games/hello`，论坛会跳转到网页版并完成授权。上传和本地调试见[本地开发与发布](https://docs.nodeloc.com/minigames/publishing.md)。

## 下一步

1. 复制模板，修改 `manifest.json` 的 `id`、`name` 和 `orientation`。
2. `scopes` 只写用得到的权限。每多一项，审核和用户首次授权时就要多确认一项。
3. 需要多人对局时，阅读[实时对局](https://docs.nodeloc.com/minigames/realtime.md)和[规则集](https://docs.nodeloc.com/minigames/rulesets.md)。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.