面向过程
import pygame
from pygame.locals import *
import random, time
class Biu():
def __init__(self, x, y, wind):
self.x = x
self.y = y
self.wind = wind
self.pic = pygame.image.load(r"D:\python\飞机大战\img\img\bullet.png")
def draw(self):
self.wind.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y -= 5
class EnemyBiu():
def __init__(self, x, y, wind):
self.x = x
self.y = y
self.wind = wind
self.pic = pygame.image.load(r"D:\python\飞机大战\img\img\bullet1.png")
def draw(self):
self.wind.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y += 5
wind = pygame.display.set_mode((480, 500), 0, 32)
background = pygame.image.load(r"D:\python\飞机大战\img\img\background.png")
icon = pygame.image.load(r"D:\python\飞机大战\img\img\icon72x72.png")
heroIndex=0
heroPlane1=pygame.image.load(r"D:\python\飞机大战\img\img\hero1.png")
heroPlane2=pygame.image.load(r"D:\python\飞机大战\img\img\hero2.png")
heroBomImageList=[r"D:\python\飞机大战\img\img\hero_blowup_n1.png",r"D:\python\飞机大战\img\img\hero_blowup_n2.png",r"D:\python\飞机大战\img\img\hero_blowup_n3.png",r"D:\python\飞机大战\img\img\hero_blowup_n4.png"]
enemyBomImageList=[r"D:\python\飞机大战\img\img\enemy1_down1.png",r"D:\python\飞机大战\img\img\enemy1_down2.png",r"D:\python\飞机大战\img\img\enemy1_down3.png",r"D:\python\飞机大战\img\img\enemy1_down3.png"]
heroBomImageIndex=0
enemyBomImageIndex=0
heroPlaneIsBomb=False
enemyPlaneIsBomb=False
enemyPlane=pygame.image.load(r"D:\python\飞机大战\img\img\enemy1.png")
pygame.display.set_caption("飞机大战")
pygame.display.set_icon(icon)
pygame.key.set_repeat(30,30)
hero_x=(480-100)//2
hero_y=500-124
enemy_x=(480-69)//2
direct="左"
heroBiuList=[]
enemyBiuList=[]
zd=None
while True:
wind.blit(background, (0, 0))
if heroPlaneIsBomb==False:
if heroIndex==0:
wind.blit(heroPlane1,(hero_x,hero_y))
heroIndex=1
else:
wind.blit(heroPlane2,(hero_x, hero_y))
heroIndex = 0
else:
if heroBomImageIndex==len(heroBomImageList):
time.sleep(1)
exit(0)
pic=pygame.image.load(heroBomImageList[heroBomImageIndex])
wind.blit(pic,(hero_x,hero_y))
heroBomImageIndex+=1
time.sleep(0.5)
if enemyPlaneIsBomb==False:
wind.blit(enemyPlane, (enemy_x, 0))
else:
if enemyBomImageIndex==len(enemyBomImageList):
time.sleep(2)
exit(0)
pic = pygame.image.load(enemyBomImageList[enemyBomImageIndex])
wind.blit(pic, (enemy_x, 0))
enemyBomImageIndex += 1
time.sleep(1)
enemy_Rect=Rect(enemy_x,0,69,69)
for zd in heroBiuList:
zd.draw()
zdRect=Rect(zd.x,zd.y,22,22)
if enemy_Rect.colliderect(zdRect):
print("击败敌机")
enemyPlaneIsBomb = True
heroBiuList.remove(zd)
else:
heroBiuList.remove(zd) if zd.y<0 else ""
hero_Rect=Rect(hero_x,hero_y,100,124)
for zd in enemyBiuList:
zd.draw()
zdRect=Rect(zd.x,zd.y,9,21)
if hero_Rect.colliderect(zdRect):
print("被击中")
heroPlaneIsBomb=True
enemyBiuList.remove(zd)
else:
enemyBiuList.remove(zd) if zd.y >500 else ""
for event in pygame.event.get():
if event.type==QUIT:
print("退出")
exit(0)
elif event.type==KEYDOWN:
if event.key==K_LEFT:
hero_x=hero_x-5 if hero_x>0 else 0
elif event.key==K_RIGHT:
hero_x = hero_x + 5 if hero_x<480-100 else 480-100
elif event.key==K_UP:
hero_y = hero_y-5 if hero_y>0 else 0
elif event.key == K_DOWN:
hero_y = hero_y + 5 if hero_y < 500 - 124 else 500-124
elif event.key==K_SPACE:
zd=Biu(hero_x+100//2 -22//2,hero_y-22,wind)
heroBiuList.append(zd)
if direct=="左":
enemy_x-=1
if enemy_x<=0:
direct="右"
else:
enemy_x+=1
if enemy_x>=480-69:
direct="左"
h=random.randint(0,100)
if h==3 :
zd=EnemyBiu(enemy_x+69//2-9//2,89,wind)
enemyBiuList.append(zd)
pass
pygame.display.update()
pass
面向对象
import pygame
from pygame.locals import *
import os
import random
import time
def getPath(path):
return os.path.join("D:\\python\\飞机大战\\img\\img", path)
class ZhuangBei():
def __init__(self, x, y, windows):
self.x = x
self.y = y
self.windows = windows
class Zd(ZhuangBei):
def draw(self):
self.windows.blit(self.image, (self.x, self.y))
self.move()
class Biu(Zd):
def __init__(self, x, y, windows):
super().__init__(x, y, windows)
self.image = pygame.image.load(getPath("bomb.png"))
def move(self):
self.y -= 5
class EnemyBiu(Zd):
def __init__(self, x, y, windows):
super().__init__(x, y, windows)
self.image = pygame.image.load(getPath("bullet1.png"))
def move(self):
self.y += 5
class Plane(ZhuangBei):
def __init__(self, x, y, windows):
super().__init__(x, y, windows)
self.normalImageIndex = 0
self.bomImageIndex = 0
self.isBomb = False
self.biuList = []
def draw(self):
if not self.isBomb:
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))
else:
if len(self.bomImageList) == self.bomImageIndex:
time.sleep(0.5)
exit(0)
image = pygame.image.load(getPath(self.bomImageList[self.bomImageIndex]))
self.bomImageIndex += 1
self.windows.blit(image, (self.x, self.y))
time.sleep(0.3)
class EnemyPlane(Plane):
def __init__(self, x, y, windows):
self.normalImageList = ['enemy1.png']
self.bomImageList = ['enemy1_down1.png', 'enemy1_down2.png', 'enemy1_down3.png', 'enemy1_down4.png']
super().__init__(x, y, windows)
self.direct = "左"
def draw(self):
super().draw()
if not self.isBomb:
self.move()
self.fire()
for zd in self.biuList:
zd.draw()
self.biuList.remove(zd) if zd.y > 625 else ""
def move(self):
if self.direct == "左":
self.x -= 2
if self.x <= 0:
self.direct = "右"
else:
self.x += 2
if self.x >= 480 - 69:
self.direct = "左"
def fire(self):
h = random.randint(0, 100)
if h == 3 or h == 67:
zd = EnemyBiu(self.x + 69 // 2 - 9 // 2, 89, windows)
self.biuList.append(zd)
def pzjc(self, bList):
eRect = Rect(self.x, self.y, 69, 89)
for zd in bList:
zdRect = Rect(zd.x, zd.y, 22, 22)
if zdRect.colliderect(eRect):
self.isBomb = True
bList.remove(zd)
class HeroPlane(Plane):
def __init__(self, x, y, windows):
self.normalImageList = ['hero1.png', 'hero2.png']
self.bomImageList = ['hero_blowup_n1.png', 'hero_blowup_n2.png', 'hero_blowup_n3.png', 'hero_blowup_n4.png']
super().__init__(x, y, windows)
def pzjc(self, bList):
zRect = Rect(self.x, self.y, 100, 124)
for zd in bList:
edRect = Rect(zd.x, zd.y, 9, 21)
if edRect.colliderect(zRect):
self.isBomb = True
bList.remove(zd)
def draw(self):
super().draw()
for zd in self.biuList:
zd.draw()
self.biuList.remove(zd) if zd.y < 0 else ""
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_UP:
self.y = self.y - 5 if self.y > 0 else 0
elif event.key == K_DOWN:
self.y = self.y + 5 if self.y < 625 - 124 else 625 - 124
elif event.key == K_SPACE:
zd = Biu(self.x + 100 // 2 - 22 // 2, self.y - 22, self.windows)
self.biuList.append(zd)
windows = pygame.display.set_mode((480, 625), 0, 32)
pygame.display.set_caption("飞机大战")
backGround = pygame.image.load(getPath("background.png"))
icon = pygame.image.load(getPath('icon72x72.png'))
pygame.display.set_icon(icon)
pygame.key.set_repeat(30, 30)
heroPlane = HeroPlane(480 // 2 - 100 // 2, 625 - 124, windows)
enemyPlane = EnemyPlane((480 - 69) // 2, 0, windows)
while True:
windows.blit(backGround, (0, 0))
heroPlane.draw()
heroPlane.dealEvent(pygame.event.get())
enemyPlane.draw()
enemyPlane.pzjc(heroPlane.biuList)
heroPlane.pzjc(enemyPlane.biuList)
pygame.display.update()