經典遊戲飛機大戰如何編碼實現?教你用 Python 寫一個!
阿新 • • 發佈:2020-11-17
當年微信 5.0 釋出時,首頁被設定成了一款新推出的小遊戲,它就是微信版飛機大戰,遊戲一經推出便是火爆異常,鉛筆畫風格的遊戲介面也受到了很多人的喜歡。
需要python資料的可回覆01或加群獲取~
最近重溫了一下這款小遊戲,儘管時隔多年,但無論是遊戲的畫質還是風格,時至今日依然都不過時。本文我們使用 Python 來實現一下這款小遊戲,遊戲的實現主要用到第三方模組 pygame,安裝使用pip install pygame
即可。
環境
作業系統:Windows
Python 版本:3.6
涉及模組:pygame、sys、random
實現
飛機大戰的構成相對比較簡單,主要包括:主介面、玩家、敵人、子彈、計分板等,下面來看一下具體實現。
首先我們來繪製一個主介面,主要實現程式碼如下所示:
# 設定螢幕的寬度 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") # 繪製螢幕 screen.fill(0) # 加入背景圖片 screen.blit(bg, (0, 0)) # 設定遊戲結束的圖片 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_pos = [200, 450]
看一下效果:
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
看一下玩家的飛機樣式:
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
看一下子彈的樣式:
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
最後,我們來定義一下游戲執行的相應邏輯,比如:擊中敵機、玩家與敵機碰撞、生成分數等,主要實現程式碼如下所示:
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()
我們來看一下最終實現效果: