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

c/app/asteroids.c · 8995 字节 · 下载原文

▶ 在浏览器里试玩

/* asteroids.c —— 陨石射击(飞船躲石头、打石头)
 * asteroids.c -- asteroid shooter (dodge rocks, shoot rocks)
 *
 * 玩法:飞船贴着底部左右移动;点屏幕或按空格发射子弹;石头从上方落下,
 * Rules: the ship slides left/right along the bottom; tap the screen or press space to fire;
 * rocks fall from above,
 * 打中一块 +10 分并分裂成两块更小的;被石头碰到扣一条命,三条命用完结束。
 * hitting one scores +10 and splits it into two smaller rocks; being hit costs a life, and three
 * lives ends the run.
 *
 * 这是**对象池**最小的样子:石头/子弹都用定长数组 + `alive` 标记,
 * This is the smallest possible **object pool**: rocks and bullets are fixed-size arrays with an
 * `alive` flag,
 * 不放"删元素"的动作(挪数组在手机上是白花的开销,也容易写错下标)。
 * and there is no "delete element" step (shifting arrays costs time on a phone and is easy to get
 * wrong with indexes).
 */

#include <vml_ui.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_FRAMES 4000
#define MAX_ROCKS 16
#define MAX_SHOTS 8

#define C_BG    0xFF101018
#define C_SHIP  0xFF51E86E
#define C_ROCK  0xFF9AA0B0
#define C_SHOT  0xFFE06C50
#define C_TEXT  0xFFE8E8F0
#define C_DIM   0xFF9AA0B0

static int SW, SH, g_lang, g_frames;
static int ship_x, ship_w, ship_h;
static int lives, score, over;

static int rock_x[MAX_ROCKS], rock_y[MAX_ROCKS], rock_r[MAX_ROCKS], rock_v[MAX_ROCKS], rock_on[MAX_ROCKS];
static int shot_x[MAX_SHOTS], shot_y[MAX_SHOTS], shot_on[MAX_SHOTS];

static unsigned int seed = 20261121;
static int rnd(int n)
{
    seed = seed * 1103515245u + 12345u;
    return (int)((seed >> 16) % (unsigned int)n);
}

static void relayout(void)
{
    SW = ui_scr_get_w();
    SH = ui_scr_get_h();
    if (SW <= 0) SW = 360;
    if (SH <= 0) SH = 620;
    ship_w = SW / 10;
    ship_h = SH / 26;
    if (ship_h < 10) ship_h = 10;
    if (ship_x <= 0 || ship_x > SW - ship_w) ship_x = SW / 2 - ship_w / 2;
}

static void spawn_rock(int i, int size)
{
    rock_on[i] = 1;
    rock_r[i] = size;
    rock_x[i] = rnd(SW - size * 2) + size;
    rock_y[i] = -size;
    rock_v[i] = SH / 90 + rnd(3);
    if (rock_v[i] < 2) rock_v[i] = 2;
}

static void new_game(void)
{
    int i;
    for (i = 0; i < MAX_ROCKS; i++) rock_on[i] = 0;
    for (i = 0; i < MAX_SHOTS; i++) shot_on[i] = 0;
    lives = 3;
    score = 0;
    over = 0;
    spawn_rock(0, SW / 14 + 4);
    spawn_rock(1, SW / 18 + 3);
    spawn_rock(2, SW / 22 + 3);
}

static void draw_ship(int x, int y)
{
    /* 一个朝上的三角:三条线拼(这一层没有 polygon,用线画也够) */
    ui_2d_line(x, y + ship_h, x + ship_w / 2, y, C_SHIP, 3);
    ui_2d_line(x + ship_w / 2, y, x + ship_w, y + ship_h, C_SHIP, 3);
    ui_2d_line(x, y + ship_h, x + ship_w, y + ship_h, C_SHIP, 3);
}

static void draw(void)
{
    int i;
    char buf[12];

    ui_2d_clear(C_BG);

    for (i = 0; i < MAX_ROCKS; i++) {
        if (!rock_on[i]) continue;
        ui_2d_circle(rock_x[i], rock_y[i], rock_r[i], C_ROCK, 1, 0);
    }
    for (i = 0; i < MAX_SHOTS; i++) {
        if (!shot_on[i]) continue;
        ui_2d_rect(shot_x[i] - 1, shot_y[i] - 6, 3, 10, C_SHOT, 1, 0, 1);
    }
    draw_ship(ship_x, SH - ship_h - 26);

    itoa(score, buf);
    ui_2d_text(10, 22, buf, C_TEXT, 16, VML_ANCHOR_LEFT);
    itoa(lives, buf);
    ui_2d_text(SW - 10, 22, buf, C_SHIP, 16, VML_ANCHOR_RIGHT);

    if (over)
        ui_2d_text(SW / 2, SH / 2, g_lang == 0 ? "结束,ESC 再来" : "Over - ESC to retry", C_TEXT, 20, VML_ANCHOR_CENTER);
    else
        ui_2d_text(SW / 2, SH - 10, g_lang == 0 ? "拖动 / 方向键移动,点一下发射" : "Drag / arrows to move, tap to fire",
                   C_DIM, 11, VML_ANCHOR_CENTER);
    ui_2d_present();
}

