继2d版我的世界之后,3d我的世界隆重推出!
中间写的是“沙漠河懒”
有10种方块可供建筑
草方块不会处理,6个面都是绿的。
区块可自定义大小
需要安装以下模块:
我经过无数实验,发现只有python3.10才能全部兼容这些模块。
不要急,代码在后面(谁偷代码我举报谁,这可是我0.00000000001亿年(1个星期)的成果)
w前进
s后退
a左走
d右走
空格跳跃
1 2 3 4 5 6 7 8 9 0 选方块
上下键飞行
左键破坏
右键放置
#导入模块
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="1.png"):
super().__init__(
model="sphere",
texture=texture,
scale=150,
double_sided=True
)
#地球类
class earth(Entity):
def __init__(self,texture="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 == "n":
quit()
#触发
#窗口类
app = Ursina()
window.fullscreen = True
window.color = color.black
window.fps_counter.enabled = True
window.exit_button.visible = False
earth = earth()
player = FirstPersonController()
#飞行地毯
pl = Entity(
model="plane",
color=color.lime,
texture="white_cube",
scale=(0,0,0),
texture_scale=(0,0),
parent=scene,
collider="box"
)
#创建地形
import random as rd
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")
log_texture=load_texture("texture/log.jpg")
select_texture=grass_texture
class Hand(Entity):
def __init__(self):
super().__init__(
parent=camera.ui,
model="cube",
scale=(0.2,0.3),
color=color.green,
rotation=Vec3(150,-10,0),
position=Vec2(0.4,-0.4)
)
def active(self):
self.position=Vec2(0.1,-0.5)
self.rotation=Vec3(90,-10,0)
def passive(self):
self.rotation=Vec3(150,-10,0)
self.position=Vec2(0.4,-0.4)
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
if held_keys["-"]: select_texture=log_texture
if held_keys["left mouse"] or held_keys["right mouse"]:
hand.active()
else:
hand.passive()
class Tree:
def __init__(self,pos=False,_height=False):
global trees
global height
if pos==False:
pos=rd.randint(-15,15),rd.randint(-15,15)
for i,y in enumerate(range(height+3,height+1+_height+2)):
n=_height-2-i
for x in range(-n,n):
for z in range(-n,n):
Block(position=(pos[0]+x,y,pos[1]+z),texture=leaf_texture)
for i in range(_height):
y=height+i
Block(position=(pos[0],y,pos[1]),texture=log_texture)
height=3
for y in range(0,height):
for z in range(-15,16):
for x in range(-15,16):
print(f"Position:({x},{y},{z})")
if y==height-1:
texture=grass_texture
else:
texture=bedrock_texture
Block(position=(x,y,z),texture=texture)
Tree(_height=5)
player=FirstPersonController()
sky=sky()
hand=Hand()
#显示窗口
app.run()
呃……别看代码简短,他可是完整的(所需图片文件在我主页,免费滴~~~)