伟大的c库raylib的python绑定
raylib-p的Python项目详细描述
raylib py
伟大的c库raylib的python绑定。
开始
先决条件
raylib py在其源代码中使用类型annotations,因此需要支持它的python版本。
有些python版本可能没有enum和/或typings模块作为标准库的一部分,这是必需的。这些是由pip自动安装的。
安装
安装raylib py的最简单方法是使用pip install命令:
根据您安装的系统和python版本,命令可能是:
pip install raylib-py
或
python -m pip install raylip-py
或者(安装了多个版本的python3.7启动程序)
py-3.x-32 -m pip install raylib-py
Note that the minimum Python version tested is 3.4. Please, let me know if you're able to run it in Python33.
raylib py附带用于windows、mac和linux的32位二进制文件,但您不必使用这些文件。如果您有一个自定义的raylibdll,dylib或so二进制文件,请确保设置一个指示其所在目录的路径:
importos# set the path before raylib is imported.os.environ[RAYLIB_LIB_PATH]="path/to/the/binary"importraylibpy# let the fun begin.
您可以将"__file__"
设置为RAYLIB_LIB_PATH
值,并且raylib py将在包目录中搜索二进制文件:
# bynary file is wherever the package is located.os.environ[RAYLIB_LIB_PATH]="__file__"
"__main__"
也可以设置为在启动脚本所在的项目目录中查找二进制文件:
# binary file is in the same dir as this py file.os.environ[RAYLIB_LIB_PATH]="__main__"# ...if__name__=="__main__":# run the game# ...
Make sure the bin file name for the respective platform is
libraylib_shared.dll
,libraylib.2.0.0.dylib
orlibraylib.so.2.0.0
.
测试
raylib py没有测试代码,但是可以运行examples directory中的示例。
raylibvsraylib py
下面是raylib和raylib py之间的用法差异。请注意,尽管这些差异是为了使raylib py尽可能成为pythonic,所以更改可能会在不通知的情况下发生。
恒定值
所有c#define
都被转换成python的“常量”。枚举已转换为
Pythonenums。
结构
一般来说,所有结构都继承自ctypes.Structure
类。目前,施工人员
(矢量除外)需要精确的参数类型,因此无法传递int
s
其中需要float
s(尽管参数可以省略)。
所有结构都实现了__str__()
,因此它们有一个非常基本的文本表示:
# Define the camera to look into our 3d world>>>camera=Camera()>>>camera.position=Vector3(5.,4.,5.)>>>camera.target=Vector3(0.,2.,0.)>>>camera.up=Vector3(0.,1.,0.)>>>camera.fovy=45.0>>>camera.type=CAMERA_PERSPECTIVE>>>camera"(CAMERA3D: position: (5.0, 4.0, 5.0), target: (0.0, 2.0, 0.0), up: (0.0, 1.0, 0.0), fovy: 45.0°, type: PERSPECTIVE)"
不过,并非所有信息都公开。例如,网格对象只公开 顶点和三角形计数属性。
向量
Vector2、Vector3和Vector4支持基本的算术运算:上瘾、减法, 乘法(包括标量乘法)、除法和模。增强的 也支持赋值;右侧操作数可以是相同的任何序列 组件数量:
vec_a=Vector3(3.,5.,7.)vec_b=Vector3(4.,2.,0.)vec_a*vec_b# outputs (12.0, 10.0, 0.0)vec_a+(8,100,-1)# outputs (11.0, 105.0, 6.0)vec_a%=2# augmented assignment (modulo)vec_a# outputs (1.0, 1.0, 0.0)
向量还支持glsl向量滑动。此外,x
、y
、z
和w
坐标映射到
标准化颜色值(r
、g
、b
和a
;仅用于Vector3
和Vector4
)和
纹理坐标(u
和v
):
# Reading (__getattr__)vec3=Vector3(123.0,467.0,789.0)vec2=vec3.uv# x and y respectively as u and vvec3=vec3.bgr# x, y and z respectively as r, g and b ( rgb is not available in Vector 2)vec4=vec2.rrrg# for attribute reading, is ok to repeat components# Writing (__setattr__)vec3=Vector3(123.0,467.0,789.0)vec4.yxwz=10,0,-1,vec3.z# sequences of ints and/or floats are accepted as valuevec2.vu=vec3.xy# x and y respectively as u and vvec3.bgr=12,vec4.x# x, y and z respectively as r, g and b ( rgb is not available in Vector 2)# the following raises an exception:vec3.rrr=vec4.yxw# for attribute writing, is _not_ ok to repeat componentsvec2.br=vec4.uv# r, g and b is not available in Vector2vec4.brxy=(0.,0.,vec2.x,vec3.z)# can't mix component name groups (rgba, xywz and uv)
构造函数和swizzled属性现在接受任意数字组合, 向量和序列,只要保留参数总数:
# all these results in the same Vector4a=Vector4(3,4,5,6)b=Vector4(a.xy,5,6)c=Vector4(b.x,b.yz,6)d=Vector4(3,c.y,c.zw)e=Vector4(d.xy,(5,6))f=Vector4(e.xyz,6)g=Vector4(3,f.yzw)h=Vector4(g)
设置属性也有效:
a=Vector4(Vector2(10,0),100,20)b=Vector4.zero()b.rgba=0.0,vec4.rg,1.0a.xyzw=(10,b.uv),1.0
这是通过删除以前的一个特性而变得可用的,该特性允许非常基本的 挥舞的模拟。一个更类似于glsl向量的特征在 顶级python容器模拟魔术函数:
vec=Vector4(0.,1.,2.,3.)# __len__()print(len(vec))# outputs 4# __iter__()forcompinvec:print(comp)# iterates on Vector4 components# __getitem__()x=vec[0]# key as inty=vec['y']# key as strzw=vec[2:]# key as slice; returns a List[float]# __setitem__()vec[0]=10vec['y']=20# vec[2:] = zw # <--- not supported; will raise TypeError
附加(功能)绘图函数:draw_texture_npatch
由raylib py安装的自定义dll包括一个尚未正式的绘图函数和
相应的NPatchInfo
辅助结构:
# draws an 3-patch (vertical, or horizontal) or 9-patch textured that stretches and# shrinks nicely.# Seq means any sequence typedefdraw_texture_npatch(texture:Texture2D,npatch_info:NPatchInfo,dest_rec:Union[Rectangle,Seq],origin:Union[Vector2,Seq],rotation:float,tint:Union[Color,Seq])->None:
目前(在raylibv2.0.0之后),只有x86自定义dll包含此函数
而且,要启用它,必须设置一个特定的os.environ
键:
# set this before importing raylibpy (the value does not matter as long is a str type)os.environ['ENABLE_V2_0_0_FEATURE_DRAWTEXTURENPATCH']='1'
从源代码构建raylib
raylibwiki页面包含通知如何在Windows、Mac、Linux和其他平台上构建它。
贡献
如果您在使用raylib py时发现任何奇怪或意外的行为,请告诉我。如果要与库相关的request features或report bugs(与此绑定相反),请参阅author's project repo。
作者
另请参阅参与此项目的contributors列表。
许可证
raylib py(和raylib)是在未经修改的zlib/libpng许可证下获得许可的,该许可证是OSI认证的、类似于BSD的许可证,允许与封闭源代码软件进行静态链接。