Python制作我的世界

大家应该很多人没见过python做的我的世界,今天我就来演示一下:

首先得安装ursina:

pip install ursina

然后新建一个python文件,名称自选

里面写上以下内容

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from perlin_noise import PerlinNoise  

app = Ursina()

grass_texture = load_texture('assets/grass_block.png')
stone_texture = load_texture('assets/stone_block.png')
brick_texture = load_texture('assets/brick_block.png')
dirt_texture = load_texture('assets/dirt_block.png')
sky_texture = load_texture('assets/skybox.png')
arm_texture = load_texture('assets/arm_texture.png')
punch_sound = Audio('assets/punch_sound',loop=False,autoplay=False)
block_pick = 1

window.fps_counter.enabled = False
window.exit_button.visible = False

scene.fog_color = color.white
scene.fog_density = 0.04

def input(key):
    if key == 'q' or key == 'escape':
        quit()
def update():
    global block_pick
    if held_keys['1']: block_pick = 1
    if held_keys['2']: block_pick = 2
    if held_keys['3']: block_pick = 3
    if held_keys['4']: block_pick = 4

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

class Block(Button):
    def __init__(self,position=(0,0,0),texture = grass_texture):
        super().__init__(
            parent = scene,
            position = position,
            model = 'assets/block',
            origin_y = 0.5,
            texture = texture,
            color = color.color(0,0,random.uniform(0.9,1)),
            #highlight_color = color.green,
            scale = 0.5
        )
    
    def input(self,key):
        if self.hovered:
            if key == 'left mouse down':
                punch_sound.play()
                if block_pick == 1: block = Block(position = self.position+mouse.normal,texture=grass_texture)
                if block_pick == 2: block = Block(position = self.position+mouse.normal,texture=stone_texture)
                if block_pick == 3: block = Block(position = self.position+mouse.normal,texture=brick_texture)
                if block_pick == 4: block = Block(position = self.position+mouse.normal,texture=dirt_texture)
            if key == 'right mouse down':
                punch_sound.play()
                destroy(self)

class Sky(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model = 'sphere',
            texture = sky_texture,
            scale = 150,
            double_sided = True
        )

class Hand(Entity):
    def __init__(self):
        super().__init__(
            parent = camera.ui,
            model = 'assets/arm',
            texture = arm_texture,
            scale = 0.2,
            rotation = Vec3(150,-10,0),
            position = Vec2(0.4,-0.6)
        )
    
    def active(self):
        self.position = Vec2(0.3,-0.5)
        
    def passive(self):
        self.position = Vec2(0.4,-0.6)

#初始化并使用柏林噪声生成随机地形
noise = PerlinNoise(octaves=3,seed=2023)
scale = 24
for z in range(50):
    for x in range(50):
        block = Block(position = (x,0,z))
        block.y = floor(noise([x/scale,z/scale])*8)

player = FirstPersonController()
sky = Sky()
hand = Hand()

app.run()

下载资源请使用以下链接下载assets文件夹:https://pan.xunlei.com/s/VNmt7tm2xppy6kWOL6vab87zA1?pwd=hect&path=%2F%E5%B8%B8%E7%94%A8%E7%BC%96%E7%A8%8B%E8%B5%84%E6%BA%90%E5%88%86%E4%BA%AB%2F02%20%E4%BE%AF%E8%80%81%E5%B8%88%E7%BC%96%E7%A8%8B%E7%BD%91%E7%AB%99%E7%9B%B8%E5%85%B3%E7%B4%A0%E6%9D%90%2FPython%E5%A4%8D%E5%88%BB%E6%88%91%E7%9A%84%E4%B8%96%E7%95%8C-%E7%B4%A0%E6%9D%90​​​​​​​

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值