rust/app/pong.rs · 8516 字节 · 下载原文
// 对打(Pong)—— 用 **Rust** 写的手机小游戏
// Pong — a small phone game written in **Rust**
//
// 玩法(与 `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.
//
// ## Rust 这一路的写法约束(见 `Examples/rust/app/snake.rs` 文件头)
// ## Style constraints on this Rust path (see the header of `Examples/rust/app/snake.rs`)
//
// · **全部状态放 `main` 的局部变量**,循环一律 `while`;
// · **all state lives in locals of `main`**, loops are always `while`;
// · **循环变量在 `main` 顶部声明一次**(本前端不支持同名 `let` 的重复声明);
// · **loop variables are declared once at the top of `main`** (this frontend does not support
// repeated `let` / shadowing);
// · 数字转字符串走库里的 `int_to_str`(共享静态缓冲,取一次立刻用掉);
// · numbers go through the library's `int_to_str` (one shared static buffer; take and use it at once);
// · 颜色写负数十进制。
// · colours are negative decimals.
//
// 跑法 —— 手机:`vml run examples/rust/pong.rs`
// How to run — on the phone: `vml run examples/rust/pong.rs`
// 跑法 —— 桌面:`vmlcli Examples/rust/pong.rs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/rust/pong.rs --screen 480x640 --frames /tmp/fr`
#include <vml_ui.rs>
use vml_ui::*;
fn main() {
let mut lang = ui_scr_get_language();
let mut sw = 360;
let mut sh = 620;
let mut pad_w = 12;
let mut pad_h = 70;
let mut ball_r = 6;
let mut left_y = 310;
let mut right_y = 310;
let mut ball_x = 180;
let mut ball_y = 310;
let mut bvx = -5;
let mut bvy = 3;
let mut score_l = 0;
let mut score_r = 0;
let mut ended = 0;
let mut frames = 0;
let mut seed = 20262;
let mut serve_dir = 0;
let mut touch_y = 0;
let mut i = 0;
let mut top = 0;
let mut tid = 0;
let mut t = 0;
let mut k = 0;
let mut done = 0;
let c_bg = -15724520; // 0xFF101018
let c_line = -14013898; // 0xFF2A2A36
let c_left = -11409298; // 0xFF51E86E
let c_right = -11890471; // 0xFF4A90D9
let c_ball = -1513232; // 0xFFE8E8F0
let c_text = -1513232; // 0xFFE8E8F0
let c_dim = -6643536; // 0xFF9AA0B0
if lang == 0 { ui_win_open("对打", 0, 0); }
else { ui_win_open("Pong", 0, 0); }
ui_scr_set_keep_on(1);
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; }
left_y = sh / 2;
right_y = sh / 2;
ball_x = sw / 2;
ball_y = sh / 2;
bvx = -5;
bvy = 3;
score_l = 0;
score_r = 0;
ended = 0;
serve_dir = 0;
ui_2d_clear(c_bg);
ui_2d_text(sw / 2, 58, "左半屏滑动控制球板", c_dim, 11, 1);
ui_2d_present();
tid = ui_timer_set(33, 0);
while done == 0 {
if ui_win_is_closed() != 0 { done = 1; }
if done != 0 { break; }
t = ui_msg_fetch_wait(0);
if t == 10 { done = 1; }
if t == 9 {
frames = frames + 1;
if frames >= 6000 { done = 1; }
if ended == 0 {
ball_x = ball_x + bvx;
ball_y = ball_y + bvy;
if ball_y < ball_r { ball_y = ball_r; bvy = 0 - bvy; }
if ball_y > sh - ball_r { ball_y = sh - ball_r; bvy = 0 - bvy; }
if bvx < 0 && ball_x <= 10 + pad_w + ball_r && ball_x >= 6 {
top = left_y - pad_h / 2;
if ball_y >= top && ball_y <= top + pad_h {
ball_x = 10 + pad_w + ball_r;
bvx = 0 - bvx - 1;
if bvx < -14 { bvx = -14; }
}
}
if bvx > 0 && ball_x >= sw - 10 - pad_w - ball_r && ball_x <= sw - 6 {
top = right_y - pad_h / 2;
if ball_y >= top && ball_y <= top + pad_h {
ball_x = sw - 10 - pad_w - ball_r;
bvx = 0 - bvx + 1;
if bvx > 14 { bvx = 14; }
}
}
if ball_x < 0 - ball_r {
score_r = score_r + 1;
if score_r >= 5 { ended = 1; }
ball_x = sw / 2;
ball_y = sh / 2;
if serve_dir == 0 { bvx = -5; serve_dir = 1; }
else { bvx = 5; serve_dir = 0; }
seed = (seed * 1103 + 12345) % 32768;
if (seed / 256) % 2 == 0 { bvy = -3; }
else { bvy = 3; }
}
if ball_x > sw + ball_r {
score_l = score_l + 1;
if score_l >= 5 { ended = 1; }
ball_x = sw / 2;
ball_y = sh / 2;
if serve_dir == 0 { bvx = -5; serve_dir = 1; }
else { bvx = 5; serve_dir = 0; }
seed = (seed * 1103 + 12345) % 32768;
if (seed / 256) % 2 == 0 { bvy = -3; }
else { bvy = 3; }
}
if right_y < ball_y { right_y = right_y + 4; }
if right_y > ball_y { right_y = right_y - 4; }
if left_y < pad_h / 2 { left_y = pad_h / 2; }
if left_y > sh - pad_h / 2 { left_y = sh - pad_h / 2; }
if right_y < pad_h / 2 { right_y = pad_h / 2; }
if right_y > sh - pad_h / 2 { right_y = sh - pad_h / 2; }
}
}
if t == 6 || t == 4 {
touch_y = ui_msg_get_b();
if ended != 0 {
score_l = 0;
score_r = 0;
ended = 0;
frames = 0;
ball_x = sw / 2;
ball_y = sh / 2;
} else {
left_y = touch_y;
}
}
if t == 7 {
touch_y = ui_msg_get_b();
if ended == 0 { left_y = touch_y; }
}
if t == 1 {
k = ui_msg_get_a();
if k == 27 { done = 1; }
if k == 13 || k == 82 {
score_l = 0;
score_r = 0;
ended = 0;
frames = 0;
}
}
ui_2d_clear(c_bg);
i = 10;
while i + 16 <= sh - 10 {
ui_2d_rect(sw / 2 - 2, i, 4, 16, c_line, 1, 0, 0);
i = i + 32;
}
ui_2d_rect(10, left_y - pad_h / 2, pad_w, pad_h, c_left, 1, 0, 4);
ui_2d_rect(sw - 10 - pad_w, right_y - pad_h / 2, pad_w, pad_h, c_right, 1, 0, 4);
ui_2d_circle(ball_x, ball_y, ball_r, c_ball, 1, 0);
ui_2d_text(sw / 2 - 70, 28, int_to_str(score_l), c_text, 26, 1);
ui_2d_text(sw / 2 + 70, 28, int_to_str(score_r), c_text, 26, 1);
if ended != 0 {
if lang == 0 {
if score_l >= 5 { 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 score_l >= 5 { 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();
}
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
}