python + ursina怎么做简单mc

1 篇文章 0 订阅
1 篇文章 0 订阅

现在插播一个教程:ursina制作简单mc。其中我们要实现飞行功能。
注: ursina不支持macOS

安装

安装python

首先去往python官网,之后选择Windows installer(64-bit)或Windows installer(32-bit),这要取决于你电脑的位数,是32位的选右面的,64位的选左面的:

看这个

下好了之后把点开,把勾全勾上,再点击Install Now就可以了,我已经安装好了,就不演示了。

打开cmd的方法

其实非常的简单,只需要按住Win+R,输入cmd,回车就完成了!

安装指令

参照上面步骤完成后,在cmd里输入pip install ursina,回车,这样我们的基础工作就完成了。
→不会python的点这里←

贴图

大家可以去网上搜,我来提供天空和地球的图片:

sky.png
sky.png

earth.png
earth.png

制作游戏

话不多说,直接放代码:

#导入模块
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="assets/texture/background/sky.png"):
        super().__init__(
            model="sphere",
            texture=texture,
            scale=150,
            double_sided=True
        )

#地球类
class earth(Entity):
    def __init__(self,texture="assets/texture/background/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 == "escape":
            quit()

		#触发
        if self.hovered:
            if key == "left mouse down":
                voxel = Voxel(position=self.position + mouse.normal)
                
            if key == "right mouse down":
                destroy(self)

#窗口类
app = Ursina()
window.fullscreen = True
window.color = color.black
window.fps_counter.enabled = False
window.exit_button.visible = False

sky = sky()
earth = earth()
player = FirstPersonController()

#飞行地毯
pl = Entity(
    model="plane",
    color=color.lime,
    texture="white_cube",
    scale=(2,0,2),
    texture_scale=(2,2),
    parent=scene,
    collider="box"
)

#创建地形
for x in range(30):
    for z in range(30):
        voxel = Voxel(position=(x,0,z))

#显示窗口
app.run()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值