fantas V3¶
快速开始¶
欢迎了解 fantas!这是一个基于 pygame-ce [1] 的 2D 图形程序框架。 当你安装好 fantas 后,下一个问题就是怎样让程序运行起来。你可能知道,pygame-ce 并不是一个开箱即用的库,它需要你完全掌控整个主循环,这对于初学者来说可能会有些困难。 幸运的是,fantas 帮你做好了一切。
安装 fantas¶
fantas 可以通过 pip 轻松安装:
pip install fantas # 最简单
pip3 install fantas # 兼容性更好
python -m pip install fantas # 兼容性最好
暂时未发布到 PyPI
目前 fantas 还没有发布到 PyPI 上,你可能需要从 GitHub 仓库下载并自行编译安装。
别担心,fantas 提供了开发用的一键安装脚本 python dev.py install,
希望你不会碰到未知的错误。
小试牛刀¶
来看一个简单的例子吧:
1import fantas
2
3# 创建窗口
4window = fantas.Window(
5 fantas.WindowConfig(
6 title="Hello, Fantas!",
7 window_size=(800, 600),
8 )
9)
10
11# 创建渐变背景
12background = fantas.LinearGradientLabel(
13 rect=fantas.Rect((0, 0), window.size),
14 start_color=fantas.Color('red'),
15 end_color=fantas.Color('blue'),
16 start_pos=(0, 0),
17 end_pos=window.size
18)
19window.append(background)
20
21# 设置默认文本样式
22fantas.DEFAULTTEXTSTYLE.font = fantas.fonts.DEFAULTSYSFONT
23fantas.DEFAULTTEXTSTYLE.size = 90
24# 创建文本
25text = fantas.Text(
26 text="好好学习\n天天向上",
27 rect=fantas.Rect(0, 0, 600, 300),
28 align_mode=fantas.AlignMode.CENTER,
29)
30text.rect.center = background.rect.center # 将文本中心对齐到背景中心
31window.append(text)
32
33# 启动窗口主循环
34window.mainloop()
想要来点动画吗,那就试试这个:
1import fantas
2
3window = fantas.Window(
4 fantas.WindowConfig(
5 title="Animate It!",
6 window_size=(400, 300),
7 )
8)
9
10# 纯色背景
11background = fantas.ColorBackground(fantas.Color(0, 0, 0))
12window.append(background)
13
14# 来个小方块吧
15block = fantas.Label(fantas.Rect(20, 20, 50, 50))
16background.append(block)
17
18# 动画1:让方块动起来
19block_pos_kf = fantas.PointKeyFrame(
20 block.rect,
21 "center",
22 fantas.Vector2(355, 255),
23 fantas.CURVE_SMOOTH,
24)
25block_pos_kf.set_duration_ms(1000)
26block_pos_kf.start()
27
28# 动画2:让背景变色
29background_color_kf = fantas.ColorKeyframe(
30 background,
31 "bgcolor",
32 fantas.Color(255, 0, 0),
33 fantas.CURVE_SMOOTH,
34)
35background_color_kf.set_duration_ms(1000)
36background_color_kf.start()
37
38window.mainloop()
教程¶
基础概念¶
fantas 中有些类是直接复用的 pygame-ce 中的类,如 fantas.Rect 和
fantas.color.Color 等,
关于这些类的详细信息可以在 pygame 的文档中找到,本教程则会介绍一些常用的用法和技巧。
有些类是继承自 pygame-ce 中的类,如 Window 和
Font 等,在保留了原有接口的基础上,添加了一些新的功能和属性,
新增的部分可以在 fantas 的文档中找到。
还有一些类是 fantas 独有的,如 UI 和 Curve
等,这些类决定了 fantas 的核心逻辑和设计理念,有关详细信息也可以在 fantas 的文档中找到。
参考¶
重要
所有模块的接口都是在 fantas 包的顶层导出的,你不需要使用任何子模块的名字来访问它们。
- color
提供颜色相关的增强功能。
- constants:
提供 fantas 库中使用的常量、枚举类型和相关函数。
- curve
提供曲线相关的类和预定义曲线。
- debug
调试模块,可以启动一个独立的调试子进程。
- event_handler
提供事件处理器类。
- fantas_typing
提供 fantas 模块中使用的类型别名,或把某些 pygame 类型重新导出以供 fantas 使用。
- fantas
fantas 顶级包
开发帮助¶
未完成
TODO: 编写开发帮助文档。
其他¶
- GitHub
这是 fantas 的代码仓库,欢迎访问、使用和贡献。
在此也一并提供 pygame-ce-for-fantas 的仓库链接。fantas 使用的是其 fantas 分支编译的版本。
- MIT License
这是 fantas 的开源许可协议,允许你自由使用、修改和分发该软件, 但必须保留原作者的版权声明和许可声明。