数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 265|回复: 4

[手机] 用AI手搓DIY网页小游戏,飞镖风暴,全开源。

[复制链接]
发表于 2026-9-4 12:02:52 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 kindzhon 于 2026-9-4 12:11 编辑

刚手搓完的游戏,飞镖风暴,打完比比分数,点这个网址,手机和电脑 都能玩:https://canvas-dodge-game.streamlit.app/
源码地址:https://github.com/kindzhon/Canvas-Dodge-Game

用qwen3.8和以下的提示词你也能手搓。
  1. 做一个单文件网页小游戏,保存为index.html。玩家用键盘左右方向键控制一个小人左右移动,躲避从上往下落下来的飞镖。碰到飞镖就结束,右上角显示分数,结束后有重新开始按钮。不要用外部图片和外部脚本,颜色和形状用 canvas 自己画。做完用本地方式打开,确认手机和键盘能玩。
复制代码



14ce32ddf46628dcb7819586b3cc2cea.jpg ffb5d096a5cc76d7bc85f5f8ef79a4a5.jpg f8f93923e2b95e4a9b632dda7c72ce9f.jpg 469aeb57bb697cd5323e8c35f58ea238.jpg

部分代码:app.py
  1. # -*- coding: utf-8 -*-
  2. """飞镖风暴 · Dart Storm —— Streamlit 启动器。

  3. 将同目录下的单文件游戏 index.html 嵌入 Streamlit 页面,
  4. 可直接部署到 Streamlit Community Cloud (share.streamlit.io)。
  5. 本地运行:streamlit run app.py
  6. """
  7. from pathlib import Path

  8. import streamlit as st
  9. import streamlit.components.v1 as components

  10. ROOT = Path(__file__).resolve().parent
  11. GAME_FILE = ROOT / "index.html"

  12. st.set_page_config(
  13.     page_title="飞镖风暴 · Dart Storm",
  14.     layout="wide",
  15.     initial_sidebar_state="collapsed",
  16. )

  17. # ---------- 页面样式:深色街机厅外壳 ----------
  18. st.markdown(
  19.     """
  20.     <style>
  21.       .stApp {
  22.         background:
  23.           radial-gradient(900px 480px at 88% -8%, rgba(255,45,85,.16), transparent 62%),
  24.           radial-gradient(760px 420px at 4% -6%, rgba(64,224,208,.10), transparent 60%),
  25.           radial-gradient(1200px 800px at 50% 120%, rgba(20,26,44,.9), transparent 70%),
  26.           #0a0e1a;
  27.       }
  28.       .block-container { max-width: 1180px; padding-top: 12px; padding-bottom: 28px; }
  29.       #MainMenu, footer, [data-testid="stToolbar"] { visibility: hidden; }
  30.       header[data-testid="stHeader"] { background: transparent; }

  31.       /* 报头:左对齐排印 + 右侧按键芯片 */
  32.       .ds-head { display:flex; align-items:flex-end; justify-content:space-between;
  33.         gap:18px; flex-wrap:wrap; padding: 4px 2px 14px; }
  34.       .ds-eyebrow { display:inline-flex; align-items:center; gap:9px;
  35.         font-size:11.5px; letter-spacing:.34em; color:#ff2d55; font-weight:700;
  36.         margin-bottom:7px; }
  37.       .ds-title { margin:0;
  38.         font-family: Impact, Haettenschweiler, "Arial Narrow Bold", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  39.         font-size: clamp(36px, 5vw, 56px); line-height: .95; color:#f2f5ff;
  40.         letter-spacing:.03em; text-shadow: 0 0 26px rgba(255,45,85,.38); }
  41.       .ds-title .en { color:#ff2d55; font-size:.4em; letter-spacing:.36em;
  42.         vertical-align: middle; margin-left:12px; }
  43.       .ds-sub { margin:8px 0 0; color:#93a0c2; font-size:13.5px; letter-spacing:.04em; }
  44.       .ds-chips { display:flex; gap:8px; flex-wrap:wrap; padding-bottom:8px; }
  45.       .ds-chips span { border:1px solid rgba(147,160,194,.35); color:#c6d0ea;
  46.         background: rgba(20,26,44,.72); padding:5px 12px; border-radius:6px;
  47.         font-size:12.5px; letter-spacing:.06em; transition: all .18s ease; }
  48.       .ds-chips span:hover { border-color:#ff2d55; color:#fff;
  49.         box-shadow: 0 0 14px rgba(255,45,85,.4); transform: translateY(-2px); }
  50.       .ds-chips b { color:#ffd23e; font-weight:700; margin-right:2px; }

  51.       /* 游戏舱:给 components.html 的 iframe 包一层边框外壳 */
  52.       div:has(> iframe[title="streamlit_html"]) {
  53.         height: 76vh !important;
  54.         border: 1px solid rgba(255,45,85,.45); border-radius: 12px; padding: 6px;
  55.         background: rgba(10,14,26,.62);
  56.         box-shadow: 0 20px 55px rgba(0,0,0,.55), 0 0 36px rgba(255,45,85,.13);
  57.       }
  58.       iframe[title="streamlit_html"] {
  59.         height: calc(76vh - 14px) !important; width: 100%;
  60.         border-radius: 8px; display: block;
  61.       }

  62.       .ds-hint { display:flex; gap:10px; align-items:center; margin:13px 2px 0;
  63.         color:#93a0c2; font-size:13px; line-height:1.6; }
  64.       .ds-hint .dot { width:8px; height:8px; border-radius:50%; background:#40e0d0;
  65.         box-shadow: 0 0 10px #40e0d0; animation: dsPulse 1.6s ease-in-out infinite; flex:none; }
  66.       @keyframes dsPulse { 50% { opacity:.3; transform: scale(.8); } }
  67.       .ds-hint b { color:#e8ecf8; }

  68.       .ds-foot { margin-top:20px; padding-top:12px; color:#5b6785; font-size:12px;
  69.         border-top:1px solid rgba(91,103,133,.22);
  70.         display:flex; justify-content:space-between; gap:12px; flex-wrap:wrap; }

  71.       [data-testid="stExpander"] { background: rgba(20,26,44,.6);
  72.         border:1px solid rgba(147,160,194,.25); border-radius:8px; }
  73.     </style>
  74.     """,
  75.     unsafe_allow_html=True,
  76. )

  77. # ---------- 报头 ----------
  78. st.markdown(
  79.     """
  80.     <div class="ds-head">
  81.       <div>
  82.         <span class="ds-eyebrow">
  83.           <svg width="15" height="15" viewBox="0 0 24 24" fill="none" aria-hidden="true">
  84.             <circle cx="12" cy="12" r="10" stroke="#ff2d55" stroke-width="2.4"/>
  85.             <circle cx="12" cy="12" r="5.2" stroke="#ffd23e" stroke-width="2.2"/>
  86.             <circle cx="12" cy="12" r="1.7" fill="#40e0d0"/>
  87.           </svg>
  88.           CANVAS 单文件街机 · STREAMLIT 云版
  89.         </span>
  90.         <h1 class="ds-title">飞镖风暴<span class="en">DART STORM</span></h1>
  91.         <p class="ds-sub">左右闪避落镖 · 擦身触发「好险」连击 · 每 12 秒提速一档 —— 看你能撑几秒</p>
  92.       </div>
  93.       <div class="ds-chips">
  94.         <span><b>← →</b>移动</span>
  95.         <span><b>P</b>暂停</span>
  96.         <span><b>M</b>静音</span>
  97.         <span><b>R</b>重开</span>
  98.       </div>
  99.     </div>
  100.     """,
  101.     unsafe_allow_html=True,
  102. )

  103. # ---------- 游戏本体 ----------
  104. if not GAME_FILE.exists():
  105.     st.error("未找到游戏文件 index.html,请确认它与 app.py 位于同一目录。")
  106.     st.stop()

  107. game_html = GAME_FILE.read_text(encoding="utf-8")

  108. # Streamlit 的 components.html 运行在沙箱 iframe 中(无 allow-same-origin),
  109. # 直接访问 localStorage 会抛异常。注入一个内存版垫片,让最高分功能继续可用;
  110. # 即便垫片注入失败,游戏自身的 try/catch 也保证不会崩溃。
  111. LS_SHIM = (
  112.     "<script>(function(){try{window.localStorage.setItem('__ds_t','1');"
  113.     "window.localStorage.removeItem('__ds_t');}catch(e){var m={};"
  114.     "try{Object.defineProperty(window,'localStorage',{configurable:true,"
  115.     "get:function(){return{getItem:function(k){return Object.prototype."
  116.     "hasOwnProperty.call(m,k)?m[k]:null;},setItem:function(k,v){"
  117.     "m[k]=String(v);},removeItem:function(k){delete m[k];},"
  118.     "clear:function(){m={};}};}});}catch(e2){}}})();</script>"
  119. )
  120. game_html = game_html.replace("<head>", "<head>" + LS_SHIM, 1)

  121. components.html(game_html, height=720, scrolling=False)

  122. st.markdown(
  123.     """
  124.     <div class="ds-hint"><span class="dot"></span>
  125.       <span>首次游玩请先<b>点击一次游戏画面</b>,让键盘焦点进入游戏窗口,
  126.       随后用 <b>← / →</b>(或 A / D)移动。最佳纪录保存在本机浏览器。</span>
  127.     </div>
  128.     """,
  129.     unsafe_allow_html=True,
  130. )

  131. # ---------- 说明 ----------
  132. with st.expander("玩法说明 · 本地运行 · 部署到 Streamlit Cloud"):
  133.     st.markdown(
  134.         """
  135. **操作**
  136. - `← / →` 或 `A / D`:左右移动
  137. - `空格 / 回车 / R`:开始 / 重新开始
  138. - `P / Esc`:暂停(切出窗口自动暂停),`M`:静音

  139. **规则**
  140. - 被飞镖击中立即结束;镖尖擦身而过触发「好险」连击加分
  141. - 每 12 秒提速一档;22 秒后出现重型金镖,45 秒后出现旋转漂移镖

  142. **本地运行**
  143. ```bash
  144. pip install -r requirements.txt
  145. streamlit run app.py
  146. ```

  147. **部署到 Streamlit Cloud**
  148. 1. 将本仓库(`app.py`、`index.html`、`requirements.txt`、`.streamlit/`)推送到 GitHub
  149. 2. 打开 share.streamlit.io → **New app**
  150. 3. Main file path 填 `app.py`,点击 **Deploy**
  151. """
  152.     )

  153. st.markdown(
  154.     """
  155.     <div class="ds-foot">
  156.       <span>飞镖风暴 · 场景与角色全部由 Canvas 手绘,零外部图片 / 脚本</span>
  157.       <span>STREAMLIT COMMUNITY CLOUD READY</span>
  158.     </div>
  159.     """,
  160.     unsafe_allow_html=True,
  161. )
复制代码





14ce32ddf46628dcb7819586b3cc2cea.jpg

打赏

参与人数 1家元 +12 收起 理由
jf201006 + 12 謝謝分享

查看全部打赏

发表于 2026-9-4 12:54:47 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-9-4 13:16:05 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2026-9-4 14:13:51 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|数码之家-技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2026-9-14 17:05 , Processed in 0.109200 second(s), 11 queries , Gzip On, Redis On.

Powered by Discuz!

© MyDigit.Net Since 2006

快速回复 返回顶部 返回列表