Python 如何在pygame中实现两个输入框,而不必重复代码

Python 如何在pygame中实现两个输入框,而不必重复代码,python,textbox,pygame,Python,Textbox,Pygame,上面是一个工作函数,用于制作一个屏幕,屏幕上有一个文本框。现在,您如何在同一屏幕上创建两个文本框,而不只是复制和粘贴同一代码两次 我的想法是,点击或激活的文本框将完成事件部分的内容,因此您不必重复两次。然而,我不知道如何实现这一点 提前感谢您可以创建一个类,并根据需要多次实例化不同的输入框 大概是这样的: def InputBox(): font = pygame.font.Font(None, 32) inputBox = pygame.Rect(50, 50, 140, 3

上面是一个工作函数,用于制作一个屏幕,屏幕上有一个文本框。现在,您如何在同一屏幕上创建两个文本框,而不只是复制和粘贴同一代码两次

我的想法是,点击或激活的文本框将完成事件部分的内容,因此您不必重复两次。然而,我不知道如何实现这一点


提前感谢

您可以创建一个类,并根据需要多次实例化不同的输入框

大概是这样的:

def InputBox():
    font = pygame.font.Font(None, 32)

    inputBox = pygame.Rect(50, 50, 140, 32)
    colourInactive = pygame.Color('lightskyblue3')
    colourActive = pygame.Color('dodgerblue2')
    colour = colourInactive
    text = ''

    active = False
    isBlue = True

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                    if inputBox.collidepoint(event.pos):
                        active = not active
                    else:
                        active = False
                    colour = colourActive if active else colourInactive
            if event.type == pygame.KEYDOWN:
                if active:
                    if event.key == pygame.K_RETURN:
                        print(text)
                        text = ''
                    elif event.key == pygame.K_BACKSPACE:
                        text = text[:-1]
                    else:
                        text += event.unicode

        screen.fill(screenGray)

        txtSurface = font.render(text, True, colour)
        width = max(200, txtSurface.get_width()+10)
        inputBox.w = width
        screen.blit(txtSurface, (inputBox.x+5, inputBox.y+5))
        pygame.draw.rect(screen, colour, inputBox, 2)

        if isBlue:
            color = (0, 128, 255)
        else:
            color = (255, 100, 0)

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

InputBox()
#伪代码#
类输入框(pygame.Rect):
定义初始化(self,position=(50,50,140,32),
font=pygame.font.font(无,32),
color\u inactive=pygame.color('lightskyblue3'),
color\u active=pygame.color('dodgerblue2'),
文本=“”,
活动=错误)

super()

大概是这样的:

def InputBox():
    font = pygame.font.Font(None, 32)

    inputBox = pygame.Rect(50, 50, 140, 32)
    colourInactive = pygame.Color('lightskyblue3')
    colourActive = pygame.Color('dodgerblue2')
    colour = colourInactive
    text = ''

    active = False
    isBlue = True

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                    if inputBox.collidepoint(event.pos):
                        active = not active
                    else:
                        active = False
                    colour = colourActive if active else colourInactive
            if event.type == pygame.KEYDOWN:
                if active:
                    if event.key == pygame.K_RETURN:
                        print(text)
                        text = ''
                    elif event.key == pygame.K_BACKSPACE:
                        text = text[:-1]
                    else:
                        text += event.unicode

        screen.fill(screenGray)

        txtSurface = font.render(text, True, colour)
        width = max(200, txtSurface.get_width()+10)
        inputBox.w = width
        screen.blit(txtSurface, (inputBox.x+5, inputBox.y+5))
        pygame.draw.rect(screen, colour, inputBox, 2)

        if isBlue:
            color = (0, 128, 255)
        else:
            color = (255, 100, 0)

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

InputBox()
#伪代码#
类输入框(pygame.Rect):
定义初始化(self,position=(50,50,140,32),
font=pygame.font.font(无,32),
color\u inactive=pygame.color('lightskyblue3'),
color\u active=pygame.color('dodgerblue2'),
文本=“”,
活动=错误)

super()。然后你可以多次使用它

完整的工作示例

# pseudocode #

class InputBox(pygame.Rect):
    def __init__(self, position=(50, 50, 140, 32), 
                 font=pygame.font.Font(None, 32), 
                 color_inactive=pygame.Color('lightskyblue3'),
                 color_active=pygame.Color('dodgerblue2'),
                 text = '',
                 active=False)
        super().__init__(position)    #<- this may need to be adjusted to pygame Rect specs
        self.font = font
        self.color_inactive = color_inactive
        self.color_active = color_active
        self.color = color_inactive
        self.text = text
        self.active = active

顺便说一句:对于更多输入,您可以将它们保留在列表中(如GUI框架中)


您可以使用绘制类和处理事件的方法创建类。然后你可以多次使用它

完整的工作示例

# pseudocode #

class InputBox(pygame.Rect):
    def __init__(self, position=(50, 50, 140, 32), 
                 font=pygame.font.Font(None, 32), 
                 color_inactive=pygame.Color('lightskyblue3'),
                 color_active=pygame.Color('dodgerblue2'),
                 text = '',
                 active=False)
        super().__init__(position)    #<- this may need to be adjusted to pygame Rect specs
        self.font = font
        self.color_inactive = color_inactive
        self.color_active = color_active
        self.color = color_inactive
        self.text = text
        self.active = active

顺便说一句:对于更多输入,您可以将它们保留在列表中(如GUI框架中)