Ursina 中的第一人称控制器 y-pos 逻辑

First-person controller y-pos logic in Ursina

我有一个 Minecraft 的克隆,我想看看如果玩家从岛上掉下来会退出游戏。我以为如果我写的话。

if player.position == Vec3(x, -80, z):
     quit()

它会退出游戏 但这没有用,所以我不知道该怎么做。

这是 Minecraft 的克隆代码。

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()

grass_color = color.rgb(1, 235, 113)
stone_color = color.rgb(138,141,143)
dirt_color = color.rgb(200, 157, 124)

block_pick = 1

def update():
    if held_keys['escape']:
        quit()

    global block_pick

    if held_keys['1']: block_pick = 1
    if held_keys['2']: block_pick = 2
    if held_keys['3']: block_pick = 3

class Voxel(Button):
    def __init__(self, position = (0,0,0), color = color.white):
        super().__init__(
            parent = scene,
            position = position,
            model = 'cube',
            origin_y = 0.5,
            texture = 'white_cube',
            color = color,
            highlight_color = color,
                    )

    def input(self,key):
        if self.hovered:
            if key == 'right mouse down':
                if block_pick == 1:voxel = Voxel(position = self.position + mouse.normal, color = grass_color)
                if block_pick == 2:voxel = Voxel(position = self.position + mouse.normal, color = stone_color)
                if block_pick == 3:voxel = Voxel(position = self.position + mouse.normal, color = dirt_color)
            if key == 'left mouse down':
                destroy(self)
            
for z in range(20):
    for x in range(20):
        voxel = Voxel(position=(x,0,z), color = grass_color)

for y in range(3):
    for x in range(20):
        for z in range(20):
            voxel = Voxel(position=(x,y + -3,z), color = dirt_color)

player = FirstPersonController()
app.run()

您只检查了玩家的 y 位置的单个值,这是行不通的 - 毕竟,您很快就会倒下。您可以检查玩家的身高是否低于某个临界值:

def update():
    if player.y < -80:
        print('You fell to death!')
        app.quit()