现在插播一个教程:ursina制作简单mc。其中我们要实现飞行功能。
注: ursina不支持macOS
安装
安装python
首先去往python官网,之后选择Windows installer(64-bit)或Windows installer(32-bit),这要取决于你电脑的位数,是32位的选右面的,64位的选左面的:
下好了之后把点开,把勾全勾上,再点击Install Now就可以了,我已经安装好了,就不演示了。
打开cmd的方法
其实非常的简单,只需要按住Win+R,输入cmd,回车就完成了!
安装指令
参照上面步骤完成后,在cmd里输入pip install ursina
,回车,这样我们的基础工作就完成了。
→不会python的点这里←
贴图
大家可以去网上搜,我来提供天空和地球的图片:
sky.png
earth.png
制作游戏
话不多说,直接放代码:
#导入模块
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
#更新函数
def update():
pl.position = player.world_position
pl.y += held_keys["up arrow"] * time.dt * 20
pl.y -= held_keys["down arrow"] * time.dt * 20
player.y = pl.y
#天空类
class sky(Entity):
def __init__(self,texture="assets/texture/background/sky.png"):
super().__init__(
model="sphere",
texture=texture,
scale=150,
double_sided=True
)
#地球类
class earth(Entity):
def __init__(self,texture="assets/texture/background/earth.png"):
super().__init__(
model="sphere",
texture=texture,
scale=151,
double_sided=True
)
#方块类
class Voxel(Button):
def __init__(self,model="cube",texture="grass",position=(0,0,0)):
super().__init__(
model=model,
color=color.white,
texture=texture,
position=position,
parent=scene,
origin_y=0.5,
highlight_color=color.lime
)
def input(self,key):
#退出
if key == "escape":
quit()
#触发
if self.hovered:
if key == "left mouse down":
voxel = Voxel(position=self.position + mouse.normal)
if key == "right mouse down":
destroy(self)
#窗口类
app = Ursina()
window.fullscreen = True
window.color = color.black
window.fps_counter.enabled = False
window.exit_button.visible = False
sky = sky()
earth = earth()
player = FirstPersonController()
#飞行地毯
pl = Entity(
model="plane",
color=color.lime,
texture="white_cube",
scale=(2,0,2),
texture_scale=(2,2),
parent=scene,
collider="box"
)
#创建地形
for x in range(30):
for z in range(30):
voxel = Voxel(position=(x,0,z))
#显示窗口
app.run()