类和对象的概念着实是有些抽象,这一点代码照着学理解起来都有点勉强了,暂时是扩展不了了。。
import pygame #引用第三方模块
import random #随机产生数据的对象
from pygame.locals import * #将所有的Pygame常量导入
#创建飞机基类
class basePlane(object):
def __init__(self,screen,imagePaht):
self.screen=screen
#定义飞机图片路径,我方和敌方调用的不同图片素材
self.image=pygame.image.load(imagePaht)
#定义双方子弹空列表
self.bulletList=[]
def display(self):
#定义飞机的显示和位置,blit函数绘制位置(第一个参数是图片,后面的参数是位置)
self.screen.blit(self.image,(self.x, self.y))
'''
完善子弹的展示逻辑
因为对象在引用的时候无法删除,所以定义一个列表,使用了两个for循环遍历列表,一个添加一个删除
第1个for循环判断judge返回为True的子弹对象加入列表,返回为True则子弹越界
紧接着第2个for循环遍历了列表直接就删除了第1个for循环添加的子弹对象
第3个for循环是遍历了初始方法的子弹列表,显示子弹同时移动子弹
'''
needDelItemList = []
for item in self.bulletList:
if item.judge():
needDelItemList.append(item)
# 重新遍历一下
for i in needDelItemList:
self.bulletList.remove(i)
for bullet in self.bulletList:
bullet.display() # 显示子弹的位置
bullet.move() #让这个子弹进行移动
#创建公共子弹类
class CommonBullet(object):
def __init__(self,x,y,screen,bulletType):
#根据传参过来的参数设定使用子弹的素材
self.type=bulletType
self.screen=screen
if self.type=='hero':
#我方子弹初始位置,坐标根据飞机图片像素变动
self.x=x+13
self.y=y-20
self.imagePath='./feiji/bullet.png'
elif self.type=='enemy':
#敌方子弹初始位置,坐标根据飞机图片像素变动
self.x=x+98
self.y=y+120
self.imagePath='./feiji/bullet1.png'
self.image=pygame.image.load(self.imagePath)
def move(self):
#子弹的移动
if self.type=='hero':
self.y-=10
elif self.type=='enemy':
self.y+=5
def display(self):
#子弹的显示及位置
self.screen.blit(self.image,(self.x,self.y))
def judge(self):
#判断子弹是否越界
if self.y>650 or self.y<80:
return True
else:
return False
#创建我方飞机类
class HeroPlane(basePlane):
# 主窗体对象
def __init__(self,screen):
# 调用父类的构造方法
super().__init__(screen,'./feiji/hero.png')
#飞机的默认位置
self.x=250 #窗口图片的宽500,250中间
self.y=650 #窗口图片高700,飞机高50
def moveleft(self):
#左移动
if self.x>0:
self.x-=30
def movereght(self):
#右移动
if self.x<500-40:
self.x+=30
# 发射子弹
def sheBullet(self):
# 创建一个新的子弹对象
newBullet=CommonBullet(self.x, self.y, self.screen,'hero')
'''
这里结合子弹展示逻辑来看,每创建一个子弹对象就加入了初始方法的子弹列表
子弹列表有了新的对象,子弹展示那里的第3个for循环就会遍历列表触发展示和移动
'''
self.bulletList.append(newBullet)
#创建敌机类
class EnemyPlane(basePlane):
def __init__(self,screen):
super().__init__(screen,'./feiji/enemy0.png')
# 飞机的默认位置
self.x = 0
self.y = 0
# 默认设置一个方向
self.direction = 'right'
def sheBullet(self):
#敌机随机的发射子弹
num=random.randint(1,30)
if num==3:
newBullet=CommonBullet(self.x,self.y,self.screen,'enemy')
self.bulletList.append(newBullet)
def move(self):
#敌机移动 随机进行的
if self.direction=='right':
self.x+=2
elif self.direction=='left':
self.x-=2
if self.x>500-197:
self.direction='left'
elif self.x<0:
self.direction='right'
#键盘检测
def key_control(HeroObj):
'''
定义一个普通的函数,用来实现键盘的检测
:param HeroObj:可控制检测的对象
:return:
'''
eventList = pygame.event.get()
for item in eventList:
if item.type == QUIT:
print('退出')
exit()
elif item.type == KEYDOWN:
if item.type == K_a or item.key == K_LEFT:
HeroObj.moveleft() #调用函数实现左移动
elif item.type == K_d or item.key == K_RIGHT:
HeroObj.movereght() # 调用函数实现右移动
elif item.key == K_SPACE:
HeroObj.sheBullet() # 调用该方法就会创建子弹对象
#主函数,定义整体框架
def main():
#pygame.display.set_mode()—初始化一个准备显示的窗口或屏幕,背景图像素宽500,高700
screen=pygame.display.set_mode((500,700))
#设定一个背景图片对象
#pygame.image.load:载入图片
#如果调用image.load()发生了错误,检查图片是否和代码文件放在同一文件夹
#或者这么写:background=pygame.image.load(r'C:\Users\Username\Desktop\background.png')
background=pygame.image.load('./feiji/background.png')
#设置窗口的标题
pygame.display.set_caption('飞机大战')
#添加背景音乐
pygame.mixer.init()
pygame.mixer.music.load('./feiji/background.mp3')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1) #循环次数,-1表示无限循环
#创建一个飞机对象
hero=HeroPlane(screen)
#创建一个敌人飞机的对象
enemyplane=EnemyPlane(screen)
# 设定要显示的内容
while True:
screen.blit(background,(0,0))
#显示玩家飞机的图片
hero.display()
enemyplane.display() # 显示敌机
enemyplane.move() # 敌机移动
enemyplane.sheBullet() # 敌机随机发送子弹
#获取键盘事件
key_control(hero)
#更新显示内容
pygame.display.update()
#通过时钟对象指定循环频率
pygame.time.Clock().tick(15)
if __name__=='__main__':
main()