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

cpp/app/pong.cpp · 7793 字节 · 下载原文

▶ 在浏览器里试玩

// 对打(Pong)—— 用 **C++** 写的手机小游戏
// Pong — a small phone game written in **C++**
//
// 玩法(与 `Examples/c/app/pong.c` / `Examples/python/app/pong.py` 同一套):
// Gameplay (same rules as the C / Python versions):
//   · 左板跟手指(点/拖左半屏),右板是 AI 追球,先到 5 分;
//   · the left paddle follows the finger (tap/drag on the left half), the right one is an AI, first to 5 wins;
//   · 球碰上下边界反弹,碰板反弹并提速;出界对方得分;
//   · the ball bounces off the top/bottom and off a paddle (speed up); going out scores for the opponent;
//   · 结束后点屏幕 / ESC / 回车重开。
//   · after the match ends, tap the screen / ESC / Enter to play again.
//
// ## C++ 这一路的写法约束(见 `Examples/cpp/app/snake.cpp` 文件头)
// ## Style constraints on this C++ path (see the header of `Examples/cpp/app/snake.cpp`)
//
//   · 颜色写**负数十进制**(本前端词法器十进制专用,不认 `0x`);
//   · colours are **negative decimals** (the lexer is decimal-only and does not accept `0x`);
//   · 整数转字符串走本文件的 `numstr()`(不引 `<string>` / `sprintf`);
//   · integers to strings go through `numstr()` here (no `<string>` / `sprintf`);
//   · 数组维度只认字面量宏(→E391);
//   · array dimensions use literal macros only (→E391);
//   · 循环更新写 `i = i + 1`。
//   · loop updates are written `i = i + 1`.
//
// 跑法 —— 手机:`vml run examples/cpp/pong.cpp`
// How to run — on the phone: `vml run examples/cpp/pong.cpp`
// 跑法 —— 桌面:`vmlcli Examples/cpp/pong.cpp --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/cpp/pong.cpp --screen 480x640 --frames /tmp/fr`

#include <vml_ui.h>

#define PG_MAX_FRAMES 6000
#define PG_WIN_SCORE 5

#define C_BG    -15724520        // 0xFF101018
#define C_LINE  -14013898        // 0xFF2A2A36
#define C_LEFT  -11409298        // 0xFF51E86E
#define C_RIGHT -11890471        // 0xFF4A90D9
#define C_BALL  -1513232         // 0xFFE8E8F0
#define C_TEXT  -1513232         // 0xFFE8E8F0
#define C_DIM   -6643536         // 0xFF9AA0B0

static int LANG = 0;
static int SW = 360;
static int SH = 620;
static int PAD_W = 12;
static int PAD_H = 70;
static int BALL_R = 6;
static int leftY = 310;
static int rightY = 310;
static int ballX = 180;
static int ballY = 310;
static int bvx = -5;
static int bvy = 3;
static int scoreL = 0;
static int scoreR = 0;
static int ended = 0;
static int frames = 0;
static int seed = 20262;
static int serveDir = 0;
static int touchY = 0;

static char g_num[12];

static char* numstr(int v)
{
    int n = 0;
    int m = 0;
    char tmp[12];
    if (v < 0) { g_num[0] = '-'; v = -v; n = 1; }
    if (v == 0) { g_num[n] = '0'; g_num[n + 1] = 0; return g_num; }
    while (v > 0) { tmp[m] = (char)('0' + v % 10); v = v / 10; m = m + 1; }
    while (m > 0) { m = m - 1; g_num[n] = tmp[m]; n = n + 1; }
    g_num[n] = 0;
    return g_num;
}

static int pgRnd(int n)
{
    seed = (seed * 1103 + 12345) % 32768;
    return (seed / 256) % n;
}

static void relayout()
{
    SW = ui_scr_get_w();
    SH = ui_scr_get_h();
    if (SW <= 0) { SW = 360; }
    if (SH <= 0) { SH = 620; }
    PAD_W = SW / 28;
    if (PAD_W < 8) { PAD_W = 8; }
    PAD_H = SH / 6;
    if (PAD_H < 40) { PAD_H = 40; }
    BALL_R = SW / 40;
    if (BALL_R < 4) { BALL_R = 4; }
}

static void serve()
{
    ballX = SW / 2;
    ballY = SH / 2;
    if (serveDir == 0) { bvx = -5; serveDir = 1; }
    else { bvx = 5; serveDir = 0; }
    if (pgRnd(2) == 0) { bvy = -3; }
    else { bvy = 3; }
}

static void newGame()
{
    leftY = SH / 2;
    rightY = SH / 2;
    scoreL = 0;
    scoreR = 0;
    ended = 0;
    frames = 0;
    serveDir = 0;
    serve();
}

