pygame小游戲打飛機_7射中檢測
定義擊中的函數:
計算子彈與飛機之間的歐式距離,若該距離小于預設碰撞距離,則判定為擊中敵人。?
# 新增 歐式距離計算函數
def distance(bx,by,ex,ey):
a = bx-ex
b = by-ey
# 新增 返回兩者距離值
return (a**2+b**2)**0.5
# 新增 定義擊中的函數
def hit(self):
# 新增 判斷是否射中敵人
for e in enemies:
if distance(self.x,self.y,e.x,e.y)<30:
若擊中成立:
判定為擊中后將敵人回復至原位,刪除子彈
# 新增 定義重置敵人位置的函數
def reset(self):
# 新增 重復顯示敵人的操作
self.x = random.randint(100, 360)
self.y = random.randint(50, 60)
# 新增 定義擊中的函數
def hit(self):
# 新增 判斷是否射中敵人
for e in enemies:
if distance(self.x,self.y,e.x,e.y)<30:
# 新增 當擊中敵人則移除子彈
bullets.remove(self)
# 新增 當擊中敵人則調用reset函數將敵人位置重置
e.reset()
完整代碼:
# 引用 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
# 定義敵人的類,其中包括圖片(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):
# 新增 判斷是否射中敵人
for e in enemies:
if distance(self.x,self.y,e.x,e.y)<30:
# 新增 當擊中敵人則移除子彈
bullets.remove(self)
# 新增 當擊中敵人則調用reset函數將敵人位置重置
e.reset()
# 保存現有的子彈
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():
# 循環控制所有敵人的顯示、移動、邊界反彈
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 += 20
if e.x < 0:
e.step *= -1
e.y += 20
# 控制邊界
if e.y > 572:
e.y = 572
if e.y < 0:
e.y = 0
# 定義顯示子彈的函數
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))
# 繪制玩家
screen.blit(playerImg, (playerX, playerY))
# 調用顯示敵人函數
show_enemy()
# 顯示子彈
show_bullet()
# 獲取游戲事件隊列中的所有事件(涉及到玩家的各種交互,如鼠標點擊、鍵盤操作、窗口事件等)
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()
運行效果如下:

浙公網安備 33010602011771號