Python - Pygame不在按钮上显示文本

时间:2017-08-13 01:45:12

标签: python button text pygame

我正在尝试在pygame中为游戏制作一个简单的菜单,但我很难显示菜单按钮的交互式文本。我有一个非常简单的菜单,有'开始'和'退出'选项,我正在尝试使用我制作的Button类来显示它们。

由于某种原因,按钮背景会亮起,但文本不会出现。当我滚动它们但没有文字时,我得到了一堆正确改变颜色的矩形。

我看了类似的问题,但似乎无法弄清楚为什么我的工作不起作用。任何帮助或指导将不胜感激。

以下是代码示例。颜色在单独的python文件中定义。

class Start(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)

        self.options = ['Start', 'Quit']
        self.buttons = pygame.sprite.Group()

    def initGraphics(self, screen):
        SceneBase.initGraphics(self, screen)

        info = pygame.display.Info()
        screenWidth, screenHeight = info.current_w, info.current_h

        font = pygame.font.Font('freesansbold.ttf', 30)

        for i, option in enumerate(self.options):
            rect = pygame.Rect(int(screenWidth/2) - 50, int(screenHeight/2) - 100 + i*50, 100, 30)
            passive_color = colors.BLACK
            active_color = colors.RED

            button = Button(rect, font, active_color, option, colors.WHITE, passive_color, option, colors.WHITE)

            self.buttons.add(button)

    def ProcessInput(self, events, pressed_keys):
        pass

    def Update(self):
        self.buttons.update()

    def Render(self):
        self.screen.fill(colors.WHITE)
        self.buttons.draw(self.screen)
        pygame.display.flip()


class Button(pygame.sprite.Sprite):
    def __init__(self, rect, font, active_color, active_text, active_textcolor, passive_color, passive_text, passive_textcolor):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((rect[2], rect[3]))

        self.rect = rect

        self.font = font

        self.active_color = active_color
        self.active_text = active_text
        self.active_textcolor = active_textcolor
        self.passive_color = passive_color
        self.passive_text = passive_text
        self.passive_textcolor = passive_textcolor

    def update(self):
        mouse = pygame.mouse.get_pos()

        if self.rect.x <= mouse[0] <= self.rect.x + self.rect.w and self.rect.y <= mouse[1] <= self.rect.y + self.rect.h:
            self.image.fill(self.active_color)
            self.renderButtonText(self.active_text, self.active_textcolor)
        else:
            self.image.fill(self.passive_color)
            self.renderButtonText(self.passive_text, self.passive_textcolor)

    def renderButtonText(self, text, color):
        textsurf = self.font.render(text, True, color)
        textrect = textsurf.get_rect()
        textrect.center = self.rect.center
        self.image.blit(textsurf, textrect)

这是SceneBase类的骨架代码:

pygame.init()


def run_game(width, height, fps, starting_scene):
    screen = pygame.display.set_mode((width, height))
    clock = pygame.time.Clock()

    active_scene = starting_scene

    initialized = False

    while active_scene:

        if not initialized:
            active_scene.initGraphics(screen)
            initialized = True

        pressed_keys = pygame.key.get_pressed()

        # Event filtering
        filtered_events = []
        for event in pygame.event.get():
            quit_attempt = False
            if event.type == pygame.QUIT:
                quit_attempt = True
            elif event.type == pygame.KEYDOWN:
                alt_pressed = pressed_keys[pygame.K_LALT] or \
                              pressed_keys[pygame.K_RALT]
                if event.key == pygame.K_ESCAPE:
                    quit_attempt = True
                elif event.key == pygame.K_F4 and alt_pressed:
                    quit_attempt = True

            if quit_attempt:
                active_scene.Terminate()
            else:
                filtered_events.append(event)

        active_scene.ProcessInput(filtered_events, pressed_keys)
        active_scene.Update()
        active_scene.Render()

        active_scene = active_scene.next

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

#==============================================================================
# The rest is code where you implement your game using the Scenes model


run_game(500, 500, 60, Start())

1 个答案:

答案 0 :(得分:0)

似乎在渲染背景之前先渲染按钮文本,所以可能发生的是正在绘制文本,但是在同一帧中绘制了按钮,因此看起来没有文字。尝试将更新函数的代码添加到名为checkIfHovering的新函数中,并将其写入:

    def checkIfHovering(self):
        mouse = pygame.mouse.get_pos()

        if self.rect.x <= mouse[0] <= self.rect.x + self.rect.w and self.rect.y <= mouse[1] <= self.rect.y + self.rect.h:
            self.image.fill(self.active_color)
            self.renderButtonText(self.active_text, self.active_textcolor)
        else:
            self.image.fill(self.passive_color)
            self.renderButtonText(self.passive_text, self.passive_textcolor)

此外,将“渲染”功能更改为:

    def Render(self):
        self.screen.fill(colors.WHITE)
        self.buttons.draw(self.screen)
        self.buttons.checkIfHovering()
        pygame.display.flip()

那样,它会在绘制按钮背景之后打印文本。