📝 应用描述
经典猜拳小游戏
📋 详细信息
| 应用ID |
rock_paper_scissors |
| 名称 |
石头剪刀布 |
| 版本 |
v1 |
| 文件大小 |
1.2 KB |
| 上传时间 |
2026-07-29 21:18:06 |
| 状态 |
✅ 已启用 |
| 权限 |
storage |
💡 界面预览 (main.lua)
📄 Lua 源码
-- 游戏状态变量
local target_seed = 0
-- 初始化/重新开始:更新种子并重置显示
local function init_game()
-- 读取并递增存储的种子,使每次游戏随机序列不同
local old_seed = app.storage_get("seed") or "0"
local seed = tonumber(old_seed) or 0
seed = seed + 1
app.storage_set("seed", tostring(seed))
math.randomseed(seed)
-- 重置界面文本
app.set_text("result", "点击按钮出拳")
app.set_text("player", "你:")
app.set_text("computer", "电脑:")
end
-- 出拳逻辑
local function play(player_choice)
local choices = {"石头", "剪刀", "布"}
local computer_choice = math.random(1, 3) -- 1石头 2剪刀 3布
-- 判定胜负
local result = ""
if player_choice == computer_choice then
result = "平局!"
elseif (player_choice == 1 and computer_choice == 2) or
(player_choice == 2 and computer_choice == 3) or
(player_choice == 3 and computer_choice == 1) then
result = "🎉 你赢了!"
else
result = "😢 你输了!"
end
-- 更新显示
app.set_text("player", "你:" .. choices[player_choice])
app.set_text("computer", "电脑:" .. choices[computer_choice])
app.set_text("result", result)
end
-- 页面启动时初始化一次
init_game()
-- 返回页面
return ui.page({
title = "石头剪刀布",
children = {
ui.text({ id = "result", text = "点击按钮出拳", size = 20, center = true, margin = 12 }),
ui.spacer({ height = 8 }),
ui.text({ id = "player", text = "你:", size = 16, margin = 4 }),
ui.text({ id = "computer", text = "电脑:", size = 16, margin = 4 }),
ui.spacer({ height = 12 }),
ui.button({ text = "🪨 石头", on_click = function() play(1) end }),
ui.button({ text = "✂️ 剪刀", on_click = function() play(2) end }),
ui.button({ text = "📄 布", on_click = function() play(3) end }),
ui.spacer({ height = 12 }),
ui.button({ text = "🔄 重新开始", on_click = function() init_game() end })
}
})