我正在用 ursina、python 构建游戏,但无法获取玩家的当前位置。
这是我的代码:
from ursina import *
from ursina.prefabs.first_person_controller \
import FirstPersonController
from os import listdir
textures = listdir("assets/texture/")
app = Ursina()
dict_textures = {
index + 1 : {
"name": file.replace(".png", "_texture"),
"texture": load_texture(file)
}
for index, file in enumerate(textures)
}
sky_texture = load_texture("/assets/Sky.png")
def input(key):
if key == "escape":
exit()
elif key == 'r':
position = player.position
print(position)
class Sky(Entity):
def __init__(self):
super().__init__(
parent=scene,
model="sphere",
texture=sky_texture,
scale = 5000,
double_sided = True)
class Grass_Block(Button):
def __init__(
self,
position,
texture = load_texture(
"/assets/texture/Grass_Block.png"
)
):
super().__init__(
parent=scene,
position=position,
model="cube",
origin_y=0.5,
texture=texture,
color=color.white,
colision = "mesh",
double_sided = True)
def create_map():
with open("maze.txt", "r") as file:
map = file.read()
map = map.split("\n")
map = [list(line) for line in map]
for z in range(len(map)):
for x in range(len(map[0])):
_ = Grass_Block(position=(z, 0, x))
if map[z][x] == '1':
for i in range(5):
_ = Grass_Block(
position=(z, i+1, x),
texture="/assets/texture/Mushroom_dirt.png"
)
class Player(Entity):
def __init__(self):
super().__init__()
self.controller = FirstPersonController(
mouse_sensitivity = Vec2(100, 100),
player = self
)
if __name__ == "__main__":
create_map()
player = Player()
sky = Sky()
app.run()
每次输入 R 键都会打印 Vec3(0,0,0) 但不打印当前位置。创建用于建造墙壁、迷宫以及玩家应该行走的地板的地图。因为玩家被设置为 (0,0,0) 位置,所以他会在墙内生成。我实际上可以通过使用空格键跳跃来退出墙壁并坐在上面,但我也想设置初始位置。似乎玩家的位置属性没有更新并且无法更改。
我做错了什么?
我删除了 Player 类并使用它:
if __name__ == "__main__":
player = FirstPersonController(
speed=10,
jump_height=1
)
player.position = (1,0,1)
create_map()
sky = Sky()
app.run()