Vibe Gaming 指用自然语言描述“想玩什么感觉”,由 AI 生成规则、关卡、叙事与美术风格,玩家在几分钟内进入可玩状态。它不是让 LLM 当游戏引擎——那会带来幻觉、不可复现与数值崩坏——而是让 LLM 出可执行规格,由确定性引擎掌管状态。专业落地的关键就在这条分工线。
三层职责必须清晰:
这样既有生成的自由,又有工程的可控。
import os, json, random
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
SPEC_SYSTEM = """你是游戏设计师。根据用户描述的氛围,输出可执行规格JSON:
{"title":str,"genre":str,"core_loop":str,
"resources":[{"name":str,"start":int}],
"actions":[{"id":str,"label":str,"cost":{str:int},"effects":{str:int}}],
"win_condition":{"resource":str,"op":">=","value":int},
"lose_condition":{"resource":str,"op":"<=","value":int},
"mood":{"palette":[str],"music":str,"narrative_voice":str}}
约束:动作3-5个,资源1-3种,效果数值在 -5..5,胜负条件必须引用已有资源。"""
def design_game(vibe: str) -> dict:
r = client.chat.completions.create(
model="gpt-4o", temperature=0.7,
response_format={"type": "json_object"},
messages=[{"role": "system", "content": SPEC_SYSTEM},
{"role": "user", "content": f"氛围:{vibe}"}],
)
return json.loads(r.choices[0].message.content)生成的规格形如:
{"title":"雾港夜巡","genre":"资源管理",
"resources":[{"name":"fuel","start":8},{"name":"reputation","start":5}],
"actions":[
{"id":"patrol","label":"巡航","cost":{"fuel":2},"effects":{"reputation":3}},
{"id":"refuel","label":"补给","cost":{},"effects":{"fuel":4,"reputation":-2}},
{"id":"rest","label":"休整","cost":{},"effects":{"fuel":1}}],
"win_condition":{"resource":"reputation","op":">=","value":15},
"lose_condition":{"resource":"fuel","op":"<=","value":0},
"mood":{"palette":["#0B1D2A","#C9A96A"],"music":"ambient",
"narrative_voice":"冷峻克制"}}import operator
OPS = {">=": operator.ge, "<=": operator.le, ">": operator.gt, "<": operator.lt}
class VibeGame:
def __init__(self, spec, seed=42):
self.spec = spec
self.rng = random.Random(seed) # 种子保证可复现
self.res = {r["name"]: r["start"] for r in spec["resources"]}
self.turn = 0
def action(self, aid):
return next((a for a in self.spec["actions"] if a["id"] == aid), None)
def legal(self, aid):
a = self.action(aid)
return a and all(self.res.get(k, 0) >= v for k, v in a["cost"].items())
def step(self, aid):
a = self.action(aid)
if not a or not self.legal(aid):
return {"ok": False, "msg": "资源不足或动作非法"}
for k, v in a["cost"].items():
self.res[k] -= v
jitter = self.rng.randint(-1, 1) # 引擎控制随机性
for k, v in a["effects"].items():
self.res[k] = max(0, self.res.get(k, 0) + v + jitter)
self.turn += 1
return {"ok": True, "resources": dict(self.res), "turn": self.turn}
def _check(self, cond):
return OPS[cond["op"]](self.res.get(cond["resource"], 0), cond["value"])
def status(self):
if self._check(self.spec["lose_condition"]):
return "lose"
if self._check(self.spec["win_condition"]):
return "win"
return "running"胜负由代码判定,LLM 无权改写——这是 Vibe Gaming 与“AI 跑团”最大的工程差异。
叙述只负责氛围,且必须显式禁止改数值:
def narrate(spec, state, last_action):
r = client.chat.completions.create(
model="gpt-4o-mini", temperature=0.8,
messages=[
{"role": "system", "content":
f"你是叙述者。语气:{spec['mood']['narrative_voice']}。"
"只写两句氛围描写,不得提及或改变任何数值与规则。"},
{"role": "user", "content":
json.dumps({"state": state, "action": last_action},
ensure_ascii=False)},
],
)
return r.choices[0].message.content自适应难度基于历史表现调节抖动幅度,而非让模型临时改规则:
def adapt(history, base=1):
if len(history) < 5:
return base
recent = history[-10:]
win = sum(1 for h in recent if h.get("gain", 0) > 0) / len(recent)
if win > 0.8: return base + 1 # 太顺,加压
if win < 0.3: return max(0, base - 1)
return base生成后不应直接上线,先跑自动试玩。用随机策略做基线,观察胜率:
def autoplay(game, policy, steps=60):
log = []
for _ in range(steps):
if game.status() != "running":
break
aid = policy(game)
r = game.step(aid)
r["gain"] = sum(r.get("resources", {}).values())
log.append(r)
return game.status(), log
def random_policy(g):
legal = [a["id"] for a in g.spec["actions"] if g.legal(a["id"])]
return g.rng.choice(legal) if legal else g.spec["actions"][0]["id"]跑 200 局统计胜率,落在 40%–65% 才算可玩;偏离则让 LLM 只调整数值,不改结构。
jsonschema 校验 LLM 输出,缺字段直接重试。Vibe Gaming 的价值不是“AI 帮你做游戏”,而是把游戏设计从代码门槛降到意图表达,同时用确定性引擎、Schema 校验与自动试玩守住质量底线。让 LLM 负责创意与氛围,让代码负责规则与公平——这条分工线,才是它从 Demo 走向产品的关键。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。