kotlin/app/pong.kt · 7613 字节 · 下载原文
// 对打(Pong)—— 用 **Kotlin** 写的手机小游戏
// Pong — a small phone game written in **Kotlin**
//
// 玩法(与 `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.
//
// ## 这一份用到的 Kotlin 特性
// ## Kotlin features used here
//
// · `const val` 颜色常量与**顶层 `var` 状态**;
// · `const val` colour constants and **file-level `var` state**;
// · **字符串模板**(`"$pgScoreL"`)画比分;
// · **string templates** (`"$pgScoreL"`) for the score;
// · **多参数函数**(`fun pgStep(...)` 形态的无参辅助函数)。
// · zero-argument helpers, the shape used throughout these examples.
//
// 跑法 —— 手机:`vml run examples/kotlin/pong.kt`
// How to run — on the phone: `vml run examples/kotlin/pong.kt`
// 跑法 —— 桌面:`vmlcli Examples/kotlin/pong.kt --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/kotlin/pong.kt --screen 480x640 --frames /tmp/fr`
// ── 显式引用:下面用到的 `ui_*` 来自共享库 vmlui.vml ──────────────────
// ── Explicit reference: the `ui_*` used below come from the shared library vmlui.vml ──
// ⚠ `.*` 必须有:`import vmlui`(无 `.*`)会被 `ExtractImports` 的正则**静默忽略**。
// ⚠ The `.*` is required: `import vmlui` (without `.*`) is silently ignored.
#include <vml_ui.kt>
import vmlui.*
const val PG_MAX_FRAMES = 6000
const val PG_WIN_SCORE = 5
const val PG_BG = 0xFF101018
const val PG_LINE = 0xFF2A2A36
const val PG_LEFT = 0xFF51E86E
const val PG_RIGHT = 0xFF4A90D9
const val PG_BALL = 0xFFE8E8F0
const val PG_TEXT = 0xFFE8E8F0
const val PG_DIM = 0xFF9AA0B0
// 状态(顶层 var,Kotlin 这一路可用)
// State (file-level vars; reliable on this Kotlin path)
var pgLx = 0
var pgLy = 0
var pgRy = 0
var pgBx = 0
var pgBy = 0
var pgBvx = 5
var pgBvy = 3
var pgScoreL = 0
var pgScoreR = 0
var pgW = 360
var pgH = 620
var pgPadW = 12
var pgPadH = 70
var pgBallR = 6
var pgLang = 0
var pgFrames = 0
var pgEnded = 0
var pgServeDir = 0
var pgSeed = 20262
var pgTouchY = 0
fun pgRnd(n: Int): Int {
pgSeed = (pgSeed * 1103 + 12345) % 32768
return (pgSeed / 256) % n
}
fun pgLayout() {
pgW = ui_scr_get_w()
pgH = ui_scr_get_h()
if (pgW <= 0) pgW = 360
if (pgH <= 0) pgH = 620
pgPadW = pgW / 28
if (pgPadW < 8) pgPadW = 8
pgPadH = pgH / 6
if (pgPadH < 40) pgPadH = 40
pgBallR = pgW / 40
if (pgBallR < 4) pgBallR = 4
}
fun pgServe() {
pgBx = pgW / 2
pgBy = pgH / 2
if (pgServeDir == 0) { pgBvx = -5; pgServeDir = 1 }
else { pgBvx = 5; pgServeDir = 0 }
if (pgRnd(2) == 0) pgBvy = -3
else pgBvy = 3
}
fun pgNewGame() {
pgLx = pgW / 2
pgLy = pgH / 2
pgRy = pgH / 2
pgScoreL = 0
pgScoreR = 0
pgEnded = 0
pgServeDir = 0
pgFrames = 0
pgServe()
}
fun pgStep() {
pgBx = pgBx + pgBvx
pgBy = pgBy + pgBvy
if (pgBy < pgBallR) { pgBy = pgBallR; pgBvy = 0 - pgBvy }
if (pgBy > pgH - pgBallR) { pgBy = pgH - pgBallR; pgBvy = 0 - pgBvy }
if (pgBvx < 0 && pgBx <= 10 + pgPadW + pgBallR && pgBx >= 6) {
var top = pgLy - pgPadH / 2
if (pgBy >= top && pgBy <= top + pgPadH) {
pgBx = 10 + pgPadW + pgBallR
pgBvx = 0 - pgBvx - 1
if (pgBvx < -14) pgBvx = -14
}
}
if (pgBvx > 0 && pgBx >= pgW - 10 - pgPadW - pgBallR && pgBx <= pgW - 6) {
var top2 = pgRy - pgPadH / 2
if (pgBy >= top2 && pgBy <= top2 + pgPadH) {
pgBx = pgW - 10 - pgPadW - pgBallR
pgBvx = 0 - pgBvx + 1
if (pgBvx > 14) pgBvx = 14
}
}
if (pgBx < 0 - pgBallR) {
pgScoreR = pgScoreR + 1
if (pgScoreR >= PG_WIN_SCORE) pgEnded = 1
pgServe()
}
if (pgBx > pgW + pgBallR) {
pgScoreL = pgScoreL + 1
if (pgScoreL >= PG_WIN_SCORE) pgEnded = 1
pgServe()
}
if (pgRy < pgBy) pgRy = pgRy + 4
if (pgRy > pgBy) pgRy = pgRy - 4
if (pgLy < pgPadH / 2) pgLy = pgPadH / 2
if (pgLy > pgH - pgPadH / 2) pgLy = pgH - pgPadH / 2
if (pgRy < pgPadH / 2) pgRy = pgPadH / 2
if (pgRy > pgH - pgPadH / 2) pgRy = pgH - pgPadH / 2
}
fun pgDraw() {
ui_2d_clear(PG_BG)
var y = 10
while (y + 16 <= pgH - 10) {
ui_2d_rect(pgW / 2 - 2, y, 4, 16, PG_LINE, 1, 0, 0)
y = y + 32
}
ui_2d_rect(10, pgLy - pgPadH / 2, pgPadW, pgPadH, PG_LEFT, 1, 0, 4)
ui_2d_rect(pgW - 10 - pgPadW, pgRy - pgPadH / 2, pgPadW, pgPadH, PG_RIGHT, 1, 0, 4)
ui_2d_circle(pgBx, pgBy, pgBallR, PG_BALL, 1, 0)
ui_2d_text(pgW / 2 - 70, 28, "$pgScoreL", PG_TEXT, 26, VML_ANCHOR_CENTER)
ui_2d_text(pgW / 2 + 70, 28, "$pgScoreR", PG_TEXT, 26, VML_ANCHOR_CENTER)
if (pgEnded != 0) {
if (pgLang == 0) {
if (pgScoreL >= PG_WIN_SCORE) { ui_2d_text(pgW / 2, pgH / 2 - 40, "你赢了!点一下再来", PG_LEFT, 20, VML_ANCHOR_CENTER) }
else { ui_2d_text(pgW / 2, pgH / 2 - 40, "电脑赢了,点一下再来", 0xFFE06C50, 20, VML_ANCHOR_CENTER) }
} else {
if (pgScoreL >= PG_WIN_SCORE) { ui_2d_text(pgW / 2, pgH / 2 - 40, "You win! Tap to retry", PG_LEFT, 20, VML_ANCHOR_CENTER) }
else { ui_2d_text(pgW / 2, pgH / 2 - 40, "AI wins - tap to retry", 0xFFE06C50, 20, VML_ANCHOR_CENTER) }
}
} else {
if (pgLang == 0) { ui_2d_text(pgW / 2, 58, "左半屏滑动控制球板,先到 5 分", PG_DIM, 11, VML_ANCHOR_CENTER) }
else { ui_2d_text(pgW / 2, 58, "Drag on the left half; first to 5", PG_DIM, 11, VML_ANCHOR_CENTER) }
}
ui_2d_present()
}
fun main() {
pgLang = ui_scr_get_language()
if (pgLang == 0) { ui_win_open_ex("对打", 0, 0, VML_WIN_PORTRAIT, VML_WIN_NO_GAMEPAD) }
else { ui_win_open_ex("Pong", 0, 0, VML_WIN_PORTRAIT, VML_WIN_NO_GAMEPAD) }
ui_scr_set_keep_on(1)
pgLayout()
pgNewGame()
pgDraw()
var tid = ui_timer_set(33, 1)
var done = 0
while (done == 0 && ui_win_is_closed() == 0) {
var t = ui_msg_fetch_wait(33)
if (t == 10) { break }
if (t == 9) {
pgFrames = pgFrames + 1
if (pgFrames >= PG_MAX_FRAMES) done = 1
if (pgEnded == 0) pgStep()
pgDraw()
continue
}
if (t == 6 || t == 4) {
pgTouchY = ui_msg_get_b()
if (pgEnded != 0) pgNewGame()
else pgLy = pgTouchY
pgDraw()
continue
}
if (t == 7) {
pgTouchY = ui_msg_get_b()
if (pgEnded == 0) pgLy = pgTouchY
pgDraw()
continue
}
if (t == 11) { pgLayout(); pgDraw(); continue }
if (t == 1) {
var k = ui_msg_get_a()
if (k == 27) { done = 1 }
if (k == 13 || k == 82) { pgNewGame(); pgDraw() }
continue
}
}
ui_timer_kill(tid)
ui_scr_set_keep_on(0)
ui_win_close()
}