手把手教你用Python编一个《我的世界》 3.添加手臂并完善整个游戏

本文通过Python的Ursina库,详细介绍了如何创建一个简单的3D我的世界风格游戏。包括建立手部动画、定义不同材质的方块、生成地形和树木,并展示了实现的完整代码。玩家可以交互式地放置和破坏方块,游戏还自动生成了随机树木,为游戏增加了更多乐趣。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天,我们要实现的效果是:

有手的动画,可以建造一个豆腐房


首先,我们可以添加一个手臂,定义Hand类

class Hand(Entity):
    def __init__(self):
        super().__init__(
            parent=camera.ui,
            model="cube",
            scale=(0.2,0.3),
            color=color.white,
            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)

在active()和passive()函数中分别设置了手的方向和动作

在Block的update函数中添加如下代码:

        if held_keys["left mouse"] or held_keys["right mouse"]:
            hand.active()
        else:
            hand.passive()

然后在程序后面写上

hand=Hand()

我们还可以设置一下地形,就是一开始是两层高的地形,最上面是草方块,最下面是不可破坏的基岩,然后在一开始自动生成一棵树

首先更改生成地形的for循环代码为:

height=2
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)

然后我们要添加树木,就需要原木的材质,从网上下载原木的材质后同样放到texture目录下,导入材质

log_texture=load_texture("texture/log.jpg")

然后定义Tree类

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)

最后生成一棵树

Tree(_height=5)

最终运行效果如下:


最终代码:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
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 Sky(Entity):
    def __init__(self):
        super().__init__(
            parent=scene,
            model="sphere",
            scale=1500,
            texture=sky_texture,
            double_sided=True,
            position=(0,0,0)
        )

class Hand(Entity):
    def __init__(self):
        super().__init__(
            parent=camera.ui,
            model="cube",
            scale=(0.2,0.3),
            color=color.white,
            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=2
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()

以上就是用Python制作《我的世界》的所有内容啦!

大家还可以根据自己的喜好制作更多地形,添加更多方块,添加物品栏之类的,这些就不再多讲啦!

下期预告:下一次我们将依然使用Ursina和我们制作的简易《我的世界》小游戏的基本框架,做一个好玩的3D迷宫游戏!敬请期待吧!

喜欢的话就点赞关注吧!我的所有专栏都是免费的,大家可以去订阅查看哦!


附资源(免费下载):

制作简易版《我的世界》所需要的材质图片-Python文档类资源-CSDN下载制作简易版《我的世界》所需要的材质图片更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/leleprogrammer/85382058

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值