Python Ursina 隐藏 UI
Python Ursina hide UI
我有一个 GUI 物品栏,但我不知道如何隐藏它以及里面的所有物品。我试图使 class Inventory(Entity) 启用或不启用,但它没有用,因为它只启用而从未禁用。这是我的第一个 post 所以,请保持友善。
我的代码:
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
class Inventory(Entity):
def __init__(self):
player.enabled = False
super().__init__(
parent = camera.ui,
model = 'quad',
scale = (.5, .8),
origin = (-.5, .5),
position = (-.3,.4),
texture = 'white_cube',
texture_scale = (5,8),
color = color.dark_gray
)
self.item_parent = Entity(parent=self, scale=(1/5,1/8))
enable = False
def input(key):
if key == 'f':
inventory_enable()
def inventory_enable():
inventory = Inventory()
Inventory().enable = False
...
设置entity.enabled = False
将停用它及其所有子项。要使其重新出现,请将其设置回 True。
app = Ursina()
class Inventory(Entity):
def __init__(self):
player.enabled = False
super().__init__(
parent = camera.ui,
)
self.inventory_ui = Entity(parent = self,
model = 'quad',
scale = (.5, .8),
origin = (-.5, .5),
position = (-.3,.4),
texture = 'white_cube',
texture_scale = (5,8),
color = color.dark_gray,
enable = True
)
self.item_parent = Entity(parent=self.inventory_ui, scale=(1/5,1/8))
def find_free_spot(self):
taken_spots = [(int(e.x), int(e.y)) for e in self.item_parent.children]
for y in range(8):
for x in range(5):
if not (x,-y) in taken_spots:
return (x,-y)
def append(self, item):
icon = Draggable(
parent = Inventory().item_parent,
model = 'quad',
texture = item,
color = color.white,
origin = (-.5,.5),
position = self.find_free_spot(),
z = -.1,
)
name = item.replace('_', ' ').title()
icon.tooltip = Tooltip(name)
icon.tooltip.background.color = color.color(0,0,0,.8)
def drag():
icon.org_pos = (icon.x, icon.y)
def drop():
icon.x = int(icon.x)
icon.y = int(icon.y)
'''if the spot is taken, swap positions'''
for c in self.children:
if c == icon:
continue
if c.x == icon.x and c.y == icon.y:
print('swap positions')
c.position = icon.org_pos
icon.drag = drag
icon.drop = drop
#removed load_texture
grass_texture = "assets/grass.png"
soil_texture = "assets/soil.png"
stone_texture = "assets/stone.png"
wood_texture = "assets/wood.png"
sky_texture = load_texture("assets/skybox.png")
current_texture = grass_texture
def update():
global current_texture
if held_keys['1']: current_texture = grass_texture
if held_keys['2']: current_texture = soil_texture
if held_keys['3']: current_texture = stone_texture
if held_keys['4']: current_texture = wood_texture
# added
if held_keys['g']:
save_game()
if held_keys['left mouse'] or held_keys['right mouse']:
hand.active()
else:
hand.passive()
def input(key):
key_f = 0
if key == 'escape':
quit()
if key == 'f' and key_f == 0:
key_f = 1
inventory_enable()
else:
key_f = 0
inventory_close()
def inventory_close():
Inventory().inventory_ui.enable = False
def inventory_enable():
inventory = Inventory()
Inventory().enable = False
def add_item():
Inventory().append(random.choice(('bag', 'bow_arrow', 'gem', 'orb', 'sword')))
for i in range(7):
add_item()
add_item_button = Button(
scale = (.1,.1),
x = -.5,
color = color.lime.tint(-.25),
text = '+',
tooltip = Tooltip('Add random item'),
on_click = add_item
)
```
我有一个 GUI 物品栏,但我不知道如何隐藏它以及里面的所有物品。我试图使 class Inventory(Entity) 启用或不启用,但它没有用,因为它只启用而从未禁用。这是我的第一个 post 所以,请保持友善。
我的代码:
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
class Inventory(Entity):
def __init__(self):
player.enabled = False
super().__init__(
parent = camera.ui,
model = 'quad',
scale = (.5, .8),
origin = (-.5, .5),
position = (-.3,.4),
texture = 'white_cube',
texture_scale = (5,8),
color = color.dark_gray
)
self.item_parent = Entity(parent=self, scale=(1/5,1/8))
enable = False
def input(key):
if key == 'f':
inventory_enable()
def inventory_enable():
inventory = Inventory()
Inventory().enable = False
...
设置entity.enabled = False
将停用它及其所有子项。要使其重新出现,请将其设置回 True。
app = Ursina()
class Inventory(Entity):
def __init__(self):
player.enabled = False
super().__init__(
parent = camera.ui,
)
self.inventory_ui = Entity(parent = self,
model = 'quad',
scale = (.5, .8),
origin = (-.5, .5),
position = (-.3,.4),
texture = 'white_cube',
texture_scale = (5,8),
color = color.dark_gray,
enable = True
)
self.item_parent = Entity(parent=self.inventory_ui, scale=(1/5,1/8))
def find_free_spot(self):
taken_spots = [(int(e.x), int(e.y)) for e in self.item_parent.children]
for y in range(8):
for x in range(5):
if not (x,-y) in taken_spots:
return (x,-y)
def append(self, item):
icon = Draggable(
parent = Inventory().item_parent,
model = 'quad',
texture = item,
color = color.white,
origin = (-.5,.5),
position = self.find_free_spot(),
z = -.1,
)
name = item.replace('_', ' ').title()
icon.tooltip = Tooltip(name)
icon.tooltip.background.color = color.color(0,0,0,.8)
def drag():
icon.org_pos = (icon.x, icon.y)
def drop():
icon.x = int(icon.x)
icon.y = int(icon.y)
'''if the spot is taken, swap positions'''
for c in self.children:
if c == icon:
continue
if c.x == icon.x and c.y == icon.y:
print('swap positions')
c.position = icon.org_pos
icon.drag = drag
icon.drop = drop
#removed load_texture
grass_texture = "assets/grass.png"
soil_texture = "assets/soil.png"
stone_texture = "assets/stone.png"
wood_texture = "assets/wood.png"
sky_texture = load_texture("assets/skybox.png")
current_texture = grass_texture
def update():
global current_texture
if held_keys['1']: current_texture = grass_texture
if held_keys['2']: current_texture = soil_texture
if held_keys['3']: current_texture = stone_texture
if held_keys['4']: current_texture = wood_texture
# added
if held_keys['g']:
save_game()
if held_keys['left mouse'] or held_keys['right mouse']:
hand.active()
else:
hand.passive()
def input(key):
key_f = 0
if key == 'escape':
quit()
if key == 'f' and key_f == 0:
key_f = 1
inventory_enable()
else:
key_f = 0
inventory_close()
def inventory_close():
Inventory().inventory_ui.enable = False
def inventory_enable():
inventory = Inventory()
Inventory().enable = False
def add_item():
Inventory().append(random.choice(('bag', 'bow_arrow', 'gem', 'orb', 'sword')))
for i in range(7):
add_item()
add_item_button = Button(
scale = (.1,.1),
x = -.5,
color = color.lime.tint(-.25),
text = '+',
tooltip = Tooltip('Add random item'),
on_click = add_item
)
```