static void fire(void)
{
    int i;
    for (i = 0; i < MAX_SHOTS; i++) {
        if (shot_on[i]) continue;
        shot_on[i] = 1;
        shot_x[i] = ship_x + ship_w / 2;
        shot_y[i] = SH - ship_h - 30;
        return;
    }
}

int main(void)
{
    int msg[4];
    int tid;
    int done = 0;
    char buf[12];

    g_lang = ui_scr_get_language();
    relayout();
    ui_win_open_ex("asteroids.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NEED_GAMEPAD);
    relayout();
    new_game();
    draw();

    tid = ui_timer_set(50, 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) {
            // ⚠ 点按分两档:**下半屏 = 把飞船挪到手指下面**,**上半屏 = 开火**。
            //   原先"点哪都开火、顺手把船瞬移过去" —— 手指一抬飞船就跳到别处,根本没法瞄准。
            if (msg[2] > SH / 2) {
                ship_x = msg[1] - ship_w / 2;
                if (ship_x < 0) ship_x = 0;
                if (ship_x > SW - ship_w) ship_x = SW - ship_w;
            } else if (!over) {
                fire();
            }
            draw();
            continue;
        }
        if (t == VML_MSG_TOUCHMOVE || t == VML_MSG_MOUSEMOVE) {
            ship_x = msg[1] - ship_w / 2;
            if (ship_x < 0) ship_x = 0;
            if (ship_x > SW - ship_w) ship_x = SW - ship_w;
            draw();
            continue;
        }
        if (t == VML_MSG_KEYDOWN) {
            if (msg[1] == VML_KEY_ESCAPE) { new_game(); draw(); continue; }
            if (msg[1] == VML_KEY_LEFT) { ship_x = ship_x - SW / 14; if (ship_x < 0) ship_x = 0; }
            if (msg[1] == VML_KEY_RIGHT) { ship_x = ship_x + SW / 14; if (ship_x > SW - ship_w) ship_x = SW - ship_w; }
            if (msg[1] == VML_KEY_SPACE) fire();
            draw();
            continue;
        }

        if (t == VML_MSG_TIMER) {
            int i, j;

            g_frames = g_frames + 1;
            if (g_frames >= MAX_FRAMES) done = 1;

            if (!over) {
                /* 石头下落 */
                for (i = 0; i < MAX_ROCKS; i++) {
                    if (!rock_on[i]) continue;
                    rock_y[i] = rock_y[i] + rock_v[i];
                    if (rock_y[i] - rock_r[i] > SH) {
                        /* 掉出屏幕:换个位置重来(不扣命,保持节奏) */
                        spawn_rock(i, rock_r[i]);
                    }
                }
                /* 子弹上升 + 命中 */
                for (i = 0; i < MAX_SHOTS; i++) {
                    if (!shot_on[i]) continue;
                    shot_y[i] = shot_y[i] - SH / 50;
                    if (shot_y[i] < 0) { shot_on[i] = 0; continue; }
                    for (j = 0; j < MAX_ROCKS; j++) {
                        int dx, dy;
                        if (!rock_on[j]) continue;
                        dx = shot_x[i] - rock_x[j];
                        dy = shot_y[i] - rock_y[j];
                        if (dx * dx + dy * dy > rock_r[j] * rock_r[j]) continue;
                        shot_on[i] = 0;
                        rock_on[j] = 0;
                        score = score + 10;
                        /* 分裂:找两个空槽放小石头 */
                        if (rock_r[j] > 6) {
                            int k, placed = 0;
                            for (k = 0; k < MAX_ROCKS && placed < 2; k++) {
                                if (rock_on[k]) continue;
                                spawn_rock(k, rock_r[j] / 2 + 2);
                                rock_x[k] = rock_x[j] + (placed == 0 ? -1 : 1) * rock_r[j];
                                if (rock_x[k] < rock_r[k]) rock_x[k] = rock_r[k];
                                if (rock_x[k] > SW - rock_r[k]) rock_x[k] = SW - rock_r[k];
                                rock_y[k] = rock_y[j];
                                placed = placed + 1;
                            }
                        }
                        break;
                    }
                }
                /* 撞船 */
                for (i = 0; i < MAX_ROCKS; i++) {
                    int cy;
                    if (!rock_on[i]) continue;
                    cy = SH - ship_h - 26 + ship_h / 2;
                    if (rock_y[i] + rock_r[i] < cy - ship_h) continue;
                    if (rock_y[i] - rock_r[i] > cy + ship_h) continue;
                    if (rock_x[i] + rock_r[i] < ship_x) continue;
                    if (rock_x[i] - rock_r[i] > ship_x + ship_w) continue;
                    lives = lives - 1;
                    rock_on[i] = 0;
                    spawn_rock(i, rock_r[i]);
                    if (lives <= 0) { over = 1; done = 1; }
                }
            }

            draw();
            continue;
        }

        if (t == VML_MSG_WINDOWRESIZE) { relayout(); draw(); continue; }
        if (t == VML_MSG_WINDOWCLOSE) break;
    }

    ui_timer_kill(tid);
    ui_win_close();
    itoa(score, buf);
    printf("asteroids: score=%s lives=%d done\n", buf, lives);
    return 0;
}