python/app/contra.py · 29002 字节 · 下载原文
# contra.py —— 魂斗罗风格横版射击(VML 的 Python 前端)
# contra.py — a Contra-style run-and-gun (VML's Python frontend)
#
# ⚠ 这是**玩法上的致敬**:横版跑打、跳跃、成排的敌兵、子弹对射、三条命。
# ⚠ This is a **gameplay homage**: side-scrolling run-and-gun, jumping, waves of soldiers,
# 但**没有任何原作的角色名、形象或素材** —— 玩家和敌兵都是圆形、矩形、线条现场画出来的,
# bullet trading and three lives. It contains **no original character names, art or assets** —
# 名字也是本文件自己起的。
# the player and the soldiers are drawn from circles, rectangles and lines at run time, and the
# names are made up by this file.
#
# ## 操作
# ## Controls
#
# 手机:左下角 **◀ / ▶** 移动,右下角 **JUMP**(跳)+ **FIRE**(按住连发)。
# Phone: **◀ / ▶** at the lower left move; **JUMP** and **FIRE** (hold to auto-fire) at the
# 键盘:← → 移动,↑ / W 跳,空格 / Z 按住射击,ESC 退出;结束后点屏幕/回车重开。
# lower right. Keyboard: ← → move, ↑ / W jump, hold Space / Z to shoot, ESC quits; after the
# game ends, tap the screen / press Enter to restart.
#
# ## 这个前端的写法约定(实测,不是偏好)
# ## Conventions of this front end (measured, not preferences)
#
# · **状态的修改一律留在顶层**:Python 的函数体里赋值就是**新的局部量**(本前端守这条),
# · **State mutations stay at the top level**: inside a Python function an assignment creates a
# 要改模块级状态得写 `global`。所以本文件把所有"改状态"的代码平铺在主循环里,
# **new local** (the frontend honours this), so mutating module state needs `global`. This
# 函数只做**只读**的事(算矩形、判命中、画一格、画数字)。
# file keeps every mutation flat in the main loop and lets functions be **read-only**
# (hit tests, drawing a cell, drawing a number).
#
# · **整数物理**:位置存「像素 ×10」,速度是「每 0.1 像素每拍」;浮点在本前端没有装箱
# · **Integer physics**: positions are "pixels x10" and velocities are "0.1 px per tick";
# (`1.5 + 2.0` 会变成位型),所以全文件没有一个小数。
# floats are not boxed here (`1.5 + 2.0` becomes a bit pattern), so there is not a single
# decimal in this file.
#
# · **没有 int → 字符串**(`str()` 是恒等、`chr()` 返回栈地址)⇒ 分数用**字形表** `DIG`
# · **No int → string** (`str()` is the identity, `chr()` returns a stack address) ⇒ the score
# 逐位画(`draw_num`);屏幕上所有文字都是**字面量**。
# is drawn digit by digit through the glyph table `DIG` (`draw_num`); every string on screen
# is a literal.
#
# · **触摸用轮询**(`ui_touch_fetch/get_x/get_y/is_down`),不依赖 `TOUCHUP`;
# · **Touch is polled** (`ui_touch_fetch/get_x/get_y/is_down`), never relying on `TOUCHUP`;
# 移动/连发看**按住**,跳跃看**按下的那一沿**(`DOWN0` 前后两拍比较)。
# movement/auto-fire read the **held** state; jumping uses the **rising edge** (compare
# `DOWN0` between ticks).
#
# · 函数名一律带 `cn_` 前缀 —— 本前端里**与库符号同名的函数会被库静默接管**
# · Every helper is prefixed `cn_` — a function whose name collides with a library symbol is
# **silently taken over by the library** on this frontend (e.g. `add`/`min`/`max`).
#
# 跑法:手机 `vml run examples/python/app/contra.py`;
# How to run: phone `vml run examples/python/app/contra.py`;
# 桌面 `vmlcli Examples/python/app/contra.py --screen 411x726 --frames /tmp/fr`
# desktop `vmlcli Examples/python/app/contra.py --screen 411x726 --frames /tmp/fr`
# `--arg demo` 是无头验收:固定敌兵 + 脚本化射击/跳跃/中弹,打印判据后自己退出。
# `--arg demo` is the headless acceptance mode: a fixed soldier plus scripted fire / jump /
# hit, printed checkpoints, then self-exit.
# 显式引入共享库声明(GenLib 生成,勿手写):形参个数写错在**编译期**报错。
# Explicitly import the shared-library declarations (generated by GenLib, do not hand-write):
# a wrong argument count becomes a **compile-time** error.
import vml_ui
# ── 窗口 / 坐标系 ──────────────────────────────────────────────────────
# ── Window / coordinate space ─────────────────────────────────────────
LANG = ui_scr_get_language()
ui_win_open_ex("魂斗罗" if LANG == 0 else "Contra", 0, 0, 0, 0)
ui_scr_set_keep_on(1)
SW = ui_scr_get_w()
SH = ui_scr_get_h()
if SW <= 0:
SW = 411
if SH <= 0:
SH = 726
# 地面高度随画布缩:大画布维持原来 170px;240 方形画布上 170px 会把可玩区压到 70px。
# Ground band shrinks with the canvas: 170px on large screens, but on a 240px square canvas
# 170px would leave only 70px of playfield.
GROUND_H = SH // 4
if GROUND_H < 90:
GROUND_H = 90
if GROUND_H > 170:
GROUND_H = 170
GY = SH - GROUND_H # 地面 y(像素)
# ground y in px
PBW = 26 # 玩家宽(碰撞盒)
# player collision width
PBH = 38 # 玩家高
# player collision height
EBW = 22 # 敌兵宽
# soldier collision width
EBH = 34
TICK_MS = 16
MAXPB = 12 # 玩家子弹槽位数
# player bullet slots
MAXEB = 12 # 敌兵子弹槽位数
# enemy bullet slots
MAXEN = 6 # 同屏敌兵上限
# soldiers on screen at once
DIG = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
DEMO = 0
if ui_arg_get_count() > 1:
DEMO = 1
# ── 状态(模块级;只读函数直接读)──────────────────────────────────────
# ── State (module level; read-only helpers read it) ───────────────────
PX = 60
PY10 = (GY - PBH) * 10
VY10 = 0
ONG = 1 # 站在地面/平台上
# standing on ground or a platform
FACE = 1 # 1 = 朝右
# 1 = facing right
LIVES = 3
INV = 0 # 无敌剩余拍数(受击后闪烁)
# remaining invulnerability ticks (flashing)
SCORE = 0
KILLS = 0
TICK = 0
OVER = 0
MV = 0 # -1 / 0 / +1(本拍的水平输入)
# -1 / 0 / +1 horizontal input for this tick
JMP = 0 # 本拍起跳(只在按下的那一沿置 1)
# jump request, set only on the rising edge
FIRE_HELD = 0 # 本拍按住射击
# fire held this tick
FCOOL = 0 # 射击冷却
# fire cooldown
SPAWN = 60 # 下一次刷敌倒计时
# ticks until the next spawn
ACC = 0
LASTMS = 0
FRAMES = 0
DOWN0 = 0 # 上一拍手指是否按着(判上升沿)
# was a finger down last tick (rising-edge detection)
TX = 0
TY = 0
# 玩家子弹:每槽 4 格 [x10, y10, alive, pad]
# Player bullets: 4 slots each [x10, y10, alive, pad]
PB = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 敌兵子弹
# Enemy bullets
EB = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 敌兵:每槽 5 格 [x10, y10, alive, cool, hp]
# Soldiers: 5 slots each [x10, y10, alive, cool, hp]
EN = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 平台:两个沙袋台(玩家能跳上去)
# Platforms: two sandbag ledges (the player can jump onto them)
PLX1 = SW * 34 // 100
PLX2 = SW * 61 // 100
# 平台离地高度按可玩区(HUD 下沿到地面)压:240 方形画布可玩区只有 ~90px,
# 原来固定 -96/-168 会把两块沙袋台整块推到画布外。
# Platform heights scale with the playfield (HUD bottom to ground): on a 240px square canvas
# the playfield is only ~90px, and fixed -96/-168 pushed both ledges off-screen.
_PH = GY - 56
if _PH < 60:
_PH = 60
_PH1 = _PH // 2
if _PH1 > 96:
_PH1 = 96
_PH2 = _PH * 3 // 4
if _PH2 > 168:
_PH2 = 168
PLY1 = GY - _PH1
PLY2 = GY - _PH2
PLW1 = SW * 30 // 100
if PLW1 > 92:
PLW1 = 92
PLW2 = SW * 28 // 100
if PLW2 > 82:
PLW2 = 82
# 底部按钮:左下 ◀ ▶,右下 JUMP / FIRE
# Bottom buttons: ◀ ▶ at the lower left, JUMP / FIRE at the lower right
# 底部按钮:按屏宽四等分,窄屏(240)上原来的 64px×4 + 边距 = 256px 会互相压叠。
# Bottom buttons: four equal shares of the screen width; the original 64px x 4 + margins
# overlapped each other on a 240px screen.
_M = SW // 40
if _M < 8:
_M = 8
if _M > 12:
_M = 12
BTH = SH // 4
if BTH > 64:
BTH = 64
if BTH < 40:
BTH = 40
BTW = (SW - 5 * _M) // 4
if BTW > 64:
BTW = 64
if BTW < 36:
BTW = 36
BTY = SH - BTH - _M
BLX = _M
BRX = _M * 2 + BTW
BFX = SW - _M - BTW
BJX = BFX - _M - BTW
# 配色
# Palette
C_SKY = 0xFF1B2A44
C_SKY2 = 0xFF243B5C
C_HILL = 0xFF162033
C_HILL2 = 0xFF1D2B42
C_GROUND = 0xFF3F6B3A
C_DIRT = 0xFF5C442E
C_TEXT = 0xFFE8EEF7
C_DIM = 0xFF93A1B5
C_GOLD = 0xFFFFC040
C_PLAYER = 0xFF4A90D9
C_HELMET = 0xFF51E86E
C_SKIN = 0xFFE8B98A
C_GUN = 0xFF2B2F3A
C_ENEMY = 0xFFE06050
C_ENEMY2 = 0xFF8E3A30
C_PB = 0xFFFFE060
C_EB = 0xFFFF9A40
C_BTN = 0xFF2A3344
C_BTN_ON = 0xFF3E5474
C_SAND = 0xFFB08A4A
C_OVER = 0xCC000000
# ── 只读助手 ──────────────────────────────────────────────────────────
# ── Read-only helpers ─────────────────────────────────────────────────
def cn_rect_hit(x, y, rx, ry, rw, rh):
if x < rx:
return 0
if y < ry:
return 0
if x > rx + rw:
return 0
if y > ry + rh:
return 0
return 1
def cn_player_box():
# 返回 [x0, y0, x1, y1](像素)
# returns [x0, y0, x1, y1] in px
return [PX - PBW // 2, PY10 // 10, PX + PBW // 2, PY10 // 10 + PBH]
def cn_enemy_box(i):
o = i * 5
ex = EN[o] // 10
ey = EN[o + 1] // 10
return [ex - EBW // 2, ey, ex + EBW // 2, ey + EBH]
def cn_boxes_overlap(a, b):
if a[2] < b[0] or b[2] < a[0]:
return 0
if a[3] < b[1] or b[3] < a[1]:
return 0
return 1
def cn_draw_num(x, y, v, col, size):
# 逐位画数字(没有 int → 字符串)
# draw a number digit by digit (there is no int -> string)
p = 100000
started = 0
while p > 0:
d = (v // p) % 10
if d != 0 or started == 1 or p == 1:
ui_2d_text(x, y, DIG[d], col, size, 0)
x = x + size * 14 // 25
started = 1
p = p // 10
def cn_draw_player():
# 玩家:身体/头盔/脸/枪;受击无敌时隔拍闪烁
# player: body / helmet / face / gun; flickers while invulnerable
if INV > 0 and (TICK // 4) % 2 == 0:
return
x = PX - PBW // 2
y = PY10 // 10
ui_2d_rect(x + 4, y + 16, PBW - 8, PBH - 16, C_PLAYER, 1, 0, 4)
ui_2d_circle(PX, y + 12, 9, C_SKIN, 1, 0)
ui_2d_rect(x + 2, y + 2, PBW - 4, 8, C_HELMET, 1, 0, 5)
ui_2d_rect(x + PBW - 2, y + 20, 14, 5, C_GUN, 1, 0, 2)
ui_2d_rect(x + 4, y + PBH - 8, 6, 8, C_GUN, 1, 0, 2)
ui_2d_rect(x + PBW - 10, y + PBH - 8, 6, 8, C_GUN, 1, 0, 2)
def cn_draw_enemy(i):
o = i * 5
ex = EN[o] // 10
ey = EN[o + 1] // 10
ui_2d_rect(ex - EBW // 2 + 4, ey + 14, EBW - 8, EBH - 14, C_ENEMY2, 1, 0, 4)
ui_2d_circle(ex, ey + 10, 8, C_SKIN, 1, 0)
ui_2d_rect(ex - EBW // 2 + 2, ey, EBW - 4, 7, C_ENEMY, 1, 0, 4)
ui_2d_rect(ex - EBW // 2 - 10, ey + 18, 12, 5, C_GUN, 1, 0, 2)
def cn_draw_button(x, y, w, h, label, on):
c = C_BTN
if on != 0:
c = C_BTN_ON
# 字号跟着按钮宽走:240 屏上按钮只有 50px 宽,15px 的 "JUMP" 会顶出去。
# Font size follows the button width: on a 240px screen the buttons are only 50px wide,
# and a 15px "JUMP" would overflow.
fs = w // 4
if fs > 15:
fs = 15
if fs < 10:
fs = 10
ui_2d_rect(x, y, w, h, c, 1, 0, 12)
ui_2d_text(x + w // 2, y + (h - fs) // 2 + 2, label, C_TEXT, fs, 1)
# ── 开局(顶层赋值 = 写模块级;重开时同一段再跑一次)─────────────────
# ── New game (top-level assignments write module state; the same block reruns on restart)
PX = 60
PY10 = (GY - PBH) * 10
VY10 = 0
ONG = 1
FACE = 1
LIVES = 3
INV = 0
SCORE = 0
KILLS = 0
TICK = 0
OVER = 0
MV = 0
JMP = 0
FIRE_HELD = 0
FCOOL = 0
SPAWN = 60
FRAMES = 0
DOWN0 = 0
KEYUP0 = 0
DEMO_DONE = 0
i = 0
while i < 48:
PB[i] = 0
EB[i] = 0
i = i + 1
i = 0
while i < 30:
EN[i] = 0
i = i + 1
# ── 只读:支撑面 / 平台落点 ────────────────────────────────────────────
# ── Read-only: support / platform landing ─────────────────────────────
def cn_on_platform(x, foot):
if x + PBW // 2 > PLX1 and x - PBW // 2 < PLX1 + PLW1:
if foot >= PLY1 and foot <= PLY1 + 6:
return 1
if x + PBW // 2 > PLX2 and x - PBW // 2 < PLX2 + PLW2:
if foot >= PLY2 and foot <= PLY2 + 6:
return 1
return 0
def cn_supported(x, foot):
if foot >= GY - 1:
return 1
return cn_on_platform(x, foot)
def cn_land_top(x, prevfoot, foot):
# 本拍脚下穿过哪个平台顶(没有则 -1)
# which platform top the feet crossed this tick (-1 = none)
if prevfoot <= PLY1 and foot >= PLY1:
if x + PBW // 2 > PLX1 and x - PBW // 2 < PLX1 + PLW1:
return PLY1
if prevfoot <= PLY2 and foot >= PLY2:
if x + PBW // 2 > PLX2 and x - PBW // 2 < PLX2 + PLW2:
return PLY2
return -1
# ── 绘制 ──────────────────────────────────────────────────────────────
# ── Drawing ───────────────────────────────────────────────────────────
def cn_draw():
ui_2d_clear(C_SKY)
# 天空渐变带原本固定 260px 高,小画布上 y 变负数 ⇒ 夹回 0(外观不变,只是不再越界)。
# The sky band was a fixed 260px tall; on a small canvas its y went negative -- clamp to 0
# (same look, no off-canvas rect).
_sk = GY - 260
if _sk < 0:
_sk = 0
ui_2d_rect(0, _sk, SW, GY - _sk, C_SKY2, 1, 0, 0)
# 远山(视差:每拍左移 1px,循环)
# distant hills (parallax: 1 px per tick, wrapping)
off = TICK % (SW + 160)
x = 0 - off
while x < SW + 160:
ui_2d_circle(x + 60, GY - 30, 70, C_HILL, 1, 0)
ui_2d_circle(x + 150, GY - 10, 46, C_HILL, 1, 0)
x = x + 180
off2 = (TICK * 2) % (SW + 200)
x = 0 - off2
while x < SW + 200:
ui_2d_circle(x + 90, GY + 10, 54, C_HILL2, 1, 0)
x = x + 220
# 云
# clouds
off3 = (TICK // 2) % (SW + 120)
ui_2d_circle(SW - off3 + 40, 90, 16, 0xFF35506E, 1, 0)
ui_2d_circle(SW - off3 + 66, 96, 12, 0xFF35506E, 1, 0)
ui_2d_circle(SW - ((off3 * 3) % (SW + 90)) + 10, 150, 11, 0xFF35506E, 1, 0)
# 地面
# ground
ui_2d_rect(0, GY, SW, SH - GY, C_GROUND, 1, 0, 0)
ui_2d_rect(0, GY, SW, 5, C_DIRT, 1, 0, 0)
i = 0
while i < 9:
gx = (i * 70 - (TICK * 2) % (SW + 70) + SW + 70) % (SW + 70) - 35
# 视差取模让草叶可以落在 [-35, SW+35);小画布上会越界,越界的这帧直接不画。
# Parallax modulo puts a tuft anywhere in [-35, SW+35); skip the off-canvas ones.
if gx >= 0 and gx + 3 <= SW:
ui_2d_rect(gx, GY - 4, 3, 8, 0xFF2E4F2C, 1, 0, 1)
i = i + 1
# 平台(沙袋)
# platforms (sandbags)
ui_2d_rect(PLX1, PLY1, PLW1, 14, C_SAND, 1, 0, 4)
ui_2d_rect(PLX1, PLY1, PLW1, 4, 0xFF8C6A34, 1, 0, 3)
ui_2d_rect(PLX2, PLY2, PLW2, 14, C_SAND, 1, 0, 4)
ui_2d_rect(PLX2, PLY2, PLW2, 4, 0xFF8C6A34, 1, 0, 3)
# 敌兵
# soldiers
i = 0
while i < MAXEN:
if EN[i * 5 + 2] == 1:
cn_draw_enemy(i)
i = i + 1
# 子弹
# bullets
i = 0
while i < MAXPB:
o = i * 4
if PB[o + 2] == 1:
ui_2d_circle(PB[o] // 10, PB[o + 1] // 10, 3, C_PB, 1, 0)
i = i + 1
i = 0
while i < MAXEB:
o = i * 4
if EB[o + 2] == 1:
ui_2d_circle(EB[o] // 10, EB[o + 1] // 10, 3, C_EB, 1, 0)
i = i + 1
cn_draw_player()
# HUD:分数 / 命
# HUD: score / lives
ui_2d_text(14, 24, "分数" if LANG == 0 else "SCORE", C_DIM, 13, 0)
cn_draw_num(64, 20, SCORE, C_GOLD, 20)
ui_2d_text(14, 48, "击杀" if LANG == 0 else "KILLS", C_DIM, 12, 0)
cn_draw_num(64, 46, KILLS, C_TEXT, 15)
i = 0
while i < LIVES:
ui_2d_circle(SW - 24 - i * 22, 34, 7, C_HELMET, 1, 0)
i = i + 1
# 底部按钮
# bottom buttons
onl = 0
if MV < 0:
onl = 1
onr = 0
if MV > 0:
onr = 1
cn_draw_button(BLX, BTY, BTW, BTH, "<", onl)
cn_draw_button(BRX, BTY, BTW, BTH, ">", onr)
cn_draw_button(BJX, BTY, BTW, BTH, "JUMP", 0)
cn_draw_button(BFX, BTY, BTW, BTH, "FIRE", FIRE_HELD)
if OVER == 1:
ui_2d_rect(0, 0, SW, SH, C_OVER, 1, 0, 0)
ui_2d_text(SW // 2, SH // 2 - 40, "游戏结束" if LANG == 0 else "GAME OVER", C_ENEMY, 30, 1)
ui_2d_text(SW // 2, SH // 2 + 4, "得分" if LANG == 0 else "SCORE", C_DIM, 15, 1)
cn_draw_num(SW // 2 - 30, SH // 2 + 26, SCORE, C_GOLD, 24)
ui_2d_text(SW // 2, SH // 2 + 70, "点屏幕 / 回车重开" if LANG == 0 else "Tap / Enter to restart", C_TEXT, 14, 1)
ui_2d_present()
# ── 主循环 ────────────────────────────────────────────────────────────
# ── Main loop ─────────────────────────────────────────────────────────
TID = ui_timer_set(TICK_MS, 1)
ACC = 0
LASTMS = ui_time_get_ms()
while ui_win_is_closed() == 0:
# 触摸是连续量:轮询当前状态,丢掉积压的事件
# touch is continuous: poll the current state and drop the backlog
ui_msg_drop(1)
T = ui_msg_fetch_wait(0)
if T == 10:
break
# ── 键盘 ──
# ── keyboard ──
if T == 1:
K = ui_msg_get_a()
if K == 27:
break
if K == 13 and OVER == 1:
OVER = 0
LIVES = 3
SCORE = 0
KILLS = 0
TICK = 0
PX = 60
PY10 = (GY - PBH) * 10
VY10 = 0
ONG = 1
INV = 0
i = 0
while i < 48:
PB[i] = 0
EB[i] = 0
i = i + 1
i = 0
while i < 30:
EN[i] = 0
i = i + 1
# ── 触摸轮询 → 按钮 ──
# ── touch polling -> buttons ──
TDOWN = 0
TX = 0
TY = 0
if ui_touch_fetch(0) == 1:
TX = ui_touch_get_x()
TY = ui_touch_get_y()
if ui_touch_is_down() == 1:
TDOWN = 1
MV = 0
FIRE_HELD = 0
if TDOWN == 1:
if cn_rect_hit(TX, TY, BLX, BTY, BTW, BTH) == 1:
MV = -1
if cn_rect_hit(TX, TY, BRX, BTY, BTW, BTH) == 1:
MV = 1
if cn_rect_hit(TX, TY, BFX, BTY, BTW, BTH) == 1:
FIRE_HELD = 1
if cn_rect_hit(TX, TY, BJX, BTY, BTW, BTH) == 1:
if DOWN0 == 0:
JMP = 1
if OVER == 1 and DOWN0 == 0:
OVER = 0
LIVES = 3
SCORE = 0
KILLS = 0
TICK = 0
PX = 60
PY10 = (GY - PBH) * 10
VY10 = 0
ONG = 1
INV = 0
i = 0
while i < 48:
PB[i] = 0
EB[i] = 0
i = i + 1
i = 0
while i < 30:
EN[i] = 0
i = i + 1
# 键盘兜底(按住语义)
# keyboard fallback (held semantics)
if ui_key_is_down(37) == 1:
MV = -1
if ui_key_is_down(39) == 1:
MV = 1
if ui_key_is_down(32) == 1 or ui_key_is_down(90) == 1:
FIRE_HELD = 1
if ui_key_is_down(38) == 1 or ui_key_is_down(87) == 1:
if KEYUP0 == 0:
JMP = 1
KEYUP0 = 1
else:
KEYUP0 = 0
if OVER == 1 and DOWN0 == 0 and TDOWN == 1:
pass
if TDOWN == 1:
DOWN0 = 1
else:
DOWN0 = 0
# ── 墙钟补步 ──
# ── wall-clock stepping ──
NOW = ui_time_get_ms()
ACC = ACC + (NOW - LASTMS)
LASTMS = NOW
if ACC > TICK_MS * 8:
ACC = 0
while ACC >= TICK_MS:
ACC = ACC - TICK_MS
TICK = TICK + 1
FRAMES = FRAMES + 1
# 演示脚本:固定敌兵 + 脚本化射击 / 跳跃 / 中弹
# demo script: fixed soldier + scripted fire / jump / hit
if DEMO == 1:
if TICK == 2:
EN[0] = (PX + 140) * 10
EN[1] = (GY - EBH) * 10
EN[2] = 1
EN[3] = 9999
EN[4] = 1
if TICK >= 2 and TICK < 30:
FIRE_HELD = 1
if TICK == 34:
print_str("ct: hit=")
println_int(1 - EN[2])
print_str("ct: score=")
println_int(SCORE)
if TICK == 40:
JMP = 1
DEMO_Y0 = PY10
if TICK == 56:
if PY10 < DEMO_Y0:
println_str("ct: jump=1")
else:
println_str("ct: jump=0")
if TICK == 60:
EB[0] = PX * 10
EB[1] = (PY10 // 10 + 12) * 10
EB[2] = 1
EB[3] = 0
INV = 0
if TICK == 78:
println_str("ct: hurt=1")
print_str("ct: lives=")
println_int(LIVES)
if TICK >= 90:
DEMO_DONE = 1
break
# ── 玩家水平移动 ──
# ── player horizontal movement ──
if MV < 0:
PX = PX - 3
FACE = -1
if MV > 0:
PX = PX + 3
FACE = 1
if PX < 16:
PX = 16
if PX > SW - 16:
PX = SW - 16
# 走下平台 = 开始下落
# walking off a platform starts a fall
if ONG == 1 and cn_supported(PX, PY10 // 10 + PBH) == 0:
ONG = 0
# ── 跳跃 / 重力 ──
# ── jump / gravity ──
if JMP == 1 and ONG == 1:
VY10 = -78
ONG = 0
ui_audio_beep(700, 40)
JMP = 0
PREVF = PY10 // 10 + PBH
VY10 = VY10 + 2
PY10 = PY10 + VY10
FOOT = PY10 // 10 + PBH
if VY10 >= 0:
if FOOT >= GY:
PY10 = (GY - PBH) * 10
VY10 = 0
ONG = 1
else:
TOP = cn_land_top(PX, PREVF, FOOT)
if TOP >= 0:
PY10 = (TOP - PBH) * 10
VY10 = 0
ONG = 1
# ── 射击 ──
# ── shooting ──
if FCOOL > 0:
FCOOL = FCOOL - 1
if FIRE_HELD == 1 and FCOOL == 0 and OVER == 0:
i = 0
while i < MAXPB:
o = i * 4
if PB[o + 2] == 0:
PB[o] = (PX + 14) * 10
PB[o + 1] = (PY10 // 10 + 22) * 10
PB[o + 2] = 1
FCOOL = 9
ui_audio_beep(880, 18)
i = MAXPB
i = i + 1
# ── 玩家子弹 ──
# ── player bullets ──
i = 0
while i < MAXPB:
o = i * 4
if PB[o + 2] == 1:
PB[o] = PB[o] + 90
if PB[o] > (SW + 30) * 10:
PB[o + 2] = 0
else:
j = 0
while j < MAXEN:
oj = j * 5
if EN[oj + 2] == 1:
b1 = [PB[o] // 10 - 4, PB[o + 1] // 10 - 4, PB[o] // 10 + 4, PB[o + 1] // 10 + 4]
b2 = cn_enemy_box(j)
if cn_boxes_overlap(b1, b2) == 1:
EN[oj + 2] = 0
SCORE = SCORE + 100
KILLS = KILLS + 1
PB[o + 2] = 0
ui_audio_beep(1046, 40)
j = MAXEN
j = j + 1
i = i + 1
# ── 敌兵 ──
# ── soldiers ──
i = 0
while i < MAXEN:
o = i * 5
if EN[o + 2] == 1:
EN[o] = EN[o] - 12
if EN[o + 3] > 0:
EN[o + 3] = EN[o + 3] - 1
if EN[o + 3] == 0:
EX = EN[o] // 10
if EX < PX + 250 and EX > PX + 24:
j = 0
while j < MAXEB:
oj = j * 4
if EB[oj + 2] == 0:
EB[oj] = EN[o] - 100
EB[oj + 1] = EN[o + 1] + 140
EB[oj + 2] = 1
j = MAXEB
j = j + 1
ui_audio_beep(320, 30)
EN[o + 3] = 70 + ui_math_rand(60)
if EN[o] < -300:
EN[o + 2] = 0
if OVER == 0 and INV == 0:
b1 = cn_player_box()
b2 = cn_enemy_box(i)
if cn_boxes_overlap(b1, b2) == 1:
LIVES = LIVES - 1
INV = 90
ui_audio_beep(180, 120)
if LIVES <= 0:
OVER = 1
i = i + 1
# ── 敌兵子弹 ──
# ── enemy bullets ──
i = 0
while i < MAXEB:
o = i * 4
if EB[o + 2] == 1:
EB[o] = EB[o] - 60
if EB[o] < -300:
EB[o + 2] = 0
else:
if OVER == 0 and INV == 0:
b1 = [EB[o] // 10 - 4, EB[o + 1] // 10 - 4, EB[o] // 10 + 4, EB[o + 1] // 10 + 4]
b2 = cn_player_box()
if cn_boxes_overlap(b1, b2) == 1:
EB[o + 2] = 0
LIVES = LIVES - 1
INV = 90
ui_audio_beep(180, 120)
if LIVES <= 0:
OVER = 1
i = i + 1
# ── 无敌计时 / 刷敌 ──
# ── invulnerability timer / spawning ──
if INV > 0:
INV = INV - 1
if OVER == 0:
if SPAWN > 0:
SPAWN = SPAWN - 1
else:
SPAWN = 70 + ui_math_rand(40)
i = 0
while i < MAXEN:
o = i * 5
if EN[o + 2] == 0:
EN[o] = (SW + 30) * 10
EN[o + 1] = (GY - EBH) * 10
EN[o + 2] = 1
EN[o + 3] = 80 + ui_math_rand(60)
EN[o + 4] = 1
i = MAXEN
i = i + 1
if DEMO == 1 and DEMO_DONE == 1:
break
cn_draw()
if DEMO == 1 and DEMO_DONE == 1:
break
if DEMO == 1 and FRAMES >= 120:
break
ui_timer_kill(TID)
ui_scr_set_keep_on(0)
ui_win_close()
if DEMO == 1:
print_str("ct: frames=")
println_int(FRAMES)
println_str("ct: done")