目录 :
主要特性
依赖库:
第一个 ursina代码:
创建一个物体:
代码解读:
复原《我的世界》:
总结
- Ursina 是一个用于创建 3D 游戏和应用程序的 Python 游戏引擎。它基于 Panda3D 引擎构建,但提供了更简单易用的接口,使得开发者能够更快速地上手和开发项目。Ursina 特别适合初学者和那些希望在 Python 环境中进行游戏开发的人。
Ursina官网https://www.ursinaengine.org/https://www.ursinaengine.org/https://www.ursinaengine.org/https://www.ursinaengine.org/
Ursina pypi网址https://pypi.org/project/ursina/https://pypi.org/project/ursina/https://pypi.org/project/ursina/https://pypi.org/project/ursina/
GitHub 仓库https://github.com/pokepetter/ursinahttps://github.com/pokepetter/ursinahttps://github.com/pokepetter/ursinahttps://github.com/pokepetter/ursina
ursina站内示例图片:
Ursina是外部库,需要导入:
pip install ursina
主要特性
-
易于使用: Ursina 提供了清晰的 API,使得开发者可以通过简单的 Python 代码来创建复杂的 3D 场景。
-
内置的 3D 引擎: Ursina 使用 Panda3D 作为底层引擎,具备强大的图形处理能力和性能。
-
丰富的组件系统: Ursina 提供了多个基本组件(如实体、音效、相机等),开发者可以轻松组合使用。
-
场景管理: 可以方便地管理场景和加载不同的游戏关卡。
-
支持物理引擎: 引擎内置了物理系统,可以实现碰撞检测和物理模拟。
-
跨平台支持: Ursina 支持在多个平台上运行,包括 Windows、macOS 和 Linux。
-
社区和文档: Ursina 拥有活跃的社区和丰富的文档,提供了各种教程和示例,帮助开发者快速上手。
依赖库:
- python 3.6+ python官网
- panda3d panda3d官网
- pillow,用于纹理操作 pillow官方文档
- psd-tools,用于转换 .psd 文件 psd-tools官方文档
- blender,用于转换 .blend 文件 blender官网
- pyperclip,用来复制/粘贴 pyperclip-Pypi
第一个 ursina代码:
from ursina import *
#从ursina库导入所有模块
app=Ursina()
#加载一个运行窗口
app.run()
#运行
当你看到类似下图灰色窗口时,表示你成功了!
创建一个物体:
from ursina import * app = Ursina() # 创建一个盒子
box = Entity(model='cube', color=color.orange, texture='white_cube') # 设置相机位置
camera.position = (0, 0, -5) # 游戏循环
def update(): box.rotation_y += 1 # 让盒子旋转 app.run()
代码解读:
在这个示例中,我们导入了 Ursina 库,创建了一个简单的 3D 盒子并设置了相机的位置。在 update
函数中,我们让盒子以每帧 1 度的速度旋转。
在Entity函数中,有三个参数,分别为model(模型),color(颜色),texture(贴图),贴图可以是自己的图片,只要把自己的图片放到python文件同目录下,将参数改为图片的名称即可。
复原《我的世界》:
《我的世界》是一款沙盒游戏,由正方形构成一个个方块,所以我们可以用Ursina库复原一个简单的我的世界
from ursina import *
from ursina.input_handler import get_combined_key
from ursina.prefabs.first_person_controller import FirstPersonController
import randomapp = Ursina()# 不同类型的方块纹理
textures = ['grass', 'stone', 'dirt', 'brick','t','zs']selected_voxel_type = 'grass' # 默认选中方块类型
def input(key):global selected_voxel_typeif key == '1':selected_voxel_type = 'grass'elif key == '2':selected_voxel_type = 'stone'elif key == '3':selected_voxel_type = 'dirt'# 定义方块类
class Voxel(Button):def __init__(self, position=(0, 0, 0), type_='grass'):super().__init__(parent=scene,model='cube',texture=textures[textures.index(type_) % len(textures)],color=color.color(1, 0, 1),highlight_color=color.lime,position=position,origin_y=0.5)self.type = type_self.a = 1def input(self, key):global selected_voxel_typeif self.hovered:if key == 'escape':quit()if key == 'right mouse down':voxel_type = selected_voxel_type # 使用全局选定的方块类型voxel = Voxel(position=self.position + mouse.normal, type_=voxel_type)if key == 'left mouse down':if self.type != 'brick':destroy(self)# 生成初始地面
for z in range(7): #可以更改循环次数,创建更大的世界(长)for y in range(20):for x in range(7):#可以更改循环次数,创建更大的世界(宽)if y <= 18:if y < 2 :if y==0:voxel_type = 'grass'else:voxel_type = 'dirt'else:if y >= 3:a=random.randint(1,100-y)b = random.randint(1, 80 - y)if b<2 and y>=13:voxel_type = 'zs'elif a<4:voxel_type = 't'else:voxel_type = 'stone'else:voxel_type = 'stone'else:voxel_type = 'brick'voxel = Voxel(position=(x, -y, z), type_=voxel_type)# 天空盒
sky_texture = load_texture('sky_sunset')
Sky(texture=sky_texture)# 玩家控制器,添加跳跃能力
class MyPlayer(FirstPersonController):def update(self):super().update()if self.jumping and self.y < 2:self.y += time.dt * 10player = MyPlayer()# 日夜循环(简化版)
def update():global sky_texturetime_of_day = (time.time() % 8) / 8 # 一天模拟为8秒if 0 <= time_of_day < 0.25: # 黎明sky_texture = load_texture('sky_dawn')elif 0.25 <= time_of_day < 0.5: # 白天sky_texture = load_texture('sky_day')elif 0.5 <= time_of_day < 0.75: # 黄昏sky_texture = load_texture('sky_sunset')else: # 夜晚sky_texture = load_texture('sky_night')app.run()
效果
代码解读:
此代码创建了一个玩家类,并创建了初始地面,在生成地面时随机生成了铁和钻石,又创建了一个简易的天空。
总结
Ursina 是一个强大的工具,适合于从小型项目到有趣的游戏开发。它简化了游戏开发的复杂性,同时保留了足够的灵活性来满足更多高级需求。对于希望用 Python 开发 3D 游戏的开发者来说,Ursina 是一个值得尝试的引擎。