定位与优势
open_window
代替 set_mode
)。与 Pygame 对比
特性 | Arcade | Pygame |
---|---|---|
坐标系 | 左下角为 (0,0),y 轴向上 | 左上角为 (0,0),y 轴向下 |
图形渲染 | OpenGL 3+ 加速,抗锯齿支持 | 基于 SDL1(过渡到 SDL2) |
API 一致性 | 统一命名(如 append() 添加精灵) |
方法命名不一致(如 add() ) |
物理引擎 | 内置平台游戏物理引擎 | 需手动实现或第三方库 |
pip install arcade # 基础安装
pip install PyObjC # macOS 额外依赖
import arcade
arcade.open_window(800, 600, "Test")
arcade.run()
窗口与坐标系
class MyGame(arcade.Window):
def __init__(self):
super().__init__(width=800, height=600, title="Game")
arcade.set_background_color(arcade.color.SKY_BLUE)
def on_draw(self):
arcade.start_render() # 必须调用
arcade.draw_circle_filled(400, 300, 50, arcade.color.RED)
(0,0)
在左下角,向右为 x 正方向,向上为 y 正方向。绘图基础
arcade.draw_rectangle_filled(center_x, center_y, width, height, color) # 实心矩形
arcade.draw_triangle_outline(x1, y1, x2, y2, x3, y3, color) # 空心三角形
ShapeElementList
组合多个图形(如用线段拼三角形)。精灵(Sprites)与动画
self.player = arcade.Sprite("player.png", scale=0.5)
self.player.center_x = 400
self.player.center_y = 300
on_update()
中更新位置:self.player.change_x = 5
。SpriteList
)批量管理精灵提升性能。游戏循环与事件处理
class MyGame(arcade.Window):
def on_key_press(self, key, modifiers):
if key == arcade.key.LEFT:
self.player.change_x = -5
def on_key_release(self, key, modifiers):
if key == arcade.key.LEFT:
self.player.change_x = 0
def update(self, delta_time):
self.player.update() # 更新精灵位置
碰撞检测
if arcade.check_for_collision(player, enemy):
player.kill() # 处理碰撞逻辑
check_for_collision_with_list()
检测精灵与列表碰撞)。视口滚动与关卡设计
Viewport
实现镜头跟随玩家:arcade.set_viewport(left, right, bottom, top) # 动态调整视口
.tmx
文件)。物理引擎
physics_engine = arcade.PhysicsEnginePlatformer(
player_sprite, platforms, gravity_constant=0.5
)
音效与 UI
arcade.play_sound(jump_sound)
。arcade.draw_text("Score: 100", 10, 580, arcade.color.WHITE, 20)
。状态管理
game_state = PAUSED
)控制渲染逻辑。打包发布
pip install pyinstaller
pyinstaller --onefile --windowed game.py # 生成独立可执行文件
学习资源
Arcade 凭借其现代 API 设计、强大的图形渲染和简洁的语法,成为 Python 2D 游戏开发的首选框架。尤其适合教育场景和快速原型开发,但对 3D 游戏和树莓派支持有限。建议通过官方示例和平台游戏项目实践深入掌握其工作流。