dart/app/pong.dart · 7144 字节 · 下载原文
// 对打(Pong)—— Dart 版小游戏
// Pong — a small game in the Dart edition
//
// 玩法(与 `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.
//
// ## Dart 这一路的写法约束(见 `Examples/dart/app/snake.dart` 文件头)
// ## Style constraints on this Dart path (see the header of `Examples/dart/app/snake.dart`)
//
// · **全部状态放 `main` 的局部变量**;颜色写十六进制 `0xAARRGGBB`;整除写 `~/`;
// · **all state lives in locals of `main`**; colours are hexadecimal `0xAARRGGBB`; integer division is `~/`;
// · 数字用**字符串插值**。
// · numbers go through **string interpolation**.
//
// 跑法 —— 手机:`vml run examples/dart/pong.dart`
// How to run — on the phone: `vml run examples/dart/pong.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/pong.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/pong.dart --screen 480x640 --frames /tmp/fr`
import 'vml_ui.dart';
void main() {
// ── 全部状态都放 main 的局部变量(见文件头)──
int sw = 360;
int sh = 620;
int padW = 12;
int padH = 70;
int ballR = 6;
int leftY = 310;
int rightY = 310;
int ballX = 180;
int ballY = 310;
int bvx = -5;
int bvy = 3;
int scoreL = 0;
int scoreR = 0;
int ended = 0;
int frames = 0;
int seed = 20262;
int serveDir = 0;
int touchY = 0;
int i = 0;
int top = 0;
int t = 0;
int k = 0;
int done = 0;
String hudL = "";
String hudR = "";
int cBg = 0xFF101018;
int cLine = 0xFF2A2A36;
int cLeft = 0xFF51E86E;
int cRight = 0xFF4A90D9;
int cBall = 0xFFE8E8F0;
int cText = 0xFFE8E8F0;
int cDim = 0xFF9AA0B0;
int lang = ui_scr_get_language();
ui_win_open_ex("对打", 0, 0, 1, 1);
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) { sw = 360; }
if (sh <= 0) { sh = 620; }
padW = sw ~/ 28;
if (padW < 8) { padW = 8; }
padH = sh ~/ 6;
if (padH < 40) { padH = 40; }
ballR = sw ~/ 40;
if (ballR < 4) { ballR = 4; }
leftY = sh ~/ 2;
rightY = sh ~/ 2;
ballX = sw ~/ 2;
ballY = sh ~/ 2;
bvx = -5;
bvy = 3;
scoreL = 0;
scoreR = 0;
ended = 0;
serveDir = 0;
int tid = ui_timer_set(33, 1);
ui_2d_present();
while (done == 0 && ui_win_is_closed() == 0) {
t = ui_msg_fetch_wait(33);
if (t == 10) { break; }
if (t == 9) {
frames = frames + 1;
if (frames >= 6000) { done = 1; }
if (ended == 0) {
ballX = ballX + bvx;
ballY = ballY + bvy;
if (ballY < ballR) { ballY = ballR; bvy = 0 - bvy; }
if (ballY > sh - ballR) { ballY = sh - ballR; bvy = 0 - bvy; }
if (bvx < 0 && ballX <= 10 + padW + ballR && ballX >= 6) {
top = leftY - padH ~/ 2;
if (ballY >= top && ballY <= top + padH) {
ballX = 10 + padW + ballR;
bvx = 0 - bvx - 1;
if (bvx < -14) { bvx = -14; }
}
}
if (bvx > 0 && ballX >= sw - 10 - padW - ballR && ballX <= sw - 6) {
top = rightY - padH ~/ 2;
if (ballY >= top && ballY <= top + padH) {
ballX = sw - 10 - padW - ballR;
bvx = 0 - bvx + 1;
if (bvx > 14) { bvx = 14; }
}
}
if (ballX < 0 - ballR) {
scoreR = scoreR + 1;
if (scoreR >= 5) { ended = 1; }
ballX = sw ~/ 2;
ballY = sh ~/ 2;
if (serveDir == 0) { bvx = -5; serveDir = 1; } else { bvx = 5; serveDir = 0; }
seed = (seed * 1103 + 12345) % 32768;
if ((seed ~/ 256) % 2 == 0) { bvy = -3; } else { bvy = 3; }
}
if (ballX > sw + ballR) {
scoreL = scoreL + 1;
if (scoreL >= 5) { ended = 1; }
ballX = sw ~/ 2;
ballY = sh ~/ 2;
if (serveDir == 0) { bvx = -5; serveDir = 1; } else { bvx = 5; serveDir = 0; }
seed = (seed * 1103 + 12345) % 32768;
if ((seed ~/ 256) % 2 == 0) { bvy = -3; } else { bvy = 3; }
}
if (rightY < ballY) { rightY = rightY + 4; }
if (rightY > ballY) { rightY = rightY - 4; }
if (leftY < padH ~/ 2) { leftY = padH ~/ 2; }
if (leftY > sh - padH ~/ 2) { leftY = sh - padH ~/ 2; }
if (rightY < padH ~/ 2) { rightY = padH ~/ 2; }
if (rightY > sh - padH ~/ 2) { rightY = sh - padH ~/ 2; }
}
}
if (t == 6 || t == 4) {
touchY = ui_msg_get_b();
if (ended != 0) {
scoreL = 0;
scoreR = 0;
ended = 0;
frames = 0;
ballX = sw ~/ 2;
ballY = sh ~/ 2;
} else {
leftY = touchY;
}
}
if (t == 7) {
touchY = ui_msg_get_b();
if (ended == 0) { leftY = touchY; }
}
if (t == 1) {
k = ui_msg_get_a();
if (k == 27) { done = 1; }
if (k == 13 || k == 82) {
scoreL = 0;
scoreR = 0;
ended = 0;
frames = 0;
}
}
if (t == 11) {
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) { sw = 360; }
if (sh <= 0) { sh = 620; }
padW = sw ~/ 28;
if (padW < 8) { padW = 8; }
padH = sh ~/ 6;
if (padH < 40) { padH = 40; }
ballR = sw ~/ 40;
if (ballR < 4) { ballR = 4; }
}
// ── 绘制 ──
ui_2d_clear(cBg);
i = 10;
while (i + 16 <= sh - 10) {
ui_2d_rect(sw ~/ 2 - 2, i, 4, 16, cLine, 1, 0, 0);
i = i + 32;
}
ui_2d_rect(10, leftY - padH ~/ 2, padW, padH, cLeft, 1, 0, 4);
ui_2d_rect(sw - 10 - padW, rightY - padH ~/ 2, padW, padH, cRight, 1, 0, 4);
ui_2d_circle(ballX, ballY, ballR, cBall, 1, 0);
hudL = "$scoreL";
hudR = "$scoreR";
ui_2d_text(sw ~/ 2 - 70, 28, hudL, cText, 26, 1);
ui_2d_text(sw ~/ 2 + 70, 28, hudR, cText, 26, 1);
if (ended != 0) {
if (lang == 0) {
if (scoreL >= 5) { ui_2d_text(sw ~/ 2, sh ~/ 2 - 40, "你赢了!点一下再来", cLeft, 20, 1); }
else { ui_2d_text(sw ~/ 2, sh ~/ 2 - 40, "电脑赢了,点一下再来", 0xFFE06C50, 20, 1); }
} else {
if (scoreL >= 5) { ui_2d_text(sw ~/ 2, sh ~/ 2 - 40, "You win! Tap to retry", cLeft, 20, 1); }
else { ui_2d_text(sw ~/ 2, sh ~/ 2 - 40, "AI wins - tap to retry", 0xFFE06C50, 20, 1); }
}
} else {
if (lang == 0) { ui_2d_text(sw ~/ 2, 58, "左半屏滑动控制球板,先到 5 分", cDim, 11, 1); }
else { ui_2d_text(sw ~/ 2, 58, "Drag on the left half; first to 5", cDim, 11, 1); }
}
ui_2d_present();
}
ui_timer_kill(tid);
ui_win_close();
}