# 创建开启游戏的play按钮

import pygame.font


class Button():
    """初始化按钮的属性"""
    def __init__(self, ai_settings, screen, msg):
        self.screen = screen
        self.screen_rect = screen.get_rect()

        # 设置按钮的尺寸和其他属性
        self.width, self.height = 200, 50
        self.button_color = (0, 208, 0)     # 按钮框的颜色--和外星人一样的颜色
        self.text_color = (255, 0, 0)   # 文本颜色--红色
        self.font = pygame.font.SysFont(None, 48)   # 使用默认字体，字号为48

        # 设置按钮的rect并使其居中
        self.button_rect = pygame.Rect(0, 0, self.width, self.height)
        self.button_rect.center = self.screen_rect.center

        # 按钮的标签只需要创建一次
        self.prep_msg(msg)

    def prep_msg(self, msg):
        """
            将文本渲染成图像并使其在按钮上居中
        """
        self.msg_image = self.font.render(msg, True, self.text_color)    # 将存储在msg中的文本转换成图像
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image_rect.center = self.button_rect.center

    def draw_button(self):
        """绘制一个用颜色填充的按钮，然后绘制文本"""
        self.screen.fill(self.button_color, self.button_rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)