本次,我们将实现这样一个效果:
首先,导入ursina模块
from ursina import *
创建app
app=Ursina()
定义Block类,继承自Button
class Block(Button):
def __init__(self,position=(0,0,0),texture=grass_texture):
super().__init__(
parent=scene,
position=position,
model="cube",
highlight_color=color.lime,
color=color.white,
texture=texture,
origin_y=0.5
)
然后,我们需要一个天空
定义Sky类
class Sky(Entity):
def __init__(self):
super().__init__(
parent=scene,
model="sphere",
scale=1500,
texture=sky_texture,
double_sided=True,
position=(0,0,0)
)
因为我们所有的方块包括天空都需要图片材质,所以我们在程序开头写以下代码:
grass_texture=load_texture("texture/grass.jpg")
dirt_texture=load_texture("texture/dirt.jpg")
sky_texture=load_texture("texture/sky.jpg")
cobblestone_texture=load_texture("texture/cobblestone.png")
plank_texture=load_texture("texture/plank.jpg")
stone_texture=load_texture("texture/stone.jpg")
bedrock_texture=load_texture("texture/bedrock.jpg")
brick_texture=load_texture("texture/brick.png")
endstone_texture=load_texture("texture/endstone.jpg")
lapis_texture=load_texture("texture/lapis.jpg")
leaf_texture=load_texture("texture/leaf.jpg")
lucky_block_texture=load_texture("texture/luckyblock.png")
然后咱们先创建一个超平坦地形,厚度就只有1层吧,因为方块多了很容易变卡掉帧
那我们就把底面设置成基岩吧
height=1
for y in range(0,height):
for z in range(-15,16):
for x in range(-15,16):
print(f"Position:({x},{y},{z})")
texture=bedrock_texture
Block(position=(x,y,z),texture=texture)
这时候,我们需要一个第一人称控制器,在程序前导入
from ursina.prefabs.first_person_controller import FirstPersonController
然后在程序后面写上
player=FirstPersonController()
顺便创建个天空
sky=Sky()
最后运行app
app.run()
我们还要继续设置放置和破坏方块的功能,在Block中添加两个函数,用于设置选取方块类型还有放置和破坏方块的效果
def input(self,key):
if self.hovered:
if key=="right mouse down":
Block(position=self.position+mouse.normal,texture=select_texture)
if key=="left mouse down":
if self.texture!=bedrock_texture:
destroy(self)
def update(self):
global select_texture
if held_keys["1"]: select_texture=grass_texture
if held_keys["2"]: select_texture=dirt_texture
if held_keys["3"]: select_texture=cobblestone_texture
if held_keys["4"]: select_texture=plank_texture
if held_keys["5"]: select_texture=stone_texture
if held_keys["6"]: select_texture=brick_texture
if held_keys["7"]: select_texture=endstone_texture
if held_keys["8"]: select_texture=lapis_texture
if held_keys["9"]: select_texture=leaf_texture
if held_keys["0"]: select_texture=lucky_block_texture
在导入材质后添加默认选取:
select_texture=grass_texture
最后运行效果如下:
最终代码:
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app=Ursina()
grass_texture=load_texture("texture/grass.jpg")
dirt_texture=load_texture("texture/dirt.jpg")
sky_texture=load_texture("texture/sky.jpg")
cobblestone_texture=load_texture("texture/cobblestone.png")
plank_texture=load_texture("texture/plank.jpg")
stone_texture=load_texture("texture/stone.jpg")
bedrock_texture=load_texture("texture/bedrock.jpg")
brick_texture=load_texture("texture/brick.png")
endstone_texture=load_texture("texture/endstone.jpg")
lapis_texture=load_texture("texture/lapis.jpg")
leaf_texture=load_texture("texture/leaf.jpg")
lucky_block_texture=load_texture("texture/luckyblock.png")
select_texture=grass_texture
class Sky(Entity):
def __init__(self):
super().__init__(
parent=scene,
model="sphere",
scale=1500,
texture=sky_texture,
double_sided=True,
position=(0,0,0)
)
class Block(Button):
def __init__(self,position=(0,0,0),texture=grass_texture):
super().__init__(
parent=scene,
position=position,
model="cube",
highlight_color=color.lime,
color=color.white,
texture=texture,
origin_y=0.5
)
def input(self,key):
if self.hovered:
if key=="right mouse down":
Block(position=self.position+mouse.normal,texture=select_texture)
if key=="left mouse down":
if self.texture!=bedrock_texture:
destroy(self)
def update(self):
global select_texture
if held_keys["1"]: select_texture=grass_texture
if held_keys["2"]: select_texture=dirt_texture
if held_keys["3"]: select_texture=cobblestone_texture
if held_keys["4"]: select_texture=plank_texture
if held_keys["5"]: select_texture=stone_texture
if held_keys["6"]: select_texture=brick_texture
if held_keys["7"]: select_texture=endstone_texture
if held_keys["8"]: select_texture=lapis_texture
if held_keys["9"]: select_texture=leaf_texture
if held_keys["0"]: select_texture=lucky_block_texture
height=1
for y in range(0,height):
for z in range(-15,16):
for x in range(-15,16):
print(f"Position:({x},{y},{z})")
texture=bedrock_texture
Block(position=(x,y,z),texture=texture)
player=FirstPersonController()
sky=Sky()
app.run()
下节我们将继续优化游戏,大家可以免费订阅我的专栏哦!
喜欢的话就点赞关注吧!我们下期再见!
附资源(免费下载):文章来源:https://www.toymoban.com/news/detail-441970.html
制作简易版《我的世界》所需要的材质图片-Python文档类资源-CSDN下载制作简易版《我的世界》所需要的材质图片更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/leleprogrammer/85382058文章来源地址https://www.toymoban.com/news/detail-441970.html
到了这里,关于手把手教你用Python编一个《我的世界》 2.材质及第一人称的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!