Walimaker — 面向教学的 Python 游戏引擎
Walimaker 基于 Pygame 与 Pymunk,用简洁直观的 API 让学生快速做出可玩的 2D 游戏,同时掌握编程核心概念。它提供中文报错提示、笛卡尔坐标系、内置物理与相机,并支持一键打包成网页游戏。
目录
快速开始
pip install Walimaker
from Walimaker import *
setup(800, 600) # 建窗口、相机、精灵组、物理空间
title("我的第一个游戏")
hero = Character("hero.png", size=(32, 32)) # 传 size 才有刚体
set_gravity(0, -1200) # 默认零重力;y 轴向上,负值向下
def game(): # 每帧调用一次
if key_pressed(K_UP):
hero.forward(5)
if key_pressed(K_SPACE):
hero.say("跳跃!")
hero.play_anim()
run(game) # 桌面和网页通用;旧写法 while True + update() 也可用
要点:size 决定是否创建刚体——只给图片不会受重力、也不会有 velocity;坐标系原点在窗口中心,y 轴向上。
打包为网页
把 while True 改成 run(game)(必须在函数里写逻辑,且不要自己调 update()),然后:
walimaker build game.py --web
产物在 dist/web/(静态站点)和 dist/game-web.zip(可直接传到 itch.io / GitHub Pages / Netlify)。本地预览要用 HTTP 服务,不要直接双击 index.html:
cd dist/web && python -m http.server 8000
首次打包按需安装 pygbag(需要网络);也可提前 pip install "walimaker[web]"。想先等用户点击再解锁音频,加 --ume-block 1。
网页版限制:
| 项目 | 说明 |
|---|---|
| 主循环 | 必须 run(game),不能有 while True |
| 物理 | pygbag 提供 pymunk 6.4.0(桌面端最低也支持,全部测试通过) |
| 音频 | 打包时自动把 wav/mp3 转成 ogg,只改构建副本,源文件不动 |
| 中文字体 | 浏览器没有系统字体,需显式 TextBox(25, font="字体.ttf") |
| 标准库 | 不能用 threading / multiprocessing / subprocess / tkinter / input();os.chdir() 会被拒绝 |
| 文件写入 | 浏览器虚拟文件系统里写入不会保存到用户电脑 |
核心概念
坐标系:原点在窗口中心,x 向右、y 向上;角度 0° 指向右侧、逆时针为正。pygame2Cartesian() / Cartesian2pygame() 做窗口与世界坐标互转。
对象继承链(对齐 Unity):
Object 坐标 / 旋转 / 缩放 / 生命周期 / 组件槽位
└─ GameObject 行为与交互:运动、方向、音效、对话、动画
├─ Character DYNAMIC 刚体
├─ Sensor KINEMATIC + sensor(穿透触发器)
├─ Wall STATIC
└─ Mouse 跟随鼠标
组件直接挂在对象上:hero.sprite / hero.body / hero.tiledmap_bodies。旧名 NewGameObject 仍是 GameObject 的别名。
写坐标的三条路径(都在 Object 上):
| 写法 | 语义 | 用途 |
|---|---|---|
obj.pos = (x, y) / goto() |
瞬移,并清零速度 | 摆放、复活、传送 |
obj.shift_by((dx, dy)) |
增量位移,不动速度 | 按键移动、滑行 |
obj.sync_pos(p) / sync_rot(r) |
只改显示缓存,不写刚体 | 框架内部每帧同步 |
组件缺失时不会抛难懂的 AttributeError,而是给一次中英提示并返回中性值(visible=False、尺寸 0、颜色 None),每个对象只提示一次。
API 速查
全部名字随 from Walimaker import * 提供;带 world= 的参数可省略(省略即默认世界 global_var)。
窗口与主循环
| 名字 | 说明 |
|---|---|
setup(width, height, world=None) |
初始化世界:窗口、相机、精灵组、物理空间 |
update(world=None) |
推进一帧(物理 → 逻辑 → 渲染) |
run(loop, world=None) |
推荐主循环:每帧执行 loop() 后自动 update();网页版自动让出事件循环 |
done() / title(text) / save_screen(path) |
保持窗口 / 设标题 / 下一帧保存截图 |
bgpic(img_or_tiled) |
铺背景(可以是路径、Surface 或 TiledMap) |
对象
构造:Character("a.png", size=(32,32)) 单图;传图片列表 = 逐帧动画;传 {"idle":[...],"walk":[...]} = 状态动画。
常用属性:
| 属性 | 说明 |
|---|---|
x / y / pos / dir / rot / angle |
坐标、朝向向量、旋转角、朝向角(只读) |
velocity / angular_velocity / mass / elasticity / friction |
物理属性(无刚体时无效) |
color / red / green / blue / alpha / visible |
颜色与可见性(show() / hide()) |
width / height / layer |
贴图尺寸、渲染层级(move_to_front() / move_to_back()) |
frame / state / dt |
动画帧、状态、帧间隔;状态下一帧生效 |
scl |
缩放向量((1,1) 起步),支持 scl += 1(x/y 同加)与 scl += (dx,dy) |
常用方法:
| 方法 | 说明 |
|---|---|
forward(d) / backward(d) / left(a) / right(a) |
沿朝向移动 / 转向(受 speed() 限速) |
goto(x,y) / slide_to(pos) / face_to(x,y) |
瞬移 / 匀速滑动 / 朝向坐标 |
scale(sx, sy) / body_scale(sx, sy) / flipx(b) / flipy(b) |
缩放显示 / 缩放刚体 / 翻转 |
apply_force(x,y) |
施加冲量(框架沿用"力"的叫法) |
collide(target) / separate(target) / distance(target) |
碰撞 / 刚分开那一帧 / 距离 |
say(text) / play_snd(path) / set_volume(v) |
对话框 / 音效 / 音量 |
play_anim() / stop_anim() / set_dt(state,dt) / set_next_state(a,b) |
动画控制 |
set_start_func(state,fn) / set_end_func(state,fn) |
状态回调 |
get_mouse_clicked() / get_mouse_just_clicked() / get_mouse_upon() |
鼠标交互 |
kill() |
销毁对象及刚体、对话框 |
物理
set_gravity(0, -1200) # 默认零重力
debug() # 画刚体形状
Body(body_type, shape, size, sensor):"STATIC"/"KINEMATIC"/"DYNAMIC";"CIRCLE"(size=半径) / "BOX"(size=(w,h)) / "POLY"(size=顶点列表)。world.SPACE 是底层 pymunk.Space。
connect(a, b) 刚性杆、Spring(a, b, rest_length=..., stiffness=..., damping=...) 弹簧;参数是游戏对象或 Body,可改 spring.rest_length。
高速物体穿墙时,框架按"最快物体"自动切分物理子步:global_var.MAX_STEP_DISTANCE = 8.0(每子步最大位移,0 关闭)、MAX_SUBSTEPS = 16(上限)。
相机
cam = global_var.CAMERA,每个世界一个:
cam.follow(hero) # 跟随
cam.deadzone = (40, 30) # 死区:主体在 80x60 内移动时相机不动
cam.zoom_to(2.0, speed=1.5) # 平滑缩放到 2 倍
cam.smooth_zoom = True # 平滑缩放(稍慢)
cam.shake(amount=8, duration=0.3) # 受击抖动
cam.scale(s) 直接缩放;cam.surface 是当前画面;cam.pos / offset / size / scl 可读。
文字与输入
hud = TextBox(24) # 屏幕文字
hud.write(f"分数 {score}") # print 是兼容别名
hud.goto(-350, 250)
obj.say("你好") # 跟随对象的对话框
按键常量(K_UP、K_a、K_SPACE…)随 from Walimaker import * 提供;key_pressed(key) 按住(不传参 = 任意键)、key_just_pressed(*keys) 刚按下、key_just_released() 刚松开、key_input() 本帧文本。鼠标:get_mouse_pos()、get_mouse_rel()、get_mouse_clicked(b="all")、get_mouse_just_clicked()、get_mouse_just_released()、set_mouse_visible(b)。对象方法 get_mouse_upon() 判断悬停。
音频与地图
bgmusic("bgm.ogg") # 循环背景音乐;set_volume(0.6)
hero.play_snd("jump.wav")
其他音乐接口:music_load / music_queue / music_play / music_stop / music_pause / music_fadeout(ms) / music_set_pos(s) / music_set_volume(v)。world.audio_available 判断有无声卡。
TiledMap("map.tmx") 解析地图:bgpic(tiled) 铺成背景;tiled.create_objects(name=None) 把对象层变成带碰撞体的游戏对象;tiled.get_objects(name=None) 取解析结果(含 name/type/x/y/points/properties)。
资源与工具
preload({"images":[...], "fonts":[(None,24)], "sounds":[...]}) 批量预加载;ResourceManager(world.RESOURCE_MANAGER)提供 load_image / load_font / load_sound / clear_cache / get_cache_info,图片/字体/音效走 LRU 缓存,碰撞 mask 走弱引用缓存。
场景工具:tracer(b) 每次更新后自动再刷新一次、speed(n) 移动限速、draw_line(p1,p2,color,width)、set_depth(i)。
多世界:每个 World() 有自己的窗口、相机、精灵组和物理空间;所有接口的 world= 省略即用默认世界。setup(800,600,world=w2) 建第二个世界,update(w2) 只推进它。
常见任务
from Walimaker import *
setup(800, 600)
# 背景与角色
bgpic("bg.png")
hero = Character("hero.png", size=(32, 32)); hero.goto(0, -100)
# 重力与地面
set_gravity(0, -1200)
ground = Wall(size=(700, 40)); ground.goto(0, -250)
if hero.collide(ground) and key_pressed(K_SPACE):
hero.velocity = (0, 620)
# 碰撞:collide 持续为真,separate 只在离开那一帧
if hero.separate(ground): print("起跳!")
# HUD 文字、音乐、截图
hud = TextBox(24); hud.write(f"分数 {score}"); hud.goto(-350, 250)
bgmusic("bgm.ogg"); hero.play_snd("jump.wav")
save_screen("shot.png"); update()
# 计时(帧率上限 60)
if global_var.FRAME % 60 == 0: spawn_enemy()
# 约束与相机
connect(anchor, box)
spring = Spring(anchor, ball, rest_length=100); spring.rest_length = 140
cam = global_var.CAMERA; cam.follow(hero); cam.deadzone = (40, 30); cam.shake(8, 0.3)
# 地图
tiled = TiledMap("level.tmx"); bgpic(tiled)
coins = tiled.create_objects(name="coin")
中文字体
包内不带字体文件(原先楷体/宋体合计 28.6 MB 且属微软版权字体,不能随包分发)。TextBox / DialogBox 的字体按以下顺序解析:
- 显式传入:
TextBox(25, font=r"C:\Windows\Fonts\simhei.ttf") - 环境变量
WALIMAKER_FONT(机房统一指定很方便) - 包内
Walimaker/static/下的任意 ttf/ttc/otf(放进去即生效) - 系统自带中文字体的固定路径直检(Windows 微软雅黑/黑体/宋体、macOS 苹方/冬青黑体、Linux Noto/文泉驿)
- 按字体名匹配的系统字体
- pygame 默认字体(不含中文字形,中文会显示成方框)
找不到字体不会报错,只发一次中英文警告。排查:
from Walimaker.fonts import font_info
print(font_info()) # has_cjk=False 时安装系统 CJK 字体或设 WALIMAKER_FONT
中文报错提示
默认开启:英文原始 traceback 完整保留,后面追加中文的「原因 / 解决」。关闭用 WALIMAKER_ERROR_HELP=0 或 set_error_help(False);在 try/except 里可用 explain_exception(e) 主动取解释。
常见问题
角色不掉下来、velocity 没反应:要同时传 size 并 set_gravity(0, -1200)(默认零重力)。
AttributeError: 'NoneType' ...:多半没先调用 setup(宽, 高)。
中文显示方框:见「中文字体」,font_info() 看 has_cjk。
子弹穿墙:调小 global_var.MAX_STEP_DISTANCE 或调大 MAX_SUBSTEPS,或把墙做厚。
找不到图片/音乐:相对路径相对于运行脚本时的工作目录,区分大小写;建议用 os.path.join(os.path.dirname(__file__), ...)。
key_pressed("w") 报错:要传 pygame 常量 K_w,常量随 from Walimaker import * 提供;不传参 = 任意键按下。
无显示器/CI 上跑:SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy python your_game.py(本仓库测试就是这么跑的)。
开发与测试
uv sync # 创建 .venv 并安装依赖
uv run python scripts/check.py # black + flake8 + mypy + 全部测试
uv run python scripts/check.py --matrix # 额外跑 pymunk 6.4.0 / 6.11.1 / 最新版矩阵
uv run python tests/smoke_test.py # 单跑一个测试
不用 uv 时 pip install -e ".[dev]" 后直接 python scripts/check.py。支持 Python 3.8.1+,推荐 3.9–3.13;3.14+ 需改用 pygame-ce。
示例与许可证
examples/ 下有可直接运行的物理、约束、TMX 地图示例,支持 WALIMAKER_EXAMPLE_FRAMES=N 跑 N 帧后退出:
uv run python examples/physics_basics.py
uv run python examples/physics_constraints.py
uv run python examples/tiledmap_basics.py
另见 Walimaker/test.py(walimaker-demo 入口)的完整平台跳跃 demo。
本项目采用 MIT 许可证,完整文本见 LICENSE。问题与建议可附重现步骤、错误日志和运行环境反馈;版本记录见 PyPI 项目页 与 CHANGELOG.md。
Metadata
Release files for walimaker 4.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| walimaker-4.4.1.tar.gz | 327.9 kB | With provenance | Details for walimaker-4.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| walimaker-4.4.1-py3-none-any.whl | Python 3 | none | any | Details for walimaker-4.4.1-py3-none-any.whl |
Total release size: 630.2 kB
Release files / walimaker-4.4.1.tar.gz
| Download URL | walimaker-4.4.1.tar.gz |
|---|---|
| Size | 327.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
28a21d460d7287adc20c2dc3d9a5441e7b45f0bf08fe9045bd9d08c318b3364f
|
|
BLAKE2b-256 checksum How to use checksums |
d92ecf4e8fa248cb025a1c37b707f650562a26c8d00d6fd5f01fde17908299bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logRelease files / walimaker-4.4.1-py3-none-any.whl
| Download URL | walimaker-4.4.1-py3-none-any.whl |
|---|---|
| Size | 302.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a9c7ea0be01cd0d02fe3f7be69bc2b227dc1848e3781e11c389f798db911dc6a
|
|
BLAKE2b-256 checksum How to use checksums |
be82a4d890fe767963c5f62f253289465195e66e0428913a0032c032d70f46e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logSecurity for walimaker 4.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for walimaker-4.4.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.