本次,我們將實(shí)現(xiàn)這樣一個(gè)效果:
首先,導(dǎo)入ursina模塊
from ursina import *
創(chuàng)建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
)
然后,我們需要一個(gè)天空
定義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)
)
因?yàn)槲覀兯械姆綁K包括天空都需要圖片材質(zhì),所以我們?cè)诔绦蜷_頭寫以下代碼:
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")
然后咱們先創(chuàng)建一個(gè)超平坦地形,厚度就只有1層吧,因?yàn)榉綁K多了很容易變卡掉幀
那我們就把底面設(shè)置成基巖吧
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)
這時(shí)候,我們需要一個(gè)第一人稱控制器,在程序前導(dǎo)入
from ursina.prefabs.first_person_controller import FirstPersonController
然后在程序后面寫上
player=FirstPersonController()
順便創(chuàng)建個(gè)天空
sky=Sky()
最后運(yùn)行app
app.run()
我們還要繼續(xù)設(shè)置放置和破壞方塊的功能,在Block中添加兩個(gè)函數(shù),用于設(shè)置選取方塊類型還有放置和破壞方塊的效果
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
在導(dǎo)入材質(zhì)后添加默認(rèn)選?。?/p>
select_texture=grass_texture
最后運(yùn)行效果如下:
最終代碼:
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()
下節(jié)我們將繼續(xù)優(yōu)化游戲,大家可以免費(fèi)訂閱我的專欄哦!
喜歡的話就點(diǎn)贊關(guān)注吧!我們下期再見!
附資源(免費(fèi)下載):文章來源:http://www.zghlxwxcb.cn/news/detail-441970.html
制作簡易版《我的世界》所需要的材質(zhì)圖片-Python文檔類資源-CSDN下載制作簡易版《我的世界》所需要的材質(zhì)圖片更多下載資源、學(xué)習(xí)資料請(qǐng)?jiān)L問CSDN下載頻道.https://download.csdn.net/download/leleprogrammer/85382058文章來源地址http://www.zghlxwxcb.cn/news/detail-441970.html
到了這里,關(guān)于手把手教你用Python編一個(gè)《我的世界》 2.材質(zhì)及第一人稱的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!