又是一年中秋至|用Python Pygame製作兔子接月餅遊戲

專注的阿熊發表於2022-09-20

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.com pygame.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(

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2915431/,如需轉載,請註明出處,否則將追究法律責任。

相關文章