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

时间:2017-11-25 23:18:49

标签: python textbox pygame

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()

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

我的想法是,单击或激活的文本框将执行事件部分中的内容,因此您不必重复两次。但是,我不知道如何实现它。

提前致谢

2 个答案:

答案 0 :(得分:2)

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

类似的东西:

# 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

答案 1 :(得分:1)

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

完整的工作示例

import pygame

class InputBox():

    def __init__(self, x, y):

        self.font = pygame.font.Font(None, 32)

        self.inputBox = pygame.Rect(x, y, 140, 32)

        self.colourInactive = pygame.Color('lightskyblue3')
        self.colourActive = pygame.Color('dodgerblue2')
        self.colour = self.colourInactive

        self.text = ''

        self.active = False
        self.isBlue = True

    def handle_event(self, event):

        if event.type == pygame.MOUSEBUTTONDOWN:
            self.active = self.inputBox.collidepoint(event.pos)
            self.colour = self.colourActive if self.active else self.colourInactive
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_RETURN:
                    print(self.text)
                    self.text = ''
                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode

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

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

# --- main ---

def mainloop():

    # create objects    
    input1 = InputBox(50, 50)
    input2 = InputBox(450, 50)

    clock = pygame.time.Clock()

    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            # handle every event
            input1.handle_event(event)
            input2.handle_event(event)

        screen.fill((128,128, 128))

        # draw it
        input1.draw(screen)
        input2.draw(screen)

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


pygame.init()        
screen = pygame.display.set_mode((800,600))
mainloop()

BTW:您可以将更多输入保留在列表中(例如在GUI框架中)

def mainloop():

    widgets = [
        InputBox(50, 50),
        InputBox(450, 50),
        InputBox(50, 150),
        InputBox(450, 150),
        InputBox(50, 250),
        InputBox(450, 250),
        InputBox(50, 350),
        InputBox(450, 350),
    ]

    clock = pygame.time.Clock()

    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            for child in widgets:
                child.handle_event(event)

        screen.fill((128,128, 128))

        for child in widgets:
            child.draw(screen)

        pygame.display.flip()

        clock.tick(60)