做的比较low,但是没有low的开始怎么会有辉煌的结果!加油,从未停止前进!
# 飞机大战面向过程
# import pygame
# from pygame.locals import *
# import random
# import time
# class HeroBullet():
# def __init__(self,x,y,windows):
# self.x=x
# self.y=y
# self.windows=windows
# self.pic=pygame.image.load('D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\bullet.png')
# def draw(self):
# self.windows.blit(self.pic,(self.x,self.y))
# self.move()
# def move(self):
# self.y-=5
# class enemyBullet():
# def __init__(self,x,y,windows):
# self.x=x
# self.y=y
# self.windows=windows
# self.pic=pygame.image.load('D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\bullet1.png')
# def draw(self):
# self.windows.blit(self.pic,(self.x,self.y))
# self.move()
# def move(self):
# self.y+=5
# windows=pygame.display.set_mode((480,650),0,32)#创建窗口
# bg=pygame.image.load('D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\background.png')
# pygame.display.set_caption("灰机大战")
# icon=pygame.image.load('D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\plane.png')
# pygame.display.set_icon(icon)
# heroPlane1=pygame.image.load('D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\hero1.png')
# heroPlane2=pygame.image.load('D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\hero2.png')
# enemyPlane=pygame.image.load('D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\enemy1.png')
# enemyBomList=['D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down1.png',
# 'D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down2.png',
# 'D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down3.png',
# 'D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down4.png',]
# heroBomList=['D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n1.png',
# 'D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n2.png',
# 'D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n3.png',
# 'D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n4.png',]
# heroIndexShift=0
# pygame.key.set_repeat(2,3)#可以一直按键
# heroPlaneX=190
# heroPlaneY=526
# enemyPlaneX=480//2-69//2
# enemyPlaneY=0
# direct="左"
# BiuList=[]
# enenyBiuList=[]
# enemy_isBomb=False
# hero_isBomb=False
# hero_BomIndex=0
# enemy_BomIndex=0
# while True:
# windows.blit(bg,(0,0))
# # if heroIndexShift==0:
# # windows.blit(heroPlane1,(heroPlaneX,heroPlaneY))
# # heroIndexShift+=1
# # else:
# # windows.blit(heroPlane2, (heroPlaneX, heroPlaneY))
# # heroIndexShift=0
# if direct=="左":
# enemyPlaneX-=3
# if enemyPlaneX<=0:
# direct="右"
# elif direct=="右":
# enemyPlaneX+=3
# if enemyPlaneX>=480-69:
# direct="左"
# if enemy_isBomb==False:
# windows.blit(enemyPlane, (enemyPlaneX, enemyPlaneY))
# else:
# if enemy_BomIndex==len(enemyBomList):
# exit(0)
# pic=pygame.image.load(enemyBomList[enemy_BomIndex])
# windows.blit(pic,(enemyPlaneX,enemyPlaneY))
# enemy_BomIndex=(enemy_BomIndex+1)
# time.sleep(0.5)
# if hero_isBomb==False:
# windows.blit(heroPlane1, (heroPlaneX, heroPlaneY))
# else:
# if hero_BomIndex==len(heroBomList):
# exit(0)
# pic=pygame.image.load(heroBomList[hero_BomIndex])
# windows.blit(pic,(heroPlaneX,heroPlaneY))
# hero_BomIndex=(hero_BomIndex+1)
# time.sleep(0.5)
# for biu in BiuList:
# biu.draw()
# BiuList.remove(biu) if biu.y<0 else ""
# for biu in enenyBiuList:
# biu.draw()
# enenyBiuList.remove(biu) if biu.y>650 else ""
# for event in pygame.event.get():
# if event.type==QUIT:
# print("关闭了")
# exit(0)
# if event.type==KEYDOWN:
# if event.key==K_LEFT:
# heroPlaneX=heroPlaneX-5 if heroPlaneX>5 else 0
# elif event.key==K_RIGHT:
# heroPlaneX=heroPlaneX+5 if heroPlaneX<380 else 380
# elif event.key==K_UP:
# heroPlaneY=heroPlaneY-5 if heroPlaneY>0 else 0
# elif event.key==K_DOWN:
# heroPlaneY =heroPlaneY+5 if heroPlaneY<650-124 else 650-124
# elif event.key==K_SPACE:
# oneBiu=HeroBullet(heroPlaneX+50-11,heroPlaneY-22,windows)
# BiuList.append(oneBiu)
# x=random.randint(0,100)
# if x==5 or x==76:
# oneBiu=enemyBullet(enemyPlaneX+69//2-9//2,enemyPlaneY+89,windows)
# enenyBiuList.append(oneBiu)
# dijiRect=Rect(enemyPlaneX,enemyPlaneY,69,89)
# heroRect=Rect(heroPlaneX,heroPlaneY,100,124)
# for biu in BiuList:
# biuRect=Rect(biu.x,biu.y,22,22)
# if biuRect.colliderect(dijiRect):
# enemy_isBomb=True
# BiuList.remove(biu)
# for biu in enenyBiuList:
# enemybiuRect=Rect(biu.x,biu.y,9,21)
# if enemybiuRect.colliderect(heroRect):
# hero_isBomb=True
# enenyBiuList.remove(biu)
# pygame.display.update()
#
# #飞机大战面向对象
import pygame,random,time,os
from pygame.locals import *
def getPic(path):
return os.path.join("D:\\软件\\录屏软件\\IT研究院-Python\\New_Stydy\\img",path)
class BasePB():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
class BasePlane(BasePB):
def __init__(self,x,y,windows,normalImageList,bomImageList):
super().__init__(x,y,windows)
self.normalImageList=normalImageList
self.normalIndex=0
self.bomImageList=bomImageList
self.bomImageIndex=0
self.isBomb=False
self.biuList=[]
def draw(self):
if self.isBomb==False:
pic=pygame.image.load(getPic(self.normalImageList[self.normalIndex]))
self.windows.blit(pic,(self.x,self.y))
self.normalIndex=(self.normalIndex+1)%len(self.normalImageList)
else:
if self.bomImageIndex==len(self.bomImageList):
time.sleep(0.8)
exit(0)
pic = pygame.image.load(getPic(self.bomImageList[self.bomImageIndex]))
self.windows.blit(pic, (self.x, self.y))
self.bomImageIndex = (self.bomImageIndex + 1)
time.sleep(0.6)
class BaseBullet(BasePB):
def __init__(self,x,y,windows,pic):
super().__init__(x, y, windows)
self.pic=pic
def draw(self):
self.windows.blit(self.pic,(self.x,self.y))
self.move()#注意父类不能实例化,否则会报错找不到这个move方法
class HeroBullet(BaseBullet):
def __init__(self,x,y,windows):
super().__init__(x,y,windows,pygame.image.load(getPic("zidan2.png")))
def move(self):
self.y-=5
class enemyBullet(BaseBullet):
def __init__(self,x,y,windows):
super().__init__(x,y,windows,pygame.image.load(getPic("bullet-1.gif")))
def move(self):
self.y+=5
class HeroPlane(BasePlane):
def __init__(self,x,y,windows):
super().__init__(x,y,windows,["hero1.png","hero2.png"],["hero_blowup_n1.png","hero_blowup_n2.png","hero_blowup_n3.png","hero_blowup_n4.png"])
def draw(self):
super().draw()
for biu in self.biuList:
biu.draw()
self.biuList.remove(biu) if biu.y<0 else ""
def pzjc(self,bList):
eRect=Rect(self.x,self.y,100,124)#我机
for biu in bList:
bRect=Rect(biu.x,biu.y,9,21)
if bRect.colliderect(eRect):
self.isBomb=True
def dealEvent(self,eventList):
for event in eventList:
if event.type==QUIT:
exit(0)
if 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_UP:
self.y=self.y-100 if self.y>=100 else 0
elif event.key==K_DOWN:
self.y=self.y+5 if self.y<650-124-5 else 650-124
elif event.key==K_SPACE:
oneBiu=HeroBullet(self.x+39,self.y-32,windows)
self.biuList.append(oneBiu)
class EnemyPlane(BasePlane):
def __init__(self,x,y,windows):
super().__init__(x,y,windows,["enemy1.png"],["enemy1_down1.png", "enemy1_down2.png", "enemy1_down3.png", "enemy1_down4.png"])
self.direct="左"
def draw(self):
super().draw()
self.move()
self.fire()
for biu in self.biuList:
biu.draw()
self.biuList.remove(biu) if biu.y>650 else ""
def move(self):
if self.direct == "左":
self.x-=3
if self.x<=0:
self.direct="右"
elif self.direct=="右":
self.x+=3
if self.x>=480-69:
self.direct="左"
def fire(self):
x = random.randint(0, 100)
if x == 5 or x == 76:
oneBiu = enemyBullet(self.x + 69 // 2 - 9 // 2, self.y + 89, windows)
self.biuList.append(oneBiu)
def pzjc(self,bList):
eRect=Rect(self.x,self.y,69,89)#敌机
for biu in bList:
bRect=Rect(biu.x,biu.y,21,59)
if bRect.colliderect(eRect):
self.isBomb=True
windows=pygame.display.set_mode((480,650),0,32)
pygame.display.set_caption("打灰机")
icon=pygame.image.load(getPic("icon72x72.png"))
pygame.key.set_repeat(2,3)
pygame.display.set_icon(icon)
bg=pygame.image.load(getPic("bg2.png"))
heroPlane=HeroPlane(480//2-100//2,650-124,windows)
enemyPlane=EnemyPlane(480//2-69//2,0,windows)
while True:
windows.blit(bg,(0,0))
heroPlane.pzjc(enemyPlane.biuList)
enemyPlane.pzjc(heroPlane.biuList)
heroPlane.draw()
enemyPlane.draw()
heroPlane.dealEvent(pygame.event.get())
pygame.display.update()
# 飞机大战的知识点抽取举例
# class Base():
# def __init__(self,name):
# self.name=name
# def say(self):
# self.changge()
#
# class bu(Base):
# def changge(self):
# print("hehehe")
#
# # a=bu("哈哈")
# # a.say()#在这里实例化bu并且调用父类的say里面的changge方法是可以的因为父类没有changge方法会找到子类里的changge方法
# b=Base("呵呵")
# b.say()#出错'Base' object has no attribute 'changge'因为把父类实例化后父类根本就没有changge这个方法所以会报错