Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648182
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-09-20 14:04:43

from lunar_sprites import *

class PlaneGame(object):

    """兔子接月饼游戏"""

    def __init__(self):

        print("游戏初始化")

        pygame.init()

        # 1.创建游戏的窗口

        self.screen = pygame.display.set_mode(SCREEN_RECT.size)

        # 2.创建游戏的时钟

        self.clock = pygame.time.Clock()

        # 3.调用私有方法,完成精灵和精灵组的创建

        self.__create_sprites()

        # 4.设置定时器事件——每1秒创建一次月饼

        pygame.time.set_timer(CREATE_MOONCAKE_EVENT, 1000)

        # 5.设置定时器事件——倒计时每1秒触发一次

        pygame.time.set_timer(CREATE_I_EVENT, 1000)

        # 6.设置定时器事件——10秒后结束游戏

        pygame.time.set_timer(CREATE_OVER_EVENT, 10000)

        # 7.设置游戏状态,1代表开始,0代表结束,-1代表退出

        self.game_state = 1

    def __create_sprites(self):

        """创建背景精灵和精灵组"""

        # 创建背景的精灵组

        bg1 = Background()

        bg2 = Background(True)

        self.back_group = pygame.sprite.Group(bg1, bg2)

        # 创建月饼的精灵组

        self.mooncake_group = pygame.sprite.Group()

        # 创建兔子的精灵和精灵组

        self.rabbit = Rabbit()

        self.rabbit_group = pygame.sprite.Group(self.rabbit)

        # 创建得分精灵和精灵组

        self.score_sprite = Score()

        self.score_group = pygame.sprite.Group(self.score_sprite)

        # 创建倒计时精灵和精灵组

        self.countdown_sprite = Countdown(10)

        self.countdown_group = pygame.sprite.Group(self.countdown_sprite)

        # 创建菜单精灵和精灵组

        self.menu_sprite = Menu()

        self.menu_group = pygame.sprite.Group(self.menu_sprite)

        # 创建鼠标精灵和精灵组

        self.mouse_sprite = Mouse()

        self.mouse_group = pygame.sprite.Group(self.mouse_sprite)

    def start_game(self):

        print("游戏正式开始。。。")

        pygame.mixer.init()

        pygame.mixer.music.load("./images/plane_background.mp3")

        pygame.mixer.music.play()

        while True:

            if self.game_state == 1:

                """游戏进行中"""

                # 1.设置刷新帧率

                self.clock.tick(FRAME_PER_SEC)

                # 2.事件监听

                self.__event_handler()

                # 3.碰撞检测

                self.__check_collide()

                # 4.更新/绘制精灵组

                self.__update_sprites()

                # 5.更新显示

                pygame.display.update()

            elif self.game_state == 0:

                """游戏某一轮结束"""

                # 1.设置刷新帧率

                self.clock.tick(FRAME_PER_SEC)

                # 2.更新游戏菜单

                self.__event_menu_handler()

                self.__check_menu_collide()

                self.__update_menu()

                # 3.更新显示

                pygame.display.update()

            elif self.game_state == -1:

                """游戏退出"""

                PlaneGame.__game_over()

    def __event_handler(self):

        for event in pygame.event.get():

            # 判断是否退出游戏

            if event.type == pygame.QUIT:

                self.game_state = -1

            elif event.type == CREATE_MOONCAKE_EVENT:

                # 创建月饼精灵

                mooncake = Mooncake()

                # 将月饼精灵添加到月饼精灵组

                self.mooncake_group.add(mooncake)

            elif event.type == CREATE_I_EVENT:

                # 倒计时减1

                self.countdown_sprite.start_time = self.countdown_sprite.start_time - 1

            elif event.type == CREATE_OVER_EVENT:

                self.menu_sprite.display = True

                self.game_state = 0

        # 使用键盘提供的方法获取键盘按键 - 按键元组

        keys_pressed = 跟单网gendan5.compygame.key.get_pressed()

        # 判断元组中对应的按键索引值

        if keys_pressed[pygame.K_RIGHT]:

            self.rabbit.speed = 5

        elif keys_pressed[pygame.K_LEFT]:

            self.rabbit.speed = -5

        else:

            self.rabbit.speed = 0

    def __check_collide(self):

        # 1.兔子接到月饼

        mooncakes = pygame.sprite.spritecollide(self.rabbit, self.mooncake_group, True)

        # 2.判断碰撞列表长度

        if len(mooncakes) > 0:

            # 接到月饼得分加1

            self.score_sprite.score = self.score_sprite.score + 1

    def __update_sprites(self):

        self.back_group.update()

        self.back_group.draw(self.screen)

        self.mooncake_group.update()

        self.mooncake_group.draw(self.screen)

        self.rabbit_group.update()

        self.rabbit_group.draw(self.screen)

        self.score_group.update()

        self.score_group.draw(self.screen)

        self.score_group.update()

        self.score_group.draw(self.screen)

        self.countdown_group.update()

        self.countdown_group.draw(self.screen)

    def __event_menu_handler(self):

        for event in pygame.event.get():

            # 判断是否退出游戏

            if event.type == pygame.QUIT:

                self.game_state = -1

            elif event.type == pygame.MOUSEBUTTONDOWN:

                if event.button == 1:

                    # 点击左键

                    pos = pygame.mouse.get_pos()

                    self.mouse_sprite.rect.top = pos[1]

                    self.mouse_sprite.rect.left = pos[0]

                    self.menu_sprite.update()

    def __check_menu_collide(self):

        click_list = pygame.sprite.spritecollide(self.menu_sprite, self.mouse_group,False)

        if len(click_list) > 0:

            """鼠标击中菜单,重新开始游戏"""

            # 1.设置游戏状态为进行中

            self.game_state = 1

            # 2.重置鼠标位置

            self.mouse_sprite.rect.top=0

            self.mouse_sprite.rect.left=0

            # 3.重新总时间,10s秒后结束

            pygame.time.set_timer(CREATE_OVER_EVENT, 0)

            pygame.time.set_timer(CREATE_OVER_EVENT, 10000)

            # 4.重置分数精灵

            self.score_sprite.score = 0

            # 5.重置倒计时精灵

            self.countdown_sprite.start_time = 10

    def __update_menu(self):

        self.menu_group.update()

        self.menu_group.draw(self.screen)

        self.mouse_group.update()

        self.mouse_group.draw(self.screen)

    @staticmethod

    def __game_over():

        print("游戏结束")

        pygame.quit()

        exit()

if __name__ == "__main__":

    # 创建游戏对象

    game = PlaneGame()

    # 启动游戏

    game.start_game()

阅读(357) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~