手把手教你用Python编一个《我的世界》 2.材质及第一人称

这篇具有很好参考价值的文章主要介绍了手把手教你用Python编一个《我的世界》 2.材质及第一人称。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本次,我们将实现这样一个效果:

手把手教你用Python编一个《我的世界》 2.材质及第一人称


首先,导入ursina模块

from ursina import *

创建app

app=Ursina()

定义Block类,继承自Button

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
        )

然后,我们需要一个天空

定义Sky类

class Sky(Entity):
    def __init__(self):
        super().__init__(
            parent=scene,
            model="sphere",
            scale=1500,
            texture=sky_texture,
            double_sided=True,
            position=(0,0,0)
        )

因为我们所有的方块包括天空都需要图片材质,所以我们在程序开头写以下代码:

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")

然后咱们先创建一个超平坦地形,厚度就只有1层吧,因为方块多了很容易变卡掉帧

那我们就把底面设置成基岩吧

height=1
for y in range(0,height):
    for z in range(-15,16):
        for x in range(-15,16):
            print(f"Position:({x},{y},{z})")
            texture=bedrock_texture
            Block(position=(x,y,z),texture=texture)

这时候,我们需要一个第一人称控制器,在程序前导入

from ursina.prefabs.first_person_controller import FirstPersonController

然后在程序后面写上

player=FirstPersonController()

顺便创建个天空

sky=Sky()

最后运行app

app.run()

我们还要继续设置放置和破坏方块的功能,在Block中添加两个函数,用于设置选取方块类型还有放置和破坏方块的效果

    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

在导入材质后添加默认选取:

select_texture=grass_texture

最后运行效果如下:

手把手教你用Python编一个《我的世界》 2.材质及第一人称


最终代码:

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

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")
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 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

height=1
for y in range(0,height):
    for z in range(-15,16):
        for x in range(-15,16):
            print(f"Position:({x},{y},{z})")
                texture=bedrock_texture
            Block(position=(x,y,z),texture=texture)

player=FirstPersonController()
sky=Sky()

app.run()

下节我们将继续优化游戏,大家可以免费订阅我的专栏哦!

喜欢的话就点赞关注吧!我们下期再见!


附资源(免费下载):

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

到了这里,关于手把手教你用Python编一个《我的世界》 2.材质及第一人称的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 手把手教你用Python实现2048小游戏

    感觉好久没有写小游戏玩了,今天恰巧有空.这次我来用Python做个2048小游戏吧.废话不多说,文中有非常详细的代码示例,需要的朋友可以参考下 目录 一、开发环境 二、环境搭建 三、原理介绍 四、效果图 Python版本:3.6.4 相关模块: pygame模块; 以及一些Python自带的模块。 安装

    2024年04月28日
    浏览(47)
  • 爬虫实战|手把手教你用Python爬虫(附详细源码)

    实践来源于理论,做爬虫前肯定要先了解相关的规则和原理,要知道互联网可不是法外之地,你一顿爬虫骚操作搞不好哪天就…  首先,咱先看下爬虫的定义:网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自

    2024年02月02日
    浏览(62)
  • 手把手教你用Python编写配置脚本引擎(福利篇)

    版权声明:原创不易,本文禁止抄袭、转载需附上链接,侵权必究! 配置信息初始化 定义配置引擎类和初始化方法,其中有两个属性,配置实例对象及配置文件路径: 将配置信息写入到配置文件中,该方法有三个形参,category(配置信息类别),name(配置字段名称),value(配置字

    2024年02月06日
    浏览(55)
  • 手把手教你用SQLServer连接Visual Studio2019并编写一个学生信息管理页面

    目录 安装SQLServer 创建新项目 建数据库建表 窗体设计 代码实现  整体效果 ​ 用SQLServer连接Visual Studio,首先需要下载SQLServer app。 下载教程,我之前写过,可以点击如下链接先下载安装SQLServer: SQL Server(express)安装教程 安装好SQL之后,打开VisualStudio2019,新建一个window项目 ,步

    2024年02月12日
    浏览(38)
  • 手把手教你用python演奏音乐(以富士山下为例)

    目录 1.代码 2.文本 3.效果图  这两天,我非常无聊,琢磨出了十二平均律,并利用mido库进行编写,实现了利用python演奏音乐。废话不多说了,直接上代码,供诸位大佬把玩。  

    2024年02月12日
    浏览(29)
  • 手把手教你用python一键抢12306火车票(附代码)

    哈喽,哈喽~,一年一度的抢火车票大战正式拉开序幕… 然饿大多数人碰到的是这种情况:当你满心期待摩拳擦掌准备抢票的时候,你会发现一票难求!想回趟家真难! 那么作为程序猿的你,当然要用程序猿的方式来抢票!下面分享用python来抢票! 城市cookie可根据具体需求自

    2024年02月15日
    浏览(49)
  • 手把手教你用代码画架构图

    作者:京东物流 覃玉杰 本文将给大家介绍一种简洁明了软件架构可视化模型——C4模型,并手把手教大家如何使用 代码 绘制出精美的C4架构图。 阅读本文之后,读者画的架构图将会是这样的: 注:该图例仅作绘图示例使用,不确保其完整性、可行性。 C4是软件架构可视化

    2024年02月04日
    浏览(41)
  • 快收藏!手把手教你用AI绘画

    点个关注👆跟腾讯工程师学技术 最近看到一篇有趣的文章,一副名为《太空歌剧院》(如下图)的艺术品在某美术比赛上,获得了第一名的成绩, 有意思的是这件作品是通过AI来实现的画作, 顿时觉得非常神奇。结合近期科技媒体频频报道的AI作画爆火现象,深入了解了下

    2024年02月09日
    浏览(29)
  • 手把手教你用AirtestIDE无线连接手机

    一直以来,我们发现同学们都挺喜欢用无线的方式连接手机,正好安卓11出了个无线连接的新姿势,我们今天就一起来看看,如何用AirtestIDE无线连接你的Android设备~ 当 手机与电脑处在同一个wifi 下,即可尝试无线连接手机了,但是这种方式受限于网络连接的稳定性,可能会出

    2023年04月18日
    浏览(43)
  • 手把手教你用 Jenkins 自动部署 SpringBoot

    CI/CD 是一种通过在应用开发阶段引入自动化来频繁向客户交付应用的方法。 CI/CD 的核心概念可以总结为三点: 持续集成 持续交付 持续部署 CI/CD 主要针对在集成新代码时所引发的问题(俗称\\\"集成地狱\\\")。 为什么会有集成地狱这个“雅称”呢?大家想想我们一个项目部署的

    2024年02月02日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包