主要对已经完成的项目利用面向对象进行封装,其作用是为保证关键数据的安全性及对外部隐藏内部细节隔离复杂度;封装可以更好地展示游戏项目,通过隐藏对象的内部细节、定义清晰的接口,提高了代码的安全性、可维护性和复用性,使得程序更加健壮和易于开发与维护。
import random
import pygame
import time
import os
def get_pic(path):
# 拼接图片路径
pic_path = os.path.join("D:\\python\\pythonProject1\\飞机大战\\img",path)
#返回pygame对象
return pygame.image.load(pic_path)
clock = pygame.time.Clock()
#英雄精灵子弹的类
class HeroBullet():
#:param x: x坐标 :param y: y坐标 :param screen: 窗口对象
def __init__(self,x,y,screen):
self.x = x
self.y = y
self.screen = screen
self.pic = get_pic("bullet1.png")
#用来画子弹
def draw(self):
self.screen.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y -=5
#定义敌机精灵子弹类
class EnemyBullet():
def __init__(self,x,y,screen):
self.x = x
self.y = y
self.screen = screen
self.pic = get_pic("bullet2.png")
#画子弹
def draw(self):
self.screen.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y +=5
#英雄战机类
class HeroPlane():
def __init__(self,x,y,screen):
self.x = x
self.y = y
self.screen = screen
self.normal_image_list = ["me1.png","me2.png"]
self.normal_image_index = 0
self.bomb_image_list = ["me_destroy_1.png",
"me_destroy_2.png",
"me_destroy_3.png",
"me_destroy_4.png"]
self.bomb_image_index = 0
#碰撞检测
self.isBomb = False
self.bullet_list = []
def draw(self):
#如果没有爆炸
if self.isBomb == False:
pic = get_pic(self.normal_image_list[self.normal_image_index])
#绘制英雄战机
self.screen.blit(pic,(self.x,self.y))
#利用取余运算进行循环
self.normal_image_index = (self.bomb_image_index+1)%len(self.normal_image_list)
else:
#当敌机爆炸图片的下表和图片总数相同时,说明爆炸图片已经绘制结束
if self.bomb_image_index ==len(self.bomb_image_list):
time.sleep(0.2)
exit()
#加载英雄爆炸图片
enemy_bomb_img = get_pic(self.bomb_image_list[self.bomb_image_index])
#绘制敌机爆炸图片
self.screen.blit(enemy_bomb_img,(self.x,self.y))
self.bomb_image_index +=1
time.sleep(0.2)
def deal_event(self,event_list):
for event in event_list:
if event.type == pygame.QUIT:
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.x = self.x - 5 if self.x >= 5 else 0
elif event.key == pygame.K_RIGHT:
self.x = self.x + 5 if self.x <= 375 else 380
elif event.key == pygame.K_UP:
self.y = self.y - 5 if self.y >= 5 else 0
elif event.key == pygame.K_DOWN:
self.y = self.y + 5 if self.y<= 521 else 526
elif event.key == pygame.K_SPACE:
# 调用子弹的方法和属性
zdd = HeroBullet(self.x + 50 - 2, self.y - 11, self.screen)
# 把子弹放入列表中,子弹是多个,我们将多个实例化的子弹放入列表中
self.bullet_list.append(zdd)
for bullet in self.bullet_list:
bullet.draw()
self.bullet_list.remove(bullet) if bullet.y<0 else " "
# 碰撞检测
def check_collide(self,bullet_list):
hero_rect = pygame.rect.Rect(self.x,self.y,100,124)
#print(hero_rect)
for bullet in bullet_list:
enemy_bullet_rect = pygame.rect.Rect(bullet.x,bullet.y,9,21) #定义英雄子弹的rect
flag = enemy_bullet_rect.colliderect(hero_rect) #检测敌机和子弹的矩形是否相交
if flag:
print("英雄机爆炸了!")
self.isBomb = True #英雄机爆炸条件为真
bullet_list.remove(bullet) #移除敌机子弹
#敌机类
class EnemyPlane():
def __init__(self,x,y,screen):
self.x = x
self.y = y
self.screen = screen
self.normal_image_list = ["enemy2.png"]
self.normal_image_index = 0
self.bomb_image_list = ["enemy2_down1.png",
"enemy2_down2.png",
"enemy2_down3.png",
"enemy2_down4.png"]
self.bomb_image_index = 0
self.isBomb = False
self.bullet_list = []
self.direct = '左'
def draw(self):
if self.isBomb == False:
#如果没爆炸
pic = get_pic(self.normal_image_list[self.normal_image_index]) #获取图片
self.screen.blit(pic,(self.x,self.y)) #绘制敌机战机
self.normal_image_index = (self.normal_image_index+1)%len(self.normal_image_list) #利用循环让敌机进行循环
else:
if self.bomb_image_index == len(self.bomb_image_list):
time.sleep(0.2)
exit(0)
enemy_bomb_img = get_pic(self.bomb_image_list[self.bomb_image_index]) #加载敌机爆炸图片
self.screen.blit(enemy_bomb_img,(self.x,self.y)) #绘制敌机爆炸图片
self.bomb_image_index +=1
time.sleep(0.2)
#调用移动函数
self.move()
#敌机开火
self.fire()
def move(self):
# 让敌机移动
if self.direct == "左":
self.x -= 5
if self.x <= 0:
self.direct = "右"
elif self.direct == "右":
self.x += 5
if self.x >= 480 - 69:
self.direct = "左"
def fire(self):
#敌机子弹
#画出敌机子弹
#产生随机数
x = random.randint(0,100)
if x ==2 or x ==50:
#实例化一个子弹
enemy_bullet = EnemyBullet(self.x+69//2-9//2,self.y+89,self.screen)
#产生的每一个子弹放到一个列表中
self.bullet_list.append(enemy_bullet)
for bullet in self.bullet_list:
bullet.draw()
# 让子弹到最下面的时候消失
self.bullet_list.remove(bullet)if bullet.y>650-89-21//2 else""
def check_collide(self, bullet_list):
"""碰撞检测"""
# 定义敌机精灵的rect
enemy_rect = pygame.rect.Rect(self.x, self.y, 50, 56) # x= 480//2-69//2
print(enemy_rect)
for bullet in bullet_list:
hero_bullet_rect = pygame.rect.Rect(bullet.x, bullet.y, 22, 22) # 定义英雄子弹的rect
flag = hero_bullet_rect.colliderect(enemy_rect) # 检测敌机和战机子弹的矩形是否相交
if flag:
print("敌机爆炸了......")
self.isBomb = True # 爆炸条件为真
bullet_list.remove(bullet) # 移除战机子弹
if __name__ == '__main__':
# 游戏初始化
pygame.init()
# 设置背景图片
screen = pygame.display.set_mode((480, 650))
# 设置标题
pygame.display.set_caption("飞机大战")
# 设置图标
pygame.display.set_icon(get_pic("icon.png"))
# 设置按键灵敏度
pygame.key.set_repeat(20, 30)
# 实例化英雄飞机对象
hero_plane = HeroPlane(480 // 2 - 100 // 2, 650 - 124, screen)
# 实例化敌机对象
enemy_plane = EnemyPlane(480 // 2 - 69 // 2, 0, screen)
while True:
clock.tick(60)
# 绘制背景图
bg = get_pic("background.png")
# 飞机可以从底部飞出
# 飞机可以从底部飞出
screen.blit(bg, (0, 0))
enemy_plane.check_collide(hero_plane.bullet_list)# 敌机碰撞检测
hero_plane.check_collide(enemy_plane.bullet_list) # 英雄机碰撞检测
hero_plane.draw() # 绘制英雄精灵
enemy_plane.draw() # 绘制敌机精灵
hero_plane.deal_event(pygame.event.get()) # 事件检测
pygame.display.update()