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.pic=pygame.image.load("E:\\学习笔记\\day18\\img\\bullet2.png")
self.windows=windows
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.pic=pygame.image.load("E:\\学习笔记\\day18\\img\\bullet3.png")
self.windows=windows
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((600,800),0,32)
bg=pygame.image.load("E:\\学习笔记\\day18\\img\\background2.jpeg")
pygame.display.set_caption("打灰机")
icon=pygame.image.load("E:\\学习笔记\\day18\\img\\plane.png")
pygame.display.set_icon(icon)
heroPlane1=pygame.image.load("E:\\学习笔记\\day18\\img\\feiji1.png")
heroPlane2=pygame.image.load("E:\\学习笔记\\day18\\img\\feiji2.png")
heroPlaneX=600//2-117//2
heroPlaneY=800-80
enemyPlane=pygame.image.load("E:\\学习笔记\\day18\\img\\feiji3.png")
enemyPlaneX=600//2-100//2
enemyPlaneY=0
heroBombList=["E:\\学习笔记\\day18\\img\\feiji1bomb1.png",
"E:\\学习笔记\\day18\\img\\feiji1bomb2.png",
"E:\\学习笔记\\day18\\img\\feiji1bomb3.png"]
enemyBombList=["E:\\学习笔记\\day18\\img\\feiji3bomb1.png",
"E:\\学习笔记\\day18\\img\\feiji3bomb2.png",
"E:\\学习笔记\\day18\\img\\feiji3bomb3.png"]
pygame.key.set_repeat(5,10)
direct="左"
HeroBiuList=[]
EnemyBiuList=[]
heroIndexShift=0
hero_isBomb=False
enemy_isBomb=False
hero_BombIndex=0
enemy_BombIndex=0
hero_HP=100
enemy_HP=2000
while True:
windows.blit(bg,(0,0))
if hero_isBomb==False:
if heroIndexShift==0:
windows.blit(heroPlane1,(heroPlaneX,heroPlaneY))
heroIndexShift=1
elif heroIndexShift==1:
windows.blit(heroPlane2,(heroPlaneX,heroPlaneY))
heroIndexShift=0
else:
if hero_BombIndex==len(heroBombList):
time.sleep(1)
exit(0)
pic=pygame.image.load(heroBombList[hero_BombIndex])
windows.blit(pic,(heroPlaneX,heroPlaneY))
hero_BombIndex+=1
time.sleep(1)
if enemy_isBomb==False:
windows.blit(enemyPlane,(enemyPlaneX,enemyPlaneY))
else:
if enemy_BombIndex==len(enemyBombList):
time.sleep(1)
exit(0)
pic=pygame.image.load(enemyBombList[enemy_BombIndex])
windows.blit(pic,(enemyPlaneX,enemyPlaneY))
enemy_BombIndex+=1
time.sleep(1)
for biu in HeroBiuList:
biu.draw()
HeroBiuList.remove(biu) if biu.y<0 else ""
for biu in EnemyBiuList:
biu.draw()
EnemyBiuList.remove(biu) if biu.y>800 else ""
for event in pygame.event.get():
if event.type==QUIT:
print("关闭了")
exit(0)
elif event.type==KEYDOWN:
if event.key==K_LEFT:
heroPlaneX=heroPlaneX-10 if heroPlaneX>10 else 0
elif event.key==K_RIGHT:
heroPlaneX=heroPlaneX+10 if heroPlaneX<600-117-10 else 600-117
elif event.key==K_UP:
heroPlaneY=heroPlaneY-10 if heroPlaneY>10 else 0
elif event.key==K_DOWN:
heroPlaneY=heroPlaneY+10 if heroPlaneY<800-80-10 else 800-80
elif event.key==K_SPACE:
zerobiu=HeroBullet(heroPlaneX,heroPlaneY-71,windows)
onebiu=HeroBullet(heroPlaneX+117-14,heroPlaneY-71,windows)
HeroBiuList.append(zerobiu)
HeroBiuList.append(onebiu)
if direct=="左":
enemyPlaneX-=5
if enemyPlaneX==0:
direct="右"
elif direct=="右":
enemyPlaneX+=5
if enemyPlaneX==600-100:
direct="左"
x=random.randint(0,30)
if x==10:
twobiu=EnemyBullet(enemyPlaneX+100//2-9/2,enemyPlaneY+76+21,windows)
EnemyBiuList.append(twobiu)
enemyRect=Rect(enemyPlaneX,enemyPlaneY,100,76)
for biu in HeroBiuList:
biuRect=Rect(biu.x,biu.y,14,71)
if biuRect.colliderect(enemyRect):
enemy_HP-=10
HeroBiuList.remove(biu)
heroRect=Rect(heroPlaneX+30,heroPlaneY,57,80)
for biu in EnemyBiuList:
biuRect=Rect(biu.x,biu.y,9,21)
if biuRect.colliderect(heroRect):
hero_HP-=20
EnemyBiuList.remove(biu)
if hero_HP==0:
hero_isBomb=True
if enemy_HP==0:
enemy_isBomb=True
pygame.display.update()
Python基础:飞机大战
最新推荐文章于 2022-07-03 08:13:03 发布