2.8 KiB
2.8 KiB
Pingpang
乒乓球对战游戏 — Godot 4.6 + C++ GDExtension
项目结构
pingpang/
├── CMakeLists.txt # CMake 构建配置
├── build-pingpang.bat # Windows 一键编译脚本
├── godot-cpp/ # Godot C++ 绑定(git submodule, 4.5 分支)
├── src/ # C++ 源码
│ ├── register_types.cpp/h # GDExtension 入口 + 类注册
│ ├── pingpong_ball.cpp/h # 球(移动、碰撞、得分检测)
│ ├── pingpong_paddle.cpp/h # 球拍(玩家输入 / AI)
│ └── pingpong_game.cpp/h # 游戏管理(计分、状态)
├── main/ # Godot 项目目录
│ ├── project.godot # Godot 项目配置
│ ├── pingpang.gdextension # GDExtension 加载配置
│ ├── scenes/
│ │ └── main.tscn # 主场景
│ ├── bin/ # 编译产物(gitignore)
│ │ └── pingpang.dll
│ └── .godot/ # Godot 编辑器数据(gitignore)
└── bin/ # 历史遗留(即将清理)
开发环境
| 工具 | 版本/路径 |
|---|---|
| Godot 编辑器 | 4.6 (F:/PCL/MyDownload/Godot_v4.6-stable_win64.exe) |
| CMake | VS 2026 自带 |
| Visual Studio | 2026 (D:/Visual Studio/) |
| godot-cpp | 4.5 分支 |
编译 & 运行
编译 C++ 扩展
# Windows: 双击运行
build-pingpang.bat
# 或手动
cmake -S . -B build -G "Visual Studio 18 2026" -A x64
cmake --build build --config Release
编译产物 pingpang.dll 会自动输出到 main/bin/。
打开 Godot 项目
- 启动 Godot 4.6 编辑器
- 导入项目 → 选择
main/目录 - 打开后直接按 F5 运行
核心类说明
PingPongBall (CharacterBody2D)
speed: float— 球速(默认 400)reset()— 重置到屏幕中央,随机方向发射scored(which: int)信号 — 0=左边得分, 1=右边得分
PingPongPaddle (CharacterBody2D)
speed: float— 移动速度(默认 350)is_player: bool— true=玩家控制, false=AIball_path: NodePath— AI 模式下追踪的球节点
PingPongGame (Node2D)
left_score: int/right_score: int— 当前比分max_score: int— 获胜分数(默认 5)game_over(winner: int)信号 — 0=左边赢, 1=右边赢
协作指南
-
添加新的 GDExtension 类:
- 在
src/创建.cpp/.h文件 - 在
src/register_types.cpp中GDREGISTER_CLASS() - 在
CMakeLists.txt的add_library()中添加源文件
- 在
-
场景编辑:在 Godot 编辑器中编辑
main/scenes/main.tscn,所有 C++ 类可在节点列表中直接创建 -
提交代码前:确保
build-pingpang.bat编译通过