python - ursina和Minecraft

继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()

呃……别看代码简短,他可是完整的(所需图片文件在我主页,免费滴~~~)

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们开始吧!首先,我们需要安装ursina模块,可以使用以下命令: ``` pip install ursina ``` 安装完成后,我们可以创建一个新的Python文件,命名为“shoot_game.py”。 接下来,我们需要导入ursina和其他必要的模块: ```python from ursina import * from ursina.prefabs.first_person_controller import FirstPersonController import random ``` 接下来,我们需要创建一个场景和相机: ```python app = Ursina() window.fps_counter.enabled = False window.exit_button.visible = False camera.orthographic = True camera.fov = 20 camera.position = (0, 20, 0) camera.rotation_x = -90 ground = Entity(model='plane', texture='grass', scale=(30, 1, 30), collider='box') ``` 然后,我们需要创建一个篮筐和一个球: ```python basket = Entity(model='cube', color=color.orange, scale=(2, 0.2, 2), position=(0, 5, -5), collider='box') ball = Entity(model='sphere', color=color.white, scale=0.5, position=(0, 2, 0), collider='sphere') ``` 接下来,我们需要定义投篮功能: ```python def shoot(): ball.position = (0, 2, 0) ball.velocity = (random.uniform(-4, 4), random.uniform(5, 10), random.uniform(-4, 4)) ``` 最后,我们需要在游戏中添加第一人称控制器和按键事件: ```python player = FirstPersonController() app.run() ``` ```python def input(key): if key == 'space': shoot() ``` 现在,我们已经完成了这个游戏!您可以在控制台中运行“python shoot_game.py”命令来运行它。玩家可以通过按下空格键射击篮球。您可以根据需要自定义游戏元素,例如添加分数计数器等。 希望这个例子能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值