static void step()
{
    ballX = ballX + bvx;
    ballY = ballY + bvy;

    if (ballY < BALL_R) { ballY = BALL_R; bvy = 0 - bvy; }
    if (ballY > SH - BALL_R) { ballY = SH - BALL_R; bvy = 0 - bvy; }

    if (bvx < 0 && ballX <= 10 + PAD_W + BALL_R && ballX >= 6) {
        int top = leftY - PAD_H / 2;
        if (ballY >= top && ballY <= top + PAD_H) {
            ballX = 10 + PAD_W + BALL_R;
            bvx = 0 - bvx - 1;
            if (bvx < -14) { bvx = -14; }
        }
    }

    if (bvx > 0 && ballX >= SW - 10 - PAD_W - BALL_R && ballX <= SW - 6) {
        int top2 = rightY - PAD_H / 2;
        if (ballY >= top2 && ballY <= top2 + PAD_H) {
            ballX = SW - 10 - PAD_W - BALL_R;
            bvx = 0 - bvx + 1;
            if (bvx > 14) { bvx = 14; }
        }
    }

    if (ballX < 0 - BALL_R) {
        scoreR = scoreR + 1;
        if (scoreR >= PG_WIN_SCORE) { ended = 1; }
        serve();
    }
    if (ballX > SW + BALL_R) {
        scoreL = scoreL + 1;
        if (scoreL >= PG_WIN_SCORE) { ended = 1; }
        serve();
    }

    if (rightY < ballY) { rightY = rightY + 4; }
    if (rightY > ballY) { rightY = rightY - 4; }

    if (leftY < PAD_H / 2) { leftY = PAD_H / 2; }
    if (leftY > SH - PAD_H / 2) { leftY = SH - PAD_H / 2; }
    if (rightY < PAD_H / 2) { rightY = PAD_H / 2; }
    if (rightY > SH - PAD_H / 2) { rightY = SH - PAD_H / 2; }
}

static void draw()
{
    ui_2d_clear(C_BG);

    int y = 10;
    while (y + 16 <= SH - 10) {
        ui_2d_rect(SW / 2 - 2, y, 4, 16, C_LINE, 1, 0, 0);
        y = y + 32;
    }

    ui_2d_rect(10, leftY - PAD_H / 2, PAD_W, PAD_H, C_LEFT, 1, 0, 4);
    ui_2d_rect(SW - 10 - PAD_W, rightY - PAD_H / 2, PAD_W, PAD_H, C_RIGHT, 1, 0, 4);
    ui_2d_circle(ballX, ballY, BALL_R, C_BALL, 1, 0);

    ui_2d_text(SW / 2 - 70, 28, numstr(scoreL), C_TEXT, 26, 1);
    ui_2d_text(SW / 2 + 70, 28, numstr(scoreR), C_TEXT, 26, 1);

    if (ended != 0) {
        if (LANG == 0) {
            if (scoreL >= PG_WIN_SCORE) { ui_2d_text(SW / 2, SH / 2 - 40, "你赢了!点一下再来", C_LEFT, 20, 1); }
            else { ui_2d_text(SW / 2, SH / 2 - 40, "电脑赢了,点一下再来", -2069424, 20, 1); }
        } else {
            if (scoreL >= PG_WIN_SCORE) { ui_2d_text(SW / 2, SH / 2 - 40, "You win! Tap to retry", C_LEFT, 20, 1); }
            else { ui_2d_text(SW / 2, SH / 2 - 40, "AI wins - tap to retry", -2069424, 20, 1); }
        }
    } else {
        if (LANG == 0) { ui_2d_text(SW / 2, 58, "左半屏滑动控制球板,先到 5 分", C_DIM, 11, 1); }
        else { ui_2d_text(SW / 2, 58, "Drag on the left half; first to 5", C_DIM, 11, 1); }
    }
    ui_2d_present();
}

int main()
{
    LANG = ui_scr_get_language();
    if (LANG == 0) { ui_win_open("对打", 0, 0); }
    else { ui_win_open("Pong", 0, 0); }
    ui_scr_set_keep_on(1);

    relayout();
    newGame();
    draw();

    int tid = ui_timer_set(33, 0);
    int done = 0;
    while (done == 0 && ui_win_is_closed() == 0) {
        int t = ui_msg_fetch_wait(0);
        if (t == 10) { break; }

        if (t == 6 || t == 4) {
            touchY = ui_msg_get_b();
            if (ended != 0) { newGame(); }
            else { leftY = touchY; }
            draw();
            continue;
        }

        if (t == 7) {
            touchY = ui_msg_get_b();
            if (ended == 0) { leftY = touchY; }
            draw();
            continue;
        }

        if (t == 1) {
            int k = ui_msg_get_a();
            if (k == 27) { done = 1; }
            if (k == 13 || k == 82) { newGame(); draw(); }
            continue;
        }

        if (t == 11) { relayout(); draw(); continue; }

        if (t == 9) {
            frames = frames + 1;
            if (frames >= PG_MAX_FRAMES) { done = 1; }
            if (ended == 0) { step(); }
            draw();
            continue;
        }
    }

    ui_timer_kill(tid);
    ui_scr_set_keep_on(0);
    ui_win_close();
    return 0;
}