瑪麗冒險


文件結構:

 

marie.py

  1 import pygame  # 將pygame庫導入到python程序中
  2 from pygame.locals import *  # 導入pygame中的常量
  3 import sys                   # 導入系統模塊
  4 SCREENWIDTH = 822  # 窗口寬度
  5 SCREENHEIGHT = 199  # 窗口高度
  6 FPS = 30  # 更新畫面的時間
  7 
  8 
  9 # 定義一個移動地圖類
 10 class MyMap():
 11 
 12     def __init__(self, x, y):
 13         # 加載背景圖片
 14         self.bg = pygame.image.load("image/bg.png").convert_alpha()
 15         self.x = x
 16         self.y = y
 17 
 18     def map_rolling(self):
 19         if self.x < -790:  # 小於-790說明地圖已經完全移動完畢
 20             self.x = 800  # 給地圖一個新的坐標點
 21         else:
 22             self.x -= 5  # 5個像素向左移動
 23 
 24     # 更新地圖
 25     def map_update(self):
 26         SCREEN.blit(self.bg, (self.x, self.y))
 27 
 28 # 背景音樂按鈕
 29 class Music_Button():
 30     is_open = True   # 背景樂音的標記
 31     def __init__(self):
 32         self.open_img = pygame.image.load('image/btn_open.png').convert_alpha()
 33         self.close_img = pygame.image.load('image/btn_close.png').convert_alpha()
 34         self.bg_music = pygame.mixer.Sound('audio/bg_music.wav')  # 加載背景音樂
 35     # 判斷鼠標是否在,按鈕的范圍內
 36     def is_select(self):
 37         # 獲取鼠標,的坐標
 38         point_x, point_y = pygame.mouse.get_pos()
 39         w, h = self.open_img.get_size()             # 獲取按鈕圖片的大小
 40         # 判斷鼠標是否在按鈕范圍內
 41         in_x = point_x > 20 and point_x < 20 + w
 42         in_y = point_y > 20 and point_y < 20 + h
 43         return in_x and in_y
 44 
 45 
 46 
 47 
 48 
 49 from itertools import cycle  # 導入迭代工具
 50 
 51 
 52 # 瑪麗類
 53 class Marie():
 54     def __init__(self):
 55         # 初始化小瑪麗矩形
 56         self.rect = pygame.Rect(0, 0, 0, 0)
 57         self.jumpState = False  # 跳躍的狀態
 58         self.jumpHeight = 130  # 跳躍的高度
 59         self.lowest_y = 140  # 最低坐標
 60         self.jumpValue = 0  # 跳躍增變量
 61         # 小瑪麗動圖索引
 62         self.marieIndex = 0
 63         self.marieIndexGen = cycle([0, 1, 2])
 64         # 加載小瑪麗圖片
 65         self.adventure_img = (
 66             pygame.image.load("image/adventure1.png").convert_alpha(),
 67             pygame.image.load("image/adventure2.png").convert_alpha(),
 68             pygame.image.load("image/adventure3.png").convert_alpha(),
 69         )
 70         self.jump_audio = pygame.mixer.Sound('audio/jump.wav')  # 跳音效
 71         self.rect.size = self.adventure_img[0].get_size()
 72         self.x = 50;  # 繪制小瑪麗的X坐標
 73         self.y = self.lowest_y;  # 繪制小瑪麗的Y坐標
 74         self.rect.topleft = (self.x, self.y)
 75 
 76     # 跳狀態
 77     def jump(self):
 78         self.jumpState = True
 79 
 80     # 小瑪麗移動
 81     def move(self):
 82         if self.jumpState:  # 當起跳的時候
 83             if self.rect.y >= self.lowest_y:  # 如果站在地上
 84                 self.jumpValue = -5  # 以5個像素值向上移動
 85             if self.rect.y <= self.lowest_y - self.jumpHeight:  # 小瑪麗到達頂部回落
 86                 self.jumpValue = 5  # 以5個像素值向下移動
 87             self.rect.y += self.jumpValue  # 通過循環改變瑪麗的Y坐標
 88             if self.rect.y >= self.lowest_y:  # 如果小瑪麗回到地面
 89                 self.jumpState = False  # 關閉跳躍狀態
 90 
 91     # 繪制小瑪麗
 92     def draw_marie(self):
 93         # 匹配小瑪麗動圖
 94         marieIndex = next(self.marieIndexGen)
 95         # 繪制小瑪麗
 96         SCREEN.blit(self.adventure_img[marieIndex],
 97                     (self.x, self.rect.y))
 98 
 99 import random  # 隨機數
