python - 为什么我的按钮没有显示在 pygame 中?

标签 python python-3.x pygame

我正在开始屏幕上制作按钮,但它们不会显示。我创建了一个按钮类,其中包含按钮所需的一切。我敢打赌,我的类中的绘图函数有问题。有谁知道如何解决这一问题?如果还有其他错误,请告诉我。谢谢!

class Button(object):
    def __init__(self, text, text_size, text_color, x, y, width, height, ic, ac):

        self.text = text
        self.text_size = text_size
        self.text_color = text_color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.ic = ic
        self.ac = ac

        self.inactive_button = pygame.Surface((width, height))
        self.inactive_button.fill(ic)

        self.active_button = pygame.Surface((width, height))
        self.active_button.fill(ac)

        self.button_rect = self.inactive_button
        self.rect = self.button_rect.get_rect()

        font_name = pygame.font.match_font('arial')
        font = pygame.font.Font(font_name, text_size)
        font_text = font.render(text, True, text_color)
        text_rect = font_text.get_rect(center=self.rect.center)

        self.inactive_button.blit(font_text, text_rect)
        self.active_button.blit(font_text, text_rect)

        self.rect.topleft = (x, y)

        self.hovered = False
        # self.clicked = False

    def update(self):

        if self.hovered:
            self.button_rect = self.active_button
        else:
            self.button_rect = self.inactive_button

    def draw(self, screen):

        screen.blit(self.button_rect, self.rect)

    def handle_event(self, event, command=None):

        mouse = pygame.mouse.get_pos()
        if event.type == pygame.MOUSEMOTION:
            self.hovered = self.rect.collide_rect(mouse, self.rect)
            if event.type == pygame.MOUSEBUTTONDOWN:
                if command == 'play' and command is not None:
                    game_loop()
                elif command == 'quit' and command is not None:
                    pygame.quit()
                    quit()
                elif command == 'instructions' and command is not None:
                    instructions()
                elif command == 'credits' and command is not None:
                    credits()
                elif command == 'back' and command is not None:
                    start_screen()


def start_screen():

    start = True

    while start:
        clock.tick(FPS)
        for events in pygame.event.get():
            if events.type == pygame.QUIT:
                pygame.quit()
                quit()

            Button("Play", 55, WHITE, 180, 285, 120, 60, BLUE, LIGHT_BLUE)
            Button("Quit", 30, BLUE, 202, 450, 80, 40, WHITE, GRAY)
            Button("Credits", 35, BLACK, 310, 45, 140, 30, WHITE, GRAY)
            Button("Instructions", 30, BLACK, 20, 45, 140, 30, WHITE, GRAY)

start_screen()

最佳答案

它的工作原理与您上一个问题中的功能不同。

您必须在 while True 之前创建实例,并在 for event 内使用 b1.handle_event(events) 来检查按钮是否被单击,在for event之后使用b1.draw()来绘制它(在清屏之后,在display.flip()之前)

def start_screen():

    start = True

    b1 = Button("Play", 55, WHITE, 180, 285, 120, 60, BLUE, LIGHT_BLUE)
    b2 = Button("Quit", 30, BLUE, 202, 450, 80, 40, WHITE, GRAY)
    b3 = Button("Credits", 35, BLACK, 310, 45, 140, 30, WHITE, GRAY)
    b4 = Button("Instructions", 30, BLACK, 20, 45, 140, 30, WHITE, GRAY)

    while start:

        clock.tick(FPS)

        # - events -

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            b1.handle_event(event)
            b2.handle_event(event)
            b3.handle_event(event)
            b4.handle_event(event)

        # - updates (without draws) - 

        b1.update()
        b2.update()
        b3.update()
        b4.update()

        # - draws (without updates) -

        screen.fill((0,0,0))

        b1.draw(screen)
        b2.draw(screen)
        b3.draw(screen)
        b4.draw(screen)

        pygame.display.flip()
<小时/>

编辑:

您的缩进错误,并且您没有检查是否单击悬停按钮,因此一次单击可能会同时按下所有按钮。

def handle_event(self, event, command=None):

    mouse = pygame.mouse.get_pos()

    if event.type == pygame.MOUSEMOTION:
        self.hovered = self.rect.collide_rect(mouse, self.rect)

    if event.type == pygame.MOUSEBUTTONDOWN:
        if self.hovered:
            if command == 'play' and command is not None:
                game_loop()
            elif command == 'quit' and command is not None:
                pygame.quit()
                quit()
            elif command == 'instructions' and command is not None:
                instructions()
            elif command == 'credits' and command is not None:
                credits()
            elif command == 'back' and command is not None:
                start_screen()

但为了使按钮更通用 - 您可以指定函数的名称而不是字符串

def handle_event(self, event, command=None):

    mouse = pygame.mouse.get_pos()

    if event.type == pygame.MOUSEMOTION:
        self.hovered = self.rect.collide_rect(mouse, self.rect)

    if event.type == pygame.MOUSEBUTTONDOWN:
        if self.hovered and command is not noen:
            command()

或者甚至将函数名称放入__init__

关于python - 为什么我的按钮没有显示在 pygame 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351209/

相关文章:

python - 从 scikit 创建 ngrams 学习和计数向量化器抛出内存错误

python - 是否可以在 Flask 和 Python 3 中使用 websockets?

windows - 权限错误: [WinError 5]

python - Pygame - 寻找 2 个 .txt 文件和用户输入的匹配项

python - 在 Intellij IDEA 中导入 pygame

python-3.x - 如何在 PyOpenGL 中获取当前相机位置?

Python:制作包含两列项目的字典

python - 将 Pandas 列转换为具有特殊字符分隔的字符串

python - VIM:预览高度

python - 提取组