pygameのゲームの練習4

練習へのエントリから、プログラミング@Python Pythonプロジェクトを行使する

九、再生ボタンを追加

まず、Buttonクラスを作成します

ゲーム非アクティブの始まりをしてみましょう

# game_stats.py
# --snip--

self.game_active = False

# def reset_stats(self):
# --snip--

直方体ボタンクラスは、長方形の上部を中心に、文字列レンダリング画像として「再生」、タグ付けされています

# button.py
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, 0, 0)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont(None, 48) # 指定字体

        # 创建按钮的rect对象,并使其居中
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        # 按钮的标签只需创建一次,即将字符串msg渲染为图像
        self.prep_msg(msg)

    def prep_msg(self, msg):
        """将msg渲染为图像,并使其在居中"""
        self.msg_image = self.font.render(msg, True, self.text_color,
                                          self.button_color)  # 渲染为图像
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image_rect.center = self.rect.center  # 使文字形成的图像居中

    def draw_button(self):
        # 绘制一个用颜色填充的按钮,再绘制文本图像,使其在上方
        self.screen.fill(self.button_color, self.rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)

第二に、ボタンを描きます

インスタンスalien_invasion.pyを作成し、()をup_screen変更し、ゲームが非アクティブ表示ボタンです

# alien_invasion.py
# --snip--
    # 创建play按钮
    play_button = Button(ai_settings, screen, "Play")

    # 开始游戏主循环
    while True:
    # --snip--
        # 每次循环重绘窗口并更新屏幕
        gf.update_screen(ai_settings, screen, stats, ship, passenger_group, bullet_group,play_button)
# game_functions.py
# --snip--

    # 如果游戏处于非活动状态,就绘制Play按钮
    if not stats.game_active:
        play_button.draw_button()

# --snip--

第三に、ボタンをクリックしてください

プレイヤーがボタンをクリックすると、ゲームは、統計をリセットする次のゲームが繰り返されていないことを確認する必要があります。そして、ときにだけ偽、ゲームに有効であることが活性化状態をクリックしてください。

# game_functions.py
#   for event in pygame.event.get():
#       if event.type == pygame.QUIT:
#          sys.exit()

#    --snip--
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(ai_settings, screen, stats, play_button, ship,
                              passenger_group, bullet_group, mouse_x, mouse_y)


def check_play_button(ai_settings, screen, stats, play_button, ship,
                      passenger_group, bullet_group, mouse_x, mouse_y):
    """在玩家单击Play按钮时开始游戏"""
    button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
    if button_clicked and not stats.game_active:  # 检查鼠标点击位置是否在play范围内
        # 隐藏光标
        pygame.mouse.set_visible(False)
        # 重置游戏统计信息
        stats.reset_stats()
        stats.game_active = True

        # 清空外星人列表和子弹列表
        passenger_group.empty()
        bullet_group.empty()

        # 创建一群新的外星人,并在飞船居中
        create_fleet(ai_settings, screen, ship, passenger_group)
        ship.center_ship()

注、変更alien_invasion.pyに、とパラメータ、game_functions.py同期をcheck_events。

おすすめ

転載: www.cnblogs.com/gg12138/p/11364555.html