'''
1、创建一个根目录,用来获取图片
2、设置窗口 背景图 标题 标题图标
3、设计双方飞机的位置 贴飞机图片
4、设置我方飞机喷火(贴两张图片 切换显示)
5、设置触发事件:窗口退出,飞机左右移动
6、设置循环 敌方飞机 左右自动移动
7、我方飞机、敌方飞机发射子弹
'''
import pygame
import os,random
from pygame.locals import *
def getPath(path):
return os.path.join(r'E:\python\0708\python使用软件\IT研究院-Python\New_Stydy\img',path)
class HBiu():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.image=pygame.image.load(getPath('bullet.png'))
def drow(self):
self.windows.blit(self.image,(self.x,self.y)) #贴图到windows
self.move()
def move(self): #设置子弹移动
self.y-=5
class HeroPlane(): #我方飞机
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.normalImageList=['hero1.png','hero2.png']
self.normalImageIndex=0 #索引list
self.hbiuList=[] #创建这个子弹图片列表
def drow(self):
image=pygame.image.load(getPath(self.normalImageList[self.normalImageIndex])) #导入我方飞机图
self.normalImageIndex=(self.normalImageIndex+1)%len(self.normalImageList) #设置循环 切换飞机图片
self.windows.blit(image,(self.x,self.y)) #贴图到windows窗口
for zd in self.hbiuList: # 循环 一个子弹图片 的 列表
zd.drow() #调用drow
def dealEvent(self,eventList): #设置事件
for event in eventList: # 循环获取事件(键盘 鼠标)
if event.type==QUIT: ##如果事件的类型是退出(点击退出按钮触发)
exit(0)
elif event.type==KEYDOWN: # 键盘
if event.key==K_LEFT: #向左
self.x =self.x-5 if self.x>5 else 0
elif event.key==K_RIGHT: #向右
self.x =self.x+5 if self.x <480-100-5 else 480-100
elif event.key==K_SPACE: #设置空格事件
zd=HBiu(self.x+100//2-22//2,self.y-22,windows)
self.hbiuList.append(zd)
# def pzjc(self,bList):
# heroRect=Rect(self.x,self.y,120,124) #我方飞机所以位置区域
class Dbiu():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.image=pygame.image.load(getPath('bullet1.png'))
def drow(self):
self.windows.blit(self.image,(self.x,self.y))
self.move()
def move(self):
self.y+=5
class EnemyPlane(): #敌方飞机
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.direct='左'
self.dbiuList=[]
def drow(self):
image=pygame.image.load(getPath('enemy0.png')) #导入敌方飞机
self.windows.blit(image,(self.x,self.y)) #贴图到windows窗口
self.move() #drow 调用move
for zd in self.dbiuList:
zd.drow()
def move(self): #设置敌机左右移动
if self.direct=='左':
self.x-=1
if self.x<=0:
self.direct='右'
else:
self.x+=1
if self.x>=480-69:
self.direct='左'
def file(self):
x=random.randint(1,100)
if x==3:
zd=Dbiu(self.x+69//2-9//2,self.y+89,self.windows)
self.dbiuList.append(zd)
windows=pygame.display.set_mode((480,625),0,32) #设置窗口
pygame.display.set_caption('飞机大战') #设置标题
backGround=pygame.image.load(getPath('backg.jpg')) #背景图片
icon=pygame.image.load(getPath('bomb.png')) #标题图标
heroPlane=HeroPlane(480//2-100//2,625-124,windows) #实例化飞机
enemyPlane=EnemyPlane(480//2-39//2,0,windows) #实例化敌机
pygame.display.set_icon(icon)
pygame.key.set_repeat(30,30)
##第一个参数是,按下键盘30毫秒后开始反应,第二个参数是按键按下30毫秒未抬起,触发一次新的按键事件
while True:
windows.blit(backGround,(0,0)) #贴背景图 到 窗口 从坐标(0,0)开始
heroPlane.drow() #调用drow函数
enemyPlane.drow() #调用drow函数
heroPlane.dealEvent(pygame.event.get()) #调用事件函数
pygame.display.update() #更新
飞机大战(简单版) 未完待续。。。。
最新推荐文章于 2020-04-13 11:46:04 发布