pygame小游戲打飛機_8音效,積分,結束游戲
音效:
引入游戲背景音效和敵人爆炸的音效,并且使背景音樂單曲循環
# 新增 引入音效
pygame.mixer.music.load("game_music.ogg")
# 新增 背景音單曲循環
pygame.mixer.music.play(-1)
# 新增 添加敵人爆炸的音效
exp_sound = pygame.mixer.Sound("exp.wav")
當敵人爆炸時播放敵人爆炸音效
# 定義擊中的函數
def hit(self):
# 判斷是否射中敵人
for e in enemies:
if distance(self.x,self.y,e.x,e.y)<30:
# 當擊中敵人則移除子彈
bullets.remove(self)
# 當擊中敵人則調用reset函數將敵人位置重置
e.reset()
# 新增 播放爆炸音效
exp_sound.play()
積分:
創建 score 變量用于記錄得分,創建顯示得分的函數
# 新增 定義分數變量
score = 0
# 新增 創建字體,使用 pygame 自帶的字體(freesansbold.ttf 是字體名稱,ttf 是字體格式,32 是字體大小)
font = pygame.font.Font("freesansbold.ttf",32)
# 新增 顯示分數的函數
def show_score():
# 新增 輸入的文字
text=f"Score:{score}"
# 新增 創建顯示文字的變量 True是開啟抗鋸齒
score_render=font.render(text ,True,(0,225,0) )
# 新增 顯示文字
screen.blit(score_render,(10,10))
# 定義擊中的函數
def hit(self):
# 新增 將score設置為全局變量
global score
# 判斷是否射中敵人
for e in enemies:
if distance(self.x,self.y,e.x,e.y)<30:
# 當擊中敵人則移除子彈
bullets.remove(self)
# 當擊中敵人則調用reset函數將敵人位置重置
e.reset()
# 新增 播放爆炸音效
exp_sound.play()
# 新增 當擊中時加分
score += 1
print (score)
游戲結束:
與積分同理,用變量控制游戲結束的狀態,用函數顯示 game over 的提示文字
當敵人和玩家在同一水平線上則游戲結束并清空所有敵人
# 新增 游戲結束
is_over = False
over_font = pygame.font.Font("freesansbold.ttf",64)
# 新增 表示游戲結束的函數
def check_is_over():
# 新增 將is_over設置為全局變量
global is_over
# 新增 進行判斷is_over是否為True
if is_over == True:
# 新增 輸入的文字
text="game over"
# 新增 創建顯示文字的變量 True是開啟抗鋸齒
render=font.render(text ,True,(225,0,0) )
# 新增 顯示文字
screen.blit(render,(200,300))
# 定義顯示敵人的函數
def show_enemy():
# 新增 將is_over設置為全局變量
global is_over
# 循環控制所有敵人的顯示、移動、邊界反彈
for e in enemies:
# 使敵人出現在(enemyX, enemyY)的位置
screen.blit(e.img, (e.x, e.y))
# 使敵人飛機左右移動
e.x += e.step
# 控制敵人移動邊界,當敵人碰到左右邊界時反彈,當敵人運動到上下邊界時停止
if e.x > 378:
e.step *= -1
# 當碰到左右邊界時下沉
e.y += 60
if e.x < 0:
e.step *= -1
e.y += 60
# 控制邊界
if e.y > 572:
e.y = 572
if e.y < 0:
e.y = 0
# 新增 當敵人和玩家在同一高度時清空所有敵人并將is_over值變為True
if e.y > 370:
is_over=True
enemies.clear()
完整代碼:
# 引用 pygame
import pygame
# 引用隨機模塊
import random
# 使程序初始化
pygame.init()
# 設置游戲窗口大小
screen = pygame.display.set_mode((480, 700))
# 設置標題
pygame.display.set_caption('打飛機')
# 設置游戲圖標
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# 引入背景圖片
bgImg = pygame.image.load('background.png')
# 引入玩家圖片
playerImg = pygame.image.load('player.png')
# 定義玩家初始位置
playerX = 225
playerY = 450
# 定義變量表示玩家運動速度
playerStep = 0
# 變量表示敵人數量
number_of_enemies = 6
# 新增 引入背景音效
pygame.mixer.music.load("game_music.ogg")
# 新增 背景音樂單曲循環
pygame.mixer.music.play(-1)
# 新增 添加敵人爆炸的音效
exp_sound = pygame.mixer.Sound("exp.wav")
# 新增 定義分數變量
score = 0
# 新增 創建字體,使用 pygame 自帶的字體,(freesansbold.ttf 是字體名稱,ttf 是字體格式,32 是字體大小)
font = pygame.font.Font("freesansbold.ttf", 32)
# 新增 游戲結束
is_over = False
over_font = pygame.font.Font("freesansbold.ttf", 64)
# 新增 顯示分數的函數
def show_score():
# 新增 輸入的文字
text = f"Score:{score}"
# 新增 創建顯示文字的變量 True是開啟抗鋸齒
score_render = font.render(text, True, (0, 225, 0))
# 新增 顯示文字
screen.blit(score_render, (10, 10))
# 新增 表示游戲結束的函數
def check_is_over():
# 新增 將is_over設置為全局變量
global is_over
# 新增 進行判斷is_over是否為True
if is_over == True:
# 新增 輸入的文字
text = "game over"
# 新增 創建顯示文字的變量 True是開啟抗鋸齒
render = font.render(text, True, (225, 0, 0))
# 新增 顯示文字
screen.blit(render, (200, 300))
# 定義敵人的類,其中包括圖片(self.img)初始位置( self.x,self.y )運動速度(self.step)
class Enemy:
def __init__(self):
self.img = pygame.image.load('enemy.png')
# 通過 random 函數生成隨機位置坐標
self.x = random.randint(100, 360)
self.y = random.randint(50, 100)
# 定義變量表示敵人運動速度。注意:由于速度隨機數為浮點型,使用 random.uniform 函數生成
self.step = random.uniform(0.05, 0.3)
# 定義重置敵人位置的函數
def reset(self):
# 重復顯示敵人的操作
self.x = random.randint(100, 360)
self.y = random.randint(50, 60)
# 定義子彈的類,其中包括圖片(self.img)初始位置( self.x,self.y )運動速度(self.step)
class Bullet:
def __init__(self):
self.img = pygame.image.load('bullet.png')
# 將子彈顯示在玩家上方
self.x = playerX + 48
self.y = playerY + 5
# 定義變量表示子彈運動速度。
self.step = 1
# 定義擊中的函數
def hit(self):
# 新增 將score設置為全局變量
global score
# 判斷是否射中敵人
for e in enemies:
if distance(self.x, self.y, e.x, e.y) < 30:
# 當擊中敵人則移除子彈
bullets.remove(self)
# 當擊中敵人則調用reset函數將敵人位置重置
e.reset()
# 新增 播放爆炸音效
exp_sound.play()
# 新增 當擊中時加分
score += 1
print(score)
# 保存現有的子彈
bullets = []
# 創建列表保存創建的 number_of_enemies 個敵人
enemies = []
for i in range(number_of_enemies):
# 每循環一次創建一個敵人保存到列表中
enemies.append(Enemy())
# 歐式距離計算函數
def distance(bx, by, ex, ey):
a = bx - ex
b = by - ey
# 返回兩者距離值
return (a ** 2 + b ** 2) ** 0.5
# 定義顯示敵人的函數
def show_enemy():
# 新增 將is_over設置為全局變量
global is_over
# 循環控制所有敵人的顯示、移動、邊界反彈
for e in enemies:
# 使敵人出現在(enemyX, enemyY)的位置
screen.blit(e.img, (e.x, e.y))
# 使敵人飛機左右移動
e.x += e.step
# 控制敵人移動邊界,當敵人碰到左右邊界時反彈,當敵人運動到上下邊界時停止
if e.x > 378:
e.step *= -1
# 當碰到左右邊界時下沉
e.y += 60
if e.x < 0:
e.step *= -1
e.y += 60
# 控制邊界
if e.y > 572:
e.y = 572
if e.y < 0:
e.y = 0
# 新增 當敵人和玩家在同一高度時清空所有敵人并將is_over值變為True
if e.y > 370:
is_over = True
enemies.clear()
# 定義顯示子彈的函數
def show_bullet():
# 循環控制所有子彈的顯示、移動和刪除
for b in bullets:
# 使子彈出現在(b.x,b.y)的位置
screen.blit(b.img, (b.x, b.y))
# 調用hit函數判斷子彈是否擊中敵人
b.hit()
# 使子彈向上移動
b.y -= b.step
# 判斷子彈是否出界,是則移除
if b.y < 0:
bullets.remove(b)
running = True
# 進行循環 游戲主循環
while running:
# 繪制背景
screen.blit(bgImg, (0, 0))
# 新增 顯示分數
show_score()
# 繪制玩家
screen.blit(playerImg, (playerX, playerY))
# 調用顯示敵人函數
show_enemy()
# 顯示子彈
show_bullet()
# 新增 檢測游戲是否結束
check_is_over()
# 獲取游戲事件隊列中的所有事件(涉及到玩家的各種交互,如鼠標點擊、鍵盤操作、窗口事件等)
for event in pygame.event.get():
# 如果事件是QUIT事件,如點擊窗口的關閉按鈕,則退出循環
if event.type == pygame.QUIT:
# 退出循環
running = False
# KEYDOWN 判斷鍵盤是否按下
if event.type == pygame.KEYDOWN:
# 判斷按下左右鍵進行移動賦值
if event.key == pygame.K_RIGHT:
playerStep = 0.5
elif event.key == pygame.K_LEFT:
playerStep = -0.5
# 判斷按下空格鍵
elif event.key == pygame.K_SPACE:
# 創建一顆子彈
bullets.append(Bullet())
# KEYUP 判斷鍵盤是否抬起
if event.type == pygame.KEYUP:
# 抬起鍵盤時將移動距離改為 0
playerStep = 0
# 玩家左右移動
playerX += playerStep
# 控制玩家移動的邊界
if playerX > 378:
playerX = 378
if playerX < 0:
playerX = 0
if playerY > 572:
playerY = 572
if playerY < 0:
playerY = 0
# 界面更新
pygame.display.update()
運行效果如下:

用 pygame 實現打飛機小游戲的學習筆記到此結束。
相關學習資料:1小時開發飛機大戰游戲
浙公網安備 33010602011771號