如何将输入值存储为 Pygame 中的变量

How to store an input value as a variable in Pygame

我在 Pygame 中有一个输入框 class,我只在其中输入数字和我想要的,每次我按 ENTER 时,这个值都会存储在一个变量中以供使用来自另一个函数。谢谢大家。 这是输入框代码:

class InputBox:

    def __init__(self, x, y, w, h, text=''):
        self.rect = pg.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = False

    def handle_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                self.active = not self.active
            else:
                self.active = False
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:
                    print(self.text)
                    self.text = ''
                elif event.key == pg.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
        pg.draw.rect(screen, self.color, self.rect, 2)


def main():
    ...

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)
                
        for box in input_boxes:
            box.update()

        screen.fill((30, 30, 30))
        for box in input_boxes:
            box.draw(screen)

        pg.display.flip()
        clock.tick(30)

此示例是否有助于您理解概念:

user_input = {'user_input': []}


class UserInput:
    def __init__(self):
        self.input_ = input('Input something: ')
        user_input['user_input'].append(self.input_)


ask = UserInput()
print(user_input)

如果您想在函数中使用字典中的值,您可以这样访问该值:

dict_name['key_name']

将访问可以是列表、整数、元组、另一个字典、字符串的值,基本上变量名就是上面看到的,所以在函数中它可以像这样使用:

your_func(user_input['user_input'][0])

例如,如果函数的第一个参数是附加到 user_input['user_input'] 列表的第一个项目,在这种情况下就是用户输入的内容

将列表属性添加到 class InputBox 并将新输入附加到列表:

class InputBox:

    def __init__(self, x, y, w, h, text=''):
        # [...]

        self.input_list = []

    def handle_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                self.active = not self.active
            else:
                self.active = False
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:
                   
                    input_list.appned(self.text)

                    print(self.text)
                    self.text = ''

                elif event.key == pg.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                self.txt_surface = FONT.render(self.text, True, self.color)

您可以访问每个框的输入列表:

last_inout = input_boxes[0].input_list[-1]