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

cpp/app/breakout.cpp · 8678 字节 · 下载原文

▶ 在浏览器里试玩

// 打砖块(Breakout)—— 用 **C++** 写的手机小游戏
// Breakout — a small phone game written in **C++**
//
// 玩法(与 `Examples/c/app/breakout.c` / `Examples/python/app/breakout.py` 同一套):
// Gameplay (same rules as the C / Python versions):
//   · 8 列 × 5 行砖块,球撞到砖就消掉 +10 分;
//   · 8 columns x 5 rows of bricks; hitting one clears it and scores +10;
//   · 挡板跟手指(点/拖屏幕下半部),球掉下去扣一条命,三条命用完结束;
//   · the paddle follows the finger (tap/drag the lower half); a lost ball costs a life; three lives ends the run;
//   · 清完所有砖算通关;结束后点屏幕 / ESC / 回车重开。
//   · clearing every brick wins; tap / ESC / Enter restarts afterwards.
//
// ## C++ 这一路的写法约束(见 `Examples/cpp/app/snake.cpp` 文件头)
// ## Style constraints on this C++ path (see the header of `Examples/cpp/app/snake.cpp`)
//
//   · 颜色写**负数十进制**;整数转字符串走 `numstr()`;数组维度只认字面量宏(→E391);
//   · colours are **negative decimals**; integers to strings go through `numstr()`; array dimensions
//     use literal macros only (→E391);
//   · 循环更新写 `i = i + 1`。
//   · loop updates are written `i = i + 1`.
//
// 跑法 —— 手机:`vml run examples/cpp/breakout.cpp`
// How to run — on the phone: `vml run examples/cpp/breakout.cpp`
// 跑法 —— 桌面:`vmlcli Examples/cpp/breakout.cpp --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/cpp/breakout.cpp --screen 480x640 --frames /tmp/fr`

#include <vml_ui.h>

#define BK_MAX_FRAMES 20000
#define BK_COLS 8
#define BK_ROWS 5
#define BK_BRICKS 40
#define BK_LIVES 3

#define C_BG     -15724520       // 0xFF101018
#define C_PADDLE -11409298       // 0xFF51E86E
#define C_BALL   -1513232        // 0xFFE8E8F0
#define C_TEXT   -1513232        // 0xFFE8E8F0
#define C_DIM    -6643536        // 0xFF9AA0B0
#define C_B1     -2069424        // 0xFFE06C50
#define C_B2     -2509750        // 0xFFD9B44A
#define C_B3     -11409298       // 0xFF51E86E
#define C_B4     -11890471       // 0xFF4A90D9
#define C_B5     -6591248        // 0xFF9B6CF0

static int LANG = 0;
static int SW = 360;
static int SH = 620;
static int PAD_W = 70;
static int PAD_H = 12;
static int BALL_R = 6;
static int padX = 145;
static int ballX = 180;
static int ballY = 400;
static int bvx = 3;
static int bvy = -3;
static int score = 0;
static int lives = BK_LIVES;
static int won = 0;
static int dead = 0;
static int frames = 0;
static int brickW = 45;
static int brickH = 18;
static int brickTop = 70;
static int bricksLeft = 0;

// 砖块:1 = 还在(维度只写字面量,见文件头)
// Bricks: 1 = still there (literal dimensions only; see the header)
static int bricks[BK_BRICKS];

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 void relayout()
{
    SW = ui_scr_get_w();
    SH = ui_scr_get_h();
    if (SW <= 0) { SW = 360; }
    if (SH <= 0) { SH = 620; }
    brickW = SW / BK_COLS;
    brickH = SH / 34;
    if (brickH < 12) { brickH = 12; }
    PAD_W = SW / 5;
    if (PAD_W < 40) { PAD_W = 40; }
    PAD_H = SH / 50;
    if (PAD_H < 8) { PAD_H = 8; }
    BALL_R = SW / 60;
    if (BALL_R < 4) { BALL_R = 4; }
}

static void layBricks()
{
    int i = 0;
    while (i < BK_BRICKS) {
        bricks[i] = 1;
        i = i + 1;
    }
    bricksLeft = BK_BRICKS;
}

static void newGame()
{
    score = 0;
    lives = BK_LIVES;
    won = 0;
    dead = 0;
    frames = 0;
    layBricks();
    padX = SW / 2;
    ballX = SW / 2;
    ballY = SH - 120;
    bvx = 3;
    bvy = -3;
}

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

    if (ballX < BALL_R) { ballX = BALL_R; bvx = 0 - bvx; }
    if (ballX > SW - BALL_R) { ballX = SW - BALL_R; bvx = 0 - bvx; }
    if (ballY < BALL_R) { ballY = BALL_R; bvy = 0 - bvy; }

    // 挡板
    // Paddle
    int padY = SH - 70;
    if (bvy > 0 && ballY + BALL_R >= padY && ballY - BALL_R <= padY + PAD_H) {
        if (ballX >= padX - PAD_W / 2 && ballX <= padX + PAD_W / 2) {
            ballY = padY - BALL_R;
            bvy = 0 - bvy;
        }
    }

    // 砖块:遍历一遍,碰到的第一块消掉
    // Bricks: scan once and clear the first one hit
    int i = 0;
    while (i < BK_BRICKS) {
        if (bricks[i] != 0) {
            int bx = (i % BK_COLS) * brickW;
            int by = brickTop + (i / BK_COLS) * brickH;
            if (ballX + BALL_R >= bx && ballX - BALL_R <= bx + brickW &&
                ballY + BALL_R >= by && ballY - BALL_R <= by + brickH) {
                bricks[i] = 0;
                bricksLeft = bricksLeft - 1;
                score = score + 10;
                bvy = 0 - bvy;
                if (bricksLeft == 0) { won = 1; }
                i = BK_BRICKS;
            }
        }
        if (i < BK_BRICKS) { i = i + 1; }
    }

    if (ballY > SH + BALL_R) {
        lives = lives - 1;
        if (lives <= 0) { dead = 1; }
        ballX = SW / 2;
        ballY = SH - 120;
        bvx = 3;
        bvy = -3;
    }
}

static int rowColor(int row)
{
    if (row == 0) { return C_B1; }
    if (row == 1) { return C_B2; }
    if (row == 2) { return C_B3; }
    if (row == 3) { return C_B4; }
    return C_B5;
}

static void draw()
{
    ui_2d_clear(C_BG);

    int i = 0;
    while (i < BK_BRICKS) {
        if (bricks[i] != 0) {
            int bx = (i % BK_COLS) * brickW;
            int by = brickTop + (i / BK_COLS) * brickH;
            ui_2d_rect(bx + 1, by + 1, brickW - 2, brickH - 2, rowColor(i / BK_COLS), 1, 0, 3);
        }
        i = i + 1;
    }

    ui_2d_rect(padX - PAD_W / 2, SH - 70, PAD_W, PAD_H, C_PADDLE, 1, 0, 4);
    ui_2d_circle(ballX, ballY, BALL_R, C_BALL, 1, 0);

    ui_2d_text(10, 24, numstr(score), C_TEXT, 15, 0);
    if (LANG == 0) { ui_2d_text(10, 46, "得分", C_DIM, 10, 0); }
    else { ui_2d_text(10, 46, "score", C_DIM, 10, 0); }
    ui_2d_text(SW - 10, 24, numstr(lives), -2069424, 16, 2);
    if (LANG == 0) { ui_2d_text(SW - 10, 46, "命", C_DIM, 10, 2); }
    else { ui_2d_text(SW - 10, 46, "lives", C_DIM, 10, 2); }

    if (won != 0) {
        if (LANG == 0) { ui_2d_text(SW / 2, SH / 2, "全打光了!点一下再来", C_TEXT, 18, 1); }
        else { ui_2d_text(SW / 2, SH / 2, "Cleared! Tap to play again", C_TEXT, 18, 1); }
    } else if (dead != 0) {
        if (LANG == 0) { ui_2d_text(SW / 2, SH / 2, "没命了,点一下再来", -2069424, 18, 1); }
        else { ui_2d_text(SW / 2, SH / 2, "Out of lives - tap to retry", -2069424, 18, 1); }
    } else {
        if (LANG == 0) { ui_2d_text(SW / 2, SH - 34, "点 / 拖屏幕控制挡板", C_DIM, 11, 1); }
        else { ui_2d_text(SW / 2, SH - 34, "Tap / drag to move the paddle", 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("Breakout", 0, 0); }
    ui_scr_set_keep_on(1);

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

    int tid = ui_timer_set(16, 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 || t == 7) {
            padX = ui_msg_get_a();
            if (padX < PAD_W / 2) { padX = PAD_W / 2; }
            if (padX > SW - PAD_W / 2) { padX = SW - PAD_W / 2; }
            if ((won != 0 || dead != 0) && t != 7) { newGame(); }
            draw();
            continue;
        }

        if (t == 1) {
            int k = ui_msg_get_a();
            if (k == 27) { done = 1; }
            if (k == 13 || k == 82) { newGame(); draw(); }
            if (k == 37) { padX = padX - 24; }
            if (k == 39) { padX = padX + 24; }
            if (padX < PAD_W / 2) { padX = PAD_W / 2; }
            if (padX > SW - PAD_W / 2) { padX = SW - PAD_W / 2; }
            continue;
        }

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

        if (t == 9) {
            frames = frames + 1;
            if (frames >= BK_MAX_FRAMES) { done = 1; }
            if (won == 0 && dead == 0) { step(); }
            draw();
            continue;
        }
    }

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