dart/app/breakout.dart · 8373 字节 · 下载原文
// 打砖块(Breakout)—— Dart 版小游戏
// Breakout — a small game in the Dart edition
//
// 玩法(与 `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.
//
// ## Dart 这一路的写法约束(见 `Examples/dart/app/snake.dart` 文件头)
// ## Style constraints on this Dart path (see the header of `Examples/dart/app/snake.dart`)
//
// · **全部状态放 `main` 的局部变量**;颜色写十六进制;整除写 `~/`;数字用字符串插值;
// · **all state lives in locals of `main`**; hex colours; `~/` for integer division; string interpolation;
// · ⚠ 双循环里不要把循环内赋值的变量作实参 ⇒ 绘制内联表达式。
// · ⚠ never pass a variable assigned inside a nested loop as an argument => inline when drawing.
//
// 跑法 —— 手机:`vml run examples/dart/breakout.dart`
// How to run — on the phone: `vml run examples/dart/breakout.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/breakout.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/breakout.dart --screen 480x640 --frames /tmp/fr`
import 'vml_ui.dart';
void main() {
int sw = 360;
int sh = 620;
int padW = 70;
int padH = 12;
int ballR = 6;
int padX = 145;
int ballX = 180;
int ballY = 400;
int bvx = 3;
int bvy = -3;
int score = 0;
int lives = 3;
int won = 0;
int dead = 0;
int frames = 0;
int brickW = 45;
int brickH = 18;
int brickTop = 70;
int bricksLeft = 0;
int i = 0;
int t = 0;
int k = 0;
int done = 0;
int padY = 0;
int bx = 0;
int by = 0;
String hud = "";
int cBg = 0xFF101018;
int cPaddle = 0xFF51E86E;
int cBall = 0xFFE8E8F0;
int cText = 0xFFE8E8F0;
int cDim = 0xFF9AA0B0;
// 砖块 8×5
List<int> bricks = [];
i = 0;
while (i < 40) { bricks.add(1); i = i + 1; }
int lang = ui_scr_get_language();
ui_win_open("打砖块", 0, 0);
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) { sw = 360; }
if (sh <= 0) { sh = 620; }
brickW = sw ~/ 8;
brickH = sh ~/ 34;
if (brickH < 12) { brickH = 12; }
padW = sw ~/ 5;
if (padW < 40) { padW = 40; }
padH = sh ~/ 50;
if (padH < 8) { padH = 8; }
ballR = sw ~/ 60;
if (ballR < 4) { ballR = 4; }
bricksLeft = 40;
padX = sw ~/ 2;
ballX = sw ~/ 2;
ballY = sh - 120;
if (ballY < brickTop + 5 * brickH + 20) { ballY = brickTop + 5 * brickH + 20; }
bvx = 3;
bvy = -3;
score = 0;
lives = 3;
won = 0;
dead = 0;
int tid = ui_timer_set(16, 1);
ui_2d_present();
while (done == 0 && ui_win_is_closed() == 0) {
t = ui_msg_fetch_wait(16);
if (t == 10) { break; }
if (t == 6 || t == 4 || t == 7) {
padX = ui_msg_get_a();
if (padX < padW ~/ 2) { padX = padW ~/ 2; }
if (padX > sw - padW ~/ 2) { padX = sw - padW ~/ 2; }
if ((won != 0 || dead != 0) && t != 7) {
i = 0;
while (i < 40) { bricks[i] = 1; i = i + 1; }
bricksLeft = 40;
score = 0;
lives = 3;
won = 0;
dead = 0;
ballX = sw ~/ 2;
ballY = sh - 120;
if (ballY < brickTop + 5 * brickH + 20) { ballY = brickTop + 5 * brickH + 20; }
bvx = 3;
bvy = -3;
}
}
if (t == 1) {
k = ui_msg_get_a();
if (k == 27) { done = 1; }
if (k == 13 || k == 82) {
i = 0;
while (i < 40) { bricks[i] = 1; i = i + 1; }
bricksLeft = 40;
score = 0;
lives = 3;
won = 0;
dead = 0;
ballX = sw ~/ 2;
ballY = sh - 120;
if (ballY < brickTop + 5 * brickH + 20) { ballY = brickTop + 5 * brickH + 20; }
}
if (k == 37) { padX = padX - 24; }
if (k == 39) { padX = padX + 24; }
if (padX < padW ~/ 2) { padX = padW ~/ 2; }
if (padX > sw - padW ~/ 2) { padX = sw - padW ~/ 2; }
}
if (t == 9) {
frames = frames + 1;
if (frames >= 20000) { done = 1; }
if (won == 0 && dead == 0) {
ballX = ballX + bvx;
ballY = ballY + bvy;
if (ballX < ballR) { ballX = ballR; bvx = 0 - bvx; }
if (ballX > sw - ballR) { ballX = sw - ballR; bvx = 0 - bvx; }
if (ballY < ballR) { ballY = ballR; bvy = 0 - bvy; }
padY = sh - 70;
if (bvy > 0 && ballY + ballR >= padY && ballY - ballR <= padY + padH) {
if (ballX >= padX - padW ~/ 2 && ballX <= padX + padW ~/ 2) {
ballY = padY - ballR;
bvy = 0 - bvy;
}
}
// 砖块:扫描一遍,碰到的第一块消掉
i = 0;
while (i < 40) {
if (bricks[i] != 0) {
bx = (i % 8) * brickW;
by = brickTop + (i ~/ 8) * brickH;
if (ballX + ballR >= bx && ballX - ballR <= bx + brickW &&
ballY + ballR >= by && ballY - ballR <= by + brickH) {
bricks[i] = 0;
bricksLeft = bricksLeft - 1;
score = score + 10;
bvy = 0 - bvy;
if (bricksLeft == 0) { won = 1; }
i = 40;
}
}
if (i < 40) { i = i + 1; }
}
if (ballY > sh + ballR) {
lives = lives - 1;
if (lives <= 0) { dead = 1; }
ballX = sw ~/ 2;
ballY = sh - 120;
if (ballY < brickTop + 5 * brickH + 20) { ballY = brickTop + 5 * brickH + 20; }
bvx = 3;
bvy = -3;
}
}
}
if (t == 11) {
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) { sw = 360; }
if (sh <= 0) { sh = 620; }
brickW = sw ~/ 8;
brickH = sh ~/ 34;
if (brickH < 12) { brickH = 12; }
}
// ── 绘制(内联表达式,见文件头)──
ui_2d_clear(cBg);
i = 0;
while (i < 40) {
if (bricks[i] != 0) {
if (i ~/ 8 == 0) { ui_2d_rect((i % 8) * brickW + 1, brickTop + (i ~/ 8) * brickH + 1, brickW - 2, brickH - 2, 0xFFE06C50, 1, 0, 3); }
else if (i ~/ 8 == 1) { ui_2d_rect((i % 8) * brickW + 1, brickTop + (i ~/ 8) * brickH + 1, brickW - 2, brickH - 2, 0xFFD9B44A, 1, 0, 3); }
else if (i ~/ 8 == 2) { ui_2d_rect((i % 8) * brickW + 1, brickTop + (i ~/ 8) * brickH + 1, brickW - 2, brickH - 2, 0xFF51E86E, 1, 0, 3); }
else if (i ~/ 8 == 3) { ui_2d_rect((i % 8) * brickW + 1, brickTop + (i ~/ 8) * brickH + 1, brickW - 2, brickH - 2, 0xFF4A90D9, 1, 0, 3); }
else { ui_2d_rect((i % 8) * brickW + 1, brickTop + (i ~/ 8) * brickH + 1, brickW - 2, brickH - 2, 0xFF9B6CF0, 1, 0, 3); }
}
i = i + 1;
}
ui_2d_rect(padX - padW ~/ 2, sh - 70, padW, padH, cPaddle, 1, 0, 4);
ui_2d_circle(ballX, ballY, ballR, cBall, 1, 0);
hud = "$score";
ui_2d_text(10, 24, hud, cText, 15, 0);
if (lang == 0) { ui_2d_text(10, 46, "得分", cDim, 10, 0); }
else { ui_2d_text(10, 46, "score", cDim, 10, 0); }
hud = "$lives";
ui_2d_text(sw - 10, 24, hud, 0xFFE06C50, 16, 2);
if (lang == 0) { ui_2d_text(sw - 10, 46, "命", cDim, 10, 2); }
else { ui_2d_text(sw - 10, 46, "lives", cDim, 10, 2); }
if (won != 0) {
if (lang == 0) { ui_2d_text(sw ~/ 2, sh ~/ 2, "全打光了!点一下再来", cText, 18, 1); }
else { ui_2d_text(sw ~/ 2, sh ~/ 2, "Cleared! Tap to play again", cText, 18, 1); }
} else if (dead != 0) {
if (lang == 0) { ui_2d_text(sw ~/ 2, sh ~/ 2, "没命了,点一下再来", 0xFFE06C50, 18, 1); }
else { ui_2d_text(sw ~/ 2, sh ~/ 2, "Out of lives - tap to retry", 0xFFE06C50, 18, 1); }
} else {
if (lang == 0) { ui_2d_text(sw ~/ 2, sh - 34, "点 / 拖屏幕控制挡板", cDim, 11, 1); }
else { ui_2d_text(sw ~/ 2, sh - 34, "Tap / drag to move the paddle", cDim, 11, 1); }
}
ui_2d_present();
}
ui_timer_kill(tid);
ui_win_close();
}