← 都来码 / Dolaima← 例程索引c/app/rpg.c▶ 在浏览器里试玩 原文

c/app/rpg.c · 4951 字节 · 下载原文

▶ 在浏览器里试玩

/* rpg.c —— 文字冒险(剧情 + 选项 + 属性)
 * rpg.c -- text adventure (story, choices, stats)
 *
 * 玩法:一段剧情 + 若干按钮选项;不同选择改变 HP/金币/进度,走到结局分支。
 * Rules: a scene plus numbered choices; each choice changes HP/gold/progress and leads to another
 * scene until an ending.
 *
 * ⚠ 数据是**一张表**(场景 → 文本/选项/目标场景),逻辑只有"按编号跳转"那一小段 ——
 * ⚠ The data is **one table** (scene -> text / choice labels / next scene); the logic is only
 *   "jump by index",
 *   写十个分支也不多一行代码。
 *   so ten branches cost no extra code.
 */
#include <vml_ui.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_FRAMES 20000
#define N_SCENE 6
#define N_OPT 3

/* 每个场景:文本 / 三个选项文字(空 = 没有这一项)/ 三个目标场景 / 三个属性变化 */
static const char *SCENE_TEXT[N_SCENE] = {
    "清晨,你在山道口醒来。\n前面有岔路:左边是雾里的森林,右边是亮着的客栈。",
    "森林里传来低鸣。\n地上有一条发光的河,对岸像有个箱子。",
    "你涉水过去,打开箱子——里面是一把钥匙和三十个金币。",
    "客栈里坐着个商人:\n“帮我找钥匙,报酬一百金。”",
    "你把钥匙交给商人,他如约付钱。你带着钱离开了这片山。",
    "你什么也没拿就离开了。山雾吞掉了来时的路。"
};
static const char *OPT_TEXT[N_SCENE][N_OPT] = {
    {"走进森林", "去客栈", ""},
    {"涉水过去", "沿河走", "退回山道口"},
    {"继续赶路", "", ""},
    {"接受委托", "拒绝", ""},
    {"", "", ""},
    {"", "", ""}
};
static const int OPT_NEXT[N_SCENE][N_OPT] = {
    {1, 3, 0},
    {2, 5, 0},
    {5, 5, 5},
    {4, 5, 0},
    {4, 4, 4},
    {5, 5, 5}
};

static int SW, SH, g_lang, g_frames;
static int scene, hp = 10, gold;
static int btn_y[N_OPT], btn_h;

static void relayout(void)
{
    SW = ui_scr_get_w(); SH = ui_scr_get_h();
    if (SW <= 0) SW = 360;
    if (SH <= 0) SH = 620;
    btn_h = SH / 10;
    if (btn_h < 40) btn_h = 40;
}

static void draw(void)
{
    int i;
    char b[32];

    ui_2d_clear(0xFF101018);

    ui_2d_text(16, 40, "rpg.c", 0xFF9AA0B0, 12, VML_ANCHOR_LEFT);
    /* 正文:这里不分行排版(字数固定、行数少),直接把换行拆成多行画 */
    {
        const char *p = SCENE_TEXT[scene];
        int x = 16, y = 90, n = 0;
        char line[128];
        while (*p != 0 && n < 120) {
            if (*p == '\n' || n >= 22) { line[n] = 0; ui_2d_text(x, y, line, 0xFFE8E8F0, 16, VML_ANCHOR_LEFT); y += 30; n = 0; p++; continue; }
            line[n++] = *p++;
        }
        if (n > 0) { line[n] = 0; ui_2d_text(x, y, line, 0xFFE8E8F0, 16, VML_ANCHOR_LEFT); }
    }

    itoa(hp, b); ui_2d_text(16, 56, b, 0xFF51E86E, 13, VML_ANCHOR_LEFT);
    itoa(gold, b); ui_2d_text(90, 56, b, 0xFFFFD040, 13, VML_ANCHOR_LEFT);

    for (i = 0; i < N_OPT; i++) {
        const char *t = OPT_TEXT[scene][i];
        if (t[0] == 0) continue;
        ui_2d_rect(20, btn_y[i], SW - 40, btn_h, 0xFF2A2A36, 1, 0, 10);
        ui_2d_text(SW / 2, btn_y[i] + btn_h / 2 + 6, (char *)t, 0xFFE8E8F0, 15, VML_ANCHOR_CENTER);
    }
    ui_2d_text(SW / 2, SH - 12, g_lang == 0 ? "点选项推进剧情" : "Tap a choice", 0xFF9AA0B0, 11, VML_ANCHOR_CENTER);
    ui_2d_present();
}

int main(void)
{
    int msg[4], tid, done = 0, i;

    g_lang = ui_scr_get_language();
    relayout();
    ui_win_open_ex("rpg.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
    relayout();
    scene = 0; hp = 10; gold = 0;
    for (i = 0; i < N_OPT; i++) btn_y[i] = SH - 60 - (N_OPT - i) * (btn_h + 10);
    draw();

    tid = ui_timer_set(200, 1);
    while (!done && ui_win_is_closed() == 0) {
        int t = ui_msg_wait(msg, 0);
        if (t == VML_MSG_NONE) continue;

        if (t == VML_MSG_TOUCHDOWN || t == VML_MSG_MOUSEDOWN) {
            for (i = 0; i < N_OPT; i++) {
                if (OPT_TEXT[scene][i][0] == 0) continue;
                if (msg[2] >= btn_y[i] && msg[2] <= btn_y[i] + btn_h) {
                    scene = OPT_NEXT[scene][i];
                    if (scene == 2) gold = gold + 30;       /* 开箱子那一支 */
                    if (scene == 4) gold = gold + 100;      /* 交钥匙 */
                    break;
                }
            }
            draw(); continue;
        }
        if (t == VML_MSG_KEYDOWN) {
            if (msg[1] == VML_KEY_ESCAPE) { scene = 0; hp = 10; gold = 0; draw(); }
            continue;
        }
        if (t == VML_MSG_TIMER) { g_frames = g_frames + 1; if (g_frames >= MAX_FRAMES) done = 1; continue; }
        if (t == VML_MSG_WINDOWRESIZE) { relayout(); draw(); continue; }
        if (t == VML_MSG_WINDOWCLOSE) break;
    }

    ui_timer_kill(tid);
    ui_win_close();
    printf("rpg: scene=%d gold=%d\n", scene, gold);
    return 0;
}