100 # 障礙物類
101 class Obstacle():
102     score = 1  # 分數
103     move = 5   # 移動距離
104     obstacle_y = 150  # 障礙物y坐標
105     def __init__(self):
106         # 初始化障礙物矩形
107         self.rect = pygame.Rect(0, 0, 0, 0)
108         # 加載障礙物圖片
109         self.missile = pygame.image.load("image/missile.png").convert_alpha()
110         self.pipe = pygame.image.load("image/pipe.png").convert_alpha()
111         # 加載分數圖片
112         self.numbers = (pygame.image.load('image/0.png').convert_alpha(),
113                         pygame.image.load('image/1.png').convert_alpha(),
114                         pygame.image.load('image/2.png').convert_alpha(),
115                         pygame.image.load('image/3.png').convert_alpha(),
116                         pygame.image.load('image/4.png').convert_alpha(),
117                         pygame.image.load('image/5.png').convert_alpha(),
118                         pygame.image.load('image/6.png').convert_alpha(),
119                         pygame.image.load('image/7.png').convert_alpha(),
120                         pygame.image.load('image/8.png').convert_alpha(),
121                         pygame.image.load('image/9.png').convert_alpha())
122         # 加載加分音效
123         self.score_audio = pygame.mixer.Sound('audio/score.wav')  # 加分
124         # 0和1隨機數
125         r = random.randint(0, 1)
126         if r == 0:  # 如果隨機數為0顯示導彈障礙物相反顯示管道
127             self.image = self.missile   # 顯示導彈障礙
128             self.move = 15              # 移動速度加快
129             self.obstacle_y = 100       # 導彈坐標在天上
130         else:
131             self.image = self.pipe      # 顯示管道障礙
132         # 根據障礙物位圖的寬高來設置矩形
133         self.rect.size = self.image.get_size()
134         # 獲取位圖寬高
135         self.width, self.height = self.rect.size
136         # 障礙物繪制坐標
137         self.x = 800
138         self.y = self.obstacle_y
139         self.rect.center = (self.x, self.y)
140 
141     # 障礙物移動
142     def obstacle_move(self):
143         self.rect.x -= self.move
144 
145     # 繪制障礙物
146     def draw_obstacle(self):
147         SCREEN.blit(self.image, (self.rect.x, self.rect.y))
148 
149     # 獲取分數
150     def getScore(self):
151         self.score
152         tmp = self.score;
153         if tmp == 1:
154             self.score_audio.play()  # 播放加分音樂
155         self.score = 0;
156         return tmp;
157 
158     # 顯示分數
159     def showScore(self, score):
160         # 獲取得分數字
161         self.scoreDigits = [int(x) for x in list(str(score))]
162         totalWidth = 0  # 要顯示的所有數字的總寬度
163         for digit in self.scoreDigits:
164             # 獲取積分圖片的寬度
165             totalWidth += self.numbers[digit].get_width()
166         # 分數橫向位置
167         Xoffset = (SCREENWIDTH - (totalWidth+30))
168         for digit in self.scoreDigits:
169             # 繪制分數
170             SCREEN.blit(self.numbers[digit], (Xoffset, SCREENHEIGHT * 0.1))
171             # 隨着數字增加改變位置
172             Xoffset += self.numbers[digit].get_width()
173 
174 # 游戲結束的方法
175 def game_over():
176     bump_audio = pygame.mixer.Sound('audio/bump.wav')  # 撞擊
177     bump_audio.play()  # 播放撞擊音效
178     # 獲取窗體寬、高
179     screen_w = pygame.display.Info().current_w
180     screen_h = pygame.display.Info().current_h
181     # 加載游戲結束的圖片
182     over_img = pygame.image.load('image/gameover.png').convert_alpha()
183     # 將游戲結束的圖片繪制在窗體的中間位置
184     SCREEN.blit(over_img, ((screen_w - over_img.get_width()) / 2,
185                                        (screen_h - over_img.get_height()) / 2))
186 
187 
188 def mainGame():
189     score = 0  # 得分
190     over = False  # 游戲結束標記
191     global SCREEN, FPSCLOCK
192     pygame.init()  # 經過初始化以后我們就可以盡情地使用pygame了。
193 
194     # 使用Pygame時鍾之前,必須先創建Clock對象的一個實例,
195     # 控制每個循環多長時間運行一次。
196     FPSCLOCK = pygame.time.Clock()
197     SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))  # 通常來說我們需要先創建一個窗口,方便我們與程序的交互。
198     pygame.display.set_caption('瑪麗冒險')  # 設置窗口標題
199 
200     # 創建地圖對象
201     bg1 = MyMap(0, 0)
202     bg2 = MyMap(800, 0)
203 
204     # 創建小瑪麗對象
205     marie = Marie()
206 
207     addObstacleTimer = 0  # 添加障礙物的時間
208     list = []  # 障礙物對象列表
209 
210     music_button = Music_Button()     # 創建背景音樂按鈕對象
211     btn_img  = music_button.open_img  # 設置背景音樂按鈕的默認圖片
212     music_button.bg_music.play(-1)    # 循環播放背景音樂
213 
214     while True:
215         # 獲取單擊事件
216         for event in pygame.event.get():
217 
218             if event.type == pygame.MOUSEBUTTONUP:  # 判斷鼠標事件
219                 if music_button.is_select():        # 判斷鼠標是否在靜音按鈕范圍
220                     if music_button.is_open:        # 判斷背景音樂狀態
221                         btn_img = music_button.close_img # 單擊后顯示關閉狀態的圖片
222                         music_button.is_open = False    # 關閉背景音樂狀態
223                         music_button.bg_music.stop()    # 停止背景音樂的播放
224                     else:
225                         btn_img = music_button.open_img
226                         music_button.is_open = True
227                         music_button.bg_music.play(-1)
228             # 如果單擊了關閉窗口就將窗口關閉
229             if event.type == QUIT:
230                 pygame.quit()  # 退出窗口
231                 sys.exit()  # 關閉窗口
232 
233             # 單擊鍵盤空格鍵,開啟跳的狀態
234             if event.type == KEYDOWN and event.key == K_SPACE:
235                 if marie.rect.y >= marie.lowest_y:  # 如果小瑪麗在地面上
236                     marie.jump_audio.play()  # 播放小瑪麗跳躍音效
237                     marie.jump()  # 開啟小瑪麗跳的狀態
238 
239                 if over == True:  # 判斷游戲結束的開關是否開啟
240                     mainGame()  # 如果開啟將調用mainGame方法重新啟動游戲
241 
242 
243 
244 
245         if over == False:
246             # 繪制地圖起到更新地圖的作用
247             bg1.map_update()
248             # 地圖移動
249             bg1.map_rolling()
250             bg2.map_update()
251             bg2.map_rolling()
252 
253             # 小瑪麗移動
254             marie.move()
255             # 繪制小瑪麗
256             marie.draw_marie()
257 
258             # 計算障礙物間隔時間
259             if addObstacleTimer >= 1300:
260                 r = random.randint(0, 100)
261                 if r > 40:
262                     # 創建障礙物對象
263                     obstacle = Obstacle()
264                     # 將障礙物對象添加到列表中
265                     list.append(obstacle)
266                 # 重置添加障礙物時間
267                 addObstacleTimer = 0
268 
269             # 循環遍歷障礙物
270             for i in range(len(list)):
271                 # 障礙物移動
272                 list[i].obstacle_move()
273                 # 繪制障礙物
274                 list[i].draw_obstacle()
275 
276                 # 判斷小瑪麗與障礙物是否碰撞
277                 if pygame.sprite.collide_rect(marie, list[i]):
278                     over = True  # 碰撞后開啟結束開關
279                     game_over()  # 調用游戲結束的方法
280                     music_button.bg_music.stop()
281                 else:
282                     # 判斷小瑪麗是否躍過了障礙物
283                     if (list[i].rect.x + list[i].rect.width) < marie.rect.x:
284                         # 加分
285                         score += list[i].getScore()
286                 # 顯示分數
287                 list[i].showScore(score)
288 
289         addObstacleTimer += 20  # 增加障礙物時間
290         SCREEN.blit(btn_img, (20, 20)) # 繪制背景音樂按鈕
291         pygame.display.update()  # 更新整個窗口
292         FPSCLOCK.tick(FPS)  # 循環應該多長時間運行一次
293 
294 
295 if __name__ == '__main__':
296     mainGame()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM