文 | 野客python
來源:Python 技術「ID: pythonall」微信
2013 年微信 5.0 版本內置了一款經典小遊戲-灰黑色版飛機大戰,被稱爲微信經典飛機大戰,在這個版本中微信甚至將歡迎設置了成了這款小遊戲,用戶首次登錄將會直接進入飛機大戰小遊戲,遊戲一經推出也是火爆異常,當時還出現了許多經典語錄。app
咱們要實現飛機大戰這款小遊戲,主要須要用到的是第三方模塊 pygame,安裝使用 pip install pygame 便可。dom
飛機大戰這款小遊戲中角色相對比較簡單,主要包括三部分:玩家、敵人和子彈。ide
咱們先來定義一個子彈類,類中主要包括子彈樣式、移動速度等,代碼實現以下:ui
# 子彈類 class Bullet(pygame.sprite.Sprite): def __init__(self, img, pos): pygame.sprite.Sprite.__init__(self) self.image = img # 設置圖片的區域 self.rect = self.image.get_rect() self.rect.midbottom = pos self.speed = 10 def move(self): self.rect.top -= self.speed
接下來咱們來定義敵人類,類中主要包括敵機樣式、移動速度、出現位置等,代碼實現以下所示:3d
# 敵人類 class Enemy(pygame.sprite.Sprite): def __init__(self, img, explosion_img, pos): pygame.sprite.Sprite.__init__(self) self.image = img self.rect = self.image.get_rect() self.rect.topleft = pos self.explosion_img = explosion_img self.speed = 2 # 設置擊毀序列 self.explosion_index = 0 def move(self): # 敵人的子彈只能一直向下 self.rect.top += self.speed
咱們再接着來定義玩家類,類中主要包括飛機初始位置、移動方法、是否被敵機擊中等,代碼實現以下所示:code
# 玩家類 class Player(pygame.sprite.Sprite): def __init__(self, img, rect, pos): pygame.sprite.Sprite.__init__(self) self.image = [] # 將飛機圖片部分分隔 for i in range(len(rect)): self.image.append(img.subsurface(rect[i]).convert_alpha()) # 獲取飛機的區域 self.rect = rect[0] self.rect.topleft = pos self.speed = 8 # 生成精靈組實例 self.bullets = pygame.sprite.Group() self.img_index = 0 # 判斷飛機是否被打中 self.is_hit = False def shoot(self, img): bullet = Bullet(img, self.rect.midtop) # 添加子彈實例到玩家的子彈組 self.bullets.add(bullet) def moveUp(self): # 當遇到頂部時,設置上頂部爲0 if self.rect.top <= 0: self.rect.top = 0 else: self.rect.top -= self.speed def moveDown(self): # 當遇到底部時,設置一直爲常值 if self.rect.top >= SCREEN_HEIGHT - self.rect.height: self.rect.top = SCREEN_HEIGHT - self.rect.height else: self.rect.top += self.speed def moveLeft(self): # 當遇到左邊時,一直停靠在左邊 if self.rect.left <= 0: self.rect.left = 0 else: self.rect.left -= self.speed def moveRight(self): # 當遇到右邊時, 停靠右邊 if self.rect.left >= SCREEN_WIDTH - self.rect.width: self.rect.left = SCREEN_WIDTH - self.rect.width else: self.rect.left += self.speed
如今咱們已經定義好遊戲的角色 model 了,接下來咱們能夠先對角色 model、圖片素材及一些常量等進行一些初始化操做,代碼實現以下所示:blog
# 設置屏幕的寬度 SCREEN_WIDTH = 450 # 設置屏幕的高度 SCREEN_HEIGHT = 600 # 初始化窗口 pygame.init() # 設置窗口標題 pygame.display.set_caption("飛機大戰") # 設置屏幕大小 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) # 隱藏光標 pygame.mouse.set_visible(False) # 設置背景 bg = pygame.image.load("resources/image/bg.png") # 設置遊戲結束的圖片 bg_game_over = pygame.image.load("resources/image/bg_game_over.png") # 加載飛機資源圖片 img_plane = pygame.image.load("resources/image/shoot.png") img_start = pygame.image.load("resources/image/start.png") img_pause = pygame.image.load("resources/image/pause.png") img_icon = pygame.image.load("resources/image/plane.png").convert_alpha() # 順便設置窗口 pygame.display.set_icon(img_icon) # 初始化飛機區域 player_rect = [] player_rect.append(pygame.Rect(0, 99, 102, 126)) player_rect.append(pygame.Rect(165, 360, 102, 126)) # 玩家爆炸圖片 player_rect.append(pygame.Rect(165, 234, 102, 126)) player_rect.append(pygame.Rect(330, 624, 102, 126)) player_rect.append(pygame.Rect(330, 498, 102, 126)) player_rect.append(pygame.Rect(432, 624, 102, 126)) # 初始化位置 player_pos = [200, 450] # 生成玩家類 player = Player(img_plane, player_rect, player_pos) # 設置子彈框 bullet_rect = pygame.Rect(1004, 987, 9, 21) # 加載子彈圖片 bullet_img = img_plane.subsurface(bullet_rect) # 設置敵人框 enemy_rect = pygame.Rect(534, 612, 57, 43) # 設置敵人圖片 enemy_img = img_plane.subsurface(enemy_rect) # 設置敵人被擊圖片 enemy_explosion_imgs = [] enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(267, 347, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(873, 697, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(267, 296, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(930, 697, 57, 43))) # 設置敵機精靈組 enemies = pygame.sprite.Group() # 設置敵機被擊精靈組 enemies_explosion = pygame.sprite.Group() # 設置射擊頻率 shoot_frequency = 0 # 設置敵機頻率 enemy_frequency = 0 # 設置玩家被擊的圖片順序 player_explosion_index = 16 score = 0 running = True is_pause = False is_game_over = False clock = pygame.time.Clock()
最後,咱們來看一下游戲運行中最重要的一部分-遊戲的運行邏輯,主要包括:敵機的生成運行、子彈的生成運行、生成分數、遊戲狀態的處理(暫停、重置、結束)等,具體代碼實現以下所示:遊戲
# 開始遊戲循環 while running: # 設置遊戲幀率爲 60 clock.tick(60) if not is_pause and not is_game_over: if not player.is_hit: # 設置連續射擊,由於每秒 60 幀,15/60=0.25 秒發一次子彈 if shoot_frequency % 15 == 0: player.shoot(bullet_img) shoot_frequency += 1 # 當設置的射擊頻率大於 15,置零 if shoot_frequency >= 15: shoot_frequency = 0 # 控制生成敵機的頻率 if enemy_frequency % 50 == 0: # 設置敵機的出現的位置 enemy_pos = [random.randint(0, SCREEN_WIDTH - enemy_rect.width), 0] enemy = Enemy(enemy_img, enemy_explosion_imgs, enemy_pos) enemies.add(enemy) enemy_frequency += 1 if enemy_frequency >= 100: enemy_frequency = 0 # 控制子彈的顯示運行 for bullet in player.bullets: bullet.move() if bullet.rect.bottom < 0: player.bullets.remove(bullet) # 控制敵機的運行 for enemy in enemies: enemy.move() # 判斷敵機是否與玩家飛機碰撞 if pygame.sprite.collide_circle(enemy, player): enemies_explosion.add(enemy) enemies.remove(enemy) player.is_hit = True # 設置玩家的飛機被毀 is_game_over = True # 判斷敵機是否在界面 if enemy.rect.top < 0: enemies.remove(enemy) # 設置敵機與玩家的飛機子彈相碰時,返回被擊的敵機實例 enemy_explosion = pygame.sprite.groupcollide(enemies, player.bullets, 1, 1) for enemy in enemy_explosion: enemies_explosion.add(enemy) # 繪製屏幕 screen.fill(0) # 加入背景圖片 screen.blit(bg, (0, 0)) # 添加玩家飛機圖片到屏幕 if not player.is_hit: screen.blit(player.image[int(player.img_index)], player.rect) player.img_index = shoot_frequency / 8 else: if player_explosion_index > 47: is_game_over = True else: player.img_index = player_explosion_index / 8 screen.blit(player.image[int(player.img_index)], player.rect) player_explosion_index += 1 # 敵機被子彈擊中的效果顯示 for enemy in enemies_explosion: if enemy.explosion_index == 0: pass if enemy.explosion_index > 7: enemies_explosion.remove(enemy) score += 100 continue # 敵機被擊時顯示圖片 screen.blit(enemy.explosion_img[int(enemy.explosion_index / 2)], enemy.rect) enemy.explosion_index += 1 # 顯示子彈 player.bullets.draw(screen) # 顯示敵機 enemies.draw(screen) # 分數的顯示效果 score_font = pygame.font.Font(None, 36) score_text = score_font.render(str(score), True, (128, 128, 128)) # 設置文字框 text_rect = score_text.get_rect() # 放置文字的位置 text_rect.topleft = [20, 10] # 顯示出分數 screen.blit(score_text, text_rect) left, middle, right = pygame.mouse.get_pressed() # 暫停遊戲 if right == True and not is_game_over: is_pause = True if left == True: # 重置遊戲 if is_game_over: is_game_over = False player_rect = [] player_rect.append(pygame.Rect(0, 99, 102, 126)) player_rect.append(pygame.Rect(165, 360, 102, 126)) player_rect.append(pygame.Rect(165, 234, 102, 126)) player_rect.append(pygame.Rect(330, 624, 102, 126)) player_rect.append(pygame.Rect(330, 498, 102, 126)) player_rect.append(pygame.Rect(432, 624, 102, 126)) player = Player(img_plane, player_rect, player_pos) bullet_rect = pygame.Rect(1004, 987, 9, 21) bullet_img = img_plane.subsurface(bullet_rect) enemy_rect = pygame.Rect(534, 612, 57, 43) enemy_img = img_plane.subsurface(enemy_rect) enemy_explosion_imgs = [] enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(267, 347, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(873, 697, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(267, 296, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(930, 697, 57, 43))) enemies = pygame.sprite.Group() enemies_explosion = pygame.sprite.Group() score = 0 shoot_frequency = 0 enemy_frequency = 0 player_explosion_index = 16 # 繼續遊戲 if is_pause: is_pause = False # 遊戲結束 if is_game_over: font = pygame.font.SysFont("微軟雅黑", 48) text = font.render("Score: " + str(score), True, (255, 0, 0)) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.centery = screen.get_rect().centery + 70 # 顯示遊戲結束畫面 screen.blit(bg_game_over, (0, 0)) # 顯示分數 screen.blit(text, text_rect) font = pygame.font.SysFont("微軟雅黑", 40) text = font.render("Press Left Mouse to Restart", True, (255, 0, 0)) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.centery = screen.get_rect().centery + 150 screen.blit(text, text_rect) # 刷新屏幕 pygame.display.update() # 處理遊戲退出 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if not is_pause and not is_game_over: key = pygame.key.get_pressed() if key[K_w] or key[K_UP]: player.moveUp() if key[K_s] or key[K_DOWN]: player.moveDown() if key[K_a] or key[K_LEFT]: player.moveLeft() if key[K_d] or key[K_RIGHT]: player.moveRight()
咱們來看一下實現效果:
本文咱們使用 Python 實現了微信版飛機大戰的基本功能,你們有興趣的能夠獲取源碼本身玩了,若是你對遊戲哪一個地方實現的不滿意,還能夠本身進行修改。