用户:
小橙子_查看:3 回复:12 评论:3 创建时间:2023-07-07T15:54:04
【作品展示】

【作品介绍】
按下WASD移动,空格跳跃,右键放置方块,左键破坏方块。欢迎转发、改编。开始加载过程会有黑屏,请等待。(温馨提示:本作品是由柏林噪声生成地形,在作品中可以通过修改代码来修改种子)
【作品源代码】
from tkinter import *
from tkinter import messagebox
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from perlin_noise import PerlinNoise
def Create_World():
app = Ursina()
punch_sound = Audio('assets/punch_sound', loop=False, autoplay=False)
music = Audio('assets/Music', loop=True, autoplay=True)
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')
hand_texture = load_texture('assets/arm_texture')
block_pick = 0
textures = [grass_texture, stone_texture, brick_texture, dirt_texture]
window.fps_counter.enabled = False
window.exit_button.visible = False
def update():
global block_pick
if held_keys['1']: block_pick = 0
if held_keys['2']: block_pick = 1
if held_keys['3']: block_pick = 2
if held_keys['4']: block_pick = 3
if held_keys['left mouse'] or held_keys['right mouse']:
hand.active()
else:
hand.passtive()
class Sky(Entity):
def __init__(self):
super().__init__(
parent = scene,
model = 'sphere',
texture = sky_texture,
scale = 1000,
double_sided = True
)
class Hand(Entity):
def __init__(self):
super().__init__(
parent = camera.ui,
model = 'assets/arm',
texture = hand_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 passtive(self):
self.position = Vec2(0.4, -0.6)
class Block(Button):
def __init__(self, postition = (0,0,0), texture = grass_texture):
super().__init__(
parent = scene,
position = postition,
model = 'assets/block',
origin_y = 0.5,
texture = grass_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 == "right mouse down":
if block_pick == 0: block = Block(postition=self.position+mouse.normal, texture=grass_texture)
if block_pick == 1: block = Block(postition=self.position+mouse.normal, texture=stone_texture)
if block_pick == 2: block = Block(postition=self.position+mouse.normal, texture=brick_texture)
if block_pick == 3: block = Block(postition=self.position+mouse.normal, texture=dirt_texture)
punch_sound.play()
if key == "left mouse down":
destroy(self)
punch_sound.play()
noise = PerlinNoise(octaves=2, seed=random.randint(-10000, 10000))
scale = 24
for z in range(125):
for x in range(125):
if z > random.randint(10, 20):
block = Block(postition=(x, 0, z), texture=grass_texture)
block.y = int(noise([x/scale, z/scale]) * 8)
else:
block = Block(postition=(x, 0, z), texture=stone_texture)
player = FirstPersonController()
sky = Sky()
hand = Hand()
music.play()
app.run()
if __name__ == "__main__":
Create_World()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cnTraceback (most recent call last):
File "C:/Users/86187/Desktop/1.py", line 3, in <module>
from ursina import *
ModuleNotFoundError: No module named 'ursina'
点赞0
评论