定义击中的函数:
通过欧式距离算出距离,并判断子弹到飞机一定范围内则判定为击中敌人
# 新增 函数表示两者距离
def distance(bx,by,ex,ey):a = bx-exb = 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)# 定义变量表示敌人运动速度。注意:将速度改为随机数时由于为浮点型用.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 + 48self.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):# 每进行一次循环添加一个Enemy到列表中enemies.append(Enemy())# 新增 函数表示两者距离
def distance(bx,by,ex,ey):a = bx-exb = 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 += 20if e.x < 0:e.step *= -1e.y += 20# 控制边界if e.y > 572:e.y = 572if 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.5elif event.key == pygame.K_LEFT:playerStep = -0.5# 判断按下空格键elif event.key == pygame.K_SPACE:# 创建一颗子弹bullets.append(Bullet())# KEYUP 判断键盘是否抬起if event.type == pygame.KEYUP:# 抬起键盘时将移动距离改为 0playerStep = 0# 玩家左右移动playerX += playerStep# 控制玩家移动的边界if playerX > 378:playerX = 378if playerX < 0:playerX = 0if playerY > 572:playerY = 572if playerY < 0:playerY = 0# 界面更新pygame.display.update()
运行效果如下: