简体   繁体   English

Python Ursina 隐藏用户界面

[英]Python Ursina hide UI

I have an GUI inventory and I don't know how to hide it and all the items there are inside.我有一个 GUI 库存,但我不知道如何隐藏它以及里面的所有物品。 I tried to make the class Inventory(Entity) enabled or not and it didn't worked because it only gets enables and never disabled.我试图使类 Inventory(Entity) 启用或不启用,但它没有工作,因为它只启用而从未禁用。 This is my first post so, please, be nice.这是我的第一篇文章,所以,拜托,好。

My code:我的代码:

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
    ...

Setting entity.enabled = False will deactivate it and all it's children.设置entity.enabled = False将停用它及其所有子项。 To make it reappear, set it back to True.要使其重新出现,请将其设置回 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                                             
    )

    

By setting gui.enable = False or gui.visible = False you can hide the gui.通过设置gui.enable = Falsegui.visible = False你可以隐藏 gui。 gui.enable = False disables it and all of his parents, gui.visible = False just makes it invisible. gui.enable = False禁用它和他所有的父母, gui.visible = False只是让它不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM