Python有一个不错的3D引擎——Ursina
Ursina官网http://www.ursinaengine.orghttp://www.ursinaengine.org
前言
打开cmd,控制台输入pip install ursina以安装ursina
1.1编写第一个程序
首先导入ursina
from ursina import *
然后创建app
app=Ursina()
运行app
app.run()
最终代码:
-
from ursina import * app=Ursina() app.run()
如果出现了一个灰色的窗口,那么说明运行成功了!
绘制实体长方体
绘制实体需要用到一个函数:Entity()
因为我们要绘制长方体,所以设置参数model="cube"
代码如下:
from ursina import *
app=Ursina()
cube=Entity(model="cube")
app.run()
绘制长方体阴影与设置颜色
这时我们需要用到函数--cube
根据cube数据来调整颜色与其他数据
from ursina import *
app = Ursina()
window.color = color.black
print(len(scene.entities))
base_cube = Entity(model='cube', color=color.yellow, texture='white_cube')
for i in range(8):
print(curve.out_expo(i/8))
Entity(parent=base_cube, model='cube', color=color.orange, scale=math.pow(1.14,i), alpha=.1)
base_cube.combine()
print(len(scene.entities))
combine_parent = Entity()
base_cube.parent = combine_parent
# for i in range(20):
# duplicate(base_cube, position=(random.uniform(-100,100),random.uniform(-100,100),random.uniform(-100,100)))
# combine_parent.combine()
EditorCamera()
app.run()
3d版长方体
from ursina import *
app = Ursina()
# Make a simple game so we have something to test with
from ursina.prefabs.first_person_controller import FirstPersonController
player = FirstPersonController(gravity=0, model='cube', color=color.azure)
camera.z = -10
ground = Entity(model='plane', texture='grass', scale=10)
# Create an Entity for handling pausing an unpausing.
# Make sure to set ignore_paused to True so the pause handler itself can still receive input while the game is paused.
pause_handler = Entity(ignore_paused=True)
pause_text = Text('PAUSED', origin=(0,0), scale=2, enabled=False) # Make a Text saying "PAUSED" just to make it clear when it's paused.
def pause_handler_input(key):
if key == 'escape':
application.paused = not application.paused # Pause/unpause the game.
pause_text.enabled = application.paused # Also toggle "PAUSED" graphic.
pause_handler.input = pause_handler_input # Assign the input function to the pause handler.
app.run()
FPS游戏
首先,导入ursina模块
from ursina import