Python飞机大战-面向对象基础

140行小游戏,更换完图片可直接运行

import pygame,copy,random,time
from pygame.locals import *
class Base():
def init(self,windows,x,y):
self.x = x
self.y = y
self.windows=windows
class Baseplane(Base):
def init(self,windows,x,y):
super().init(windows,x,y)
self.normalIndex=0
self.bombIndex=0
self.isBomb=False
self.zdList=[]
def draw(self):
if self.isBombFalse:
pic=pygame.image.load(self.normalImageList[self.normalIndex])
self.windows.blit(pic,(self.x,self.y))
self.normalIndex=(self.normalIndex+1)%len(self.normalImageList)
else:
if self.bombIndex
len(self.bombImageList):
time.sleep(0.4)
exit(0)
pic = pygame.image.load(self.bombImageList[self.bombIndex])
self.windows.blit(pic, (self.x, self.y))
self.bombIndex = (self.bombIndex + 1)
time.sleep(0.4)

class Bullet(Base):
def init(self,windows,x,y,path,moveSize):
super().init(windows,x,y)
self.moveSize=moveSize
self.pic = pygame.image.load(path)
def draw(self):
self.windows.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y+=self.moveSize

class EnemyPalne(Baseplane):
def init(self,windows,x,y):
super().init(windows,x,y)
self.normalImageList=[’.\img\enemy1.png’]
self.bombImageList=[’.\img\enemy1_down1.png’,’.\img\enemy1_down2.png’,
‘.\img\enemy1_down3.png’,’.\img\enemy1_down4.png’]
self.direct=“右”
def draw(self):
super().draw()
self.move()
self.fire()
tempList=copy.copy(self.zdList)
for zd in tempList:
zd.draw()
self.zdList.remove(zd) if zd.y>600 else""
def fire(self):
d=random.randint(1,100)
if d3 or d69:
one=Bullet(self.windows,self.x+69//2 -9//2,89,’.\img\bullet1.png’,3)
self.zdList.append(one)
def move(self):
if self.direct==“右”:
self.x+=2
if self.x>480-69:
self.direct=“左”
else:
self.x-=2
if self.x<=0:
self.direct=“右”
def pzjc(self,zdList):
tempList=zdList
EnemyRect = Rect(self.x, self.y, 69, 89)
for zd in tempList:
zdRect=Rect(zd.x,zd.y,22,22)
if zdRect.colliderect(EnemyRect):
self.isBomb=True
zdList.remove(zd)

class HeroPalne(Baseplane):
def init(self,windows,x,y):
super().init(windows,x,y)
self.normalImageList=[’.\img\hero1.png’,’.\img\hero2.png’]
self.bombImageList=[’.\img\hero_blowup_n1.png’,’.\img\hero_blowup_n2.png’,
‘.\img\hero_blowup_n3.png’,’.\img\hero_blowup_n4.png’]
def draw(self):
super().draw()
tempList=copy.copy(self.zdList)
for zd in tempList:
zd.draw()
self.zdList.remove(zd) if zd.y<0 else “”
def move_left(self):
if self.x>0:
self.x-=15
def move_right(self):
if self.x<480-100:
self.x+=15
def fire(self):
onezd=Bullet(windows,self.x+50-11,self.y-22,’.\img\bullet.png’,-5)
self.zdList.append(onezd)
def pzjc(self,zdList):
tempList=zdList
jtRect=Rect(self.x+36,self.y,28,40)
jsRect=Rect(self.x,self.y+40,100,124-40)
for zd in tempList:
zdRect=Rect(zd.x,zd.y,9,21)
if zdRect.colliderect(jtRect) or zdRect.colliderect(jsRect):
self.isBomb=True
zdList.remove(zd)

def control(plane):
for event in pygame.event.get():
if event.typeQUIT:
exit(0)
elif event.type
KEYDOWN:
if event.keyK_LEFT:
plane.move_left()
elif event.key
K_RIGHT:
plane.move_right()
elif event.key==K_SPACE:
plane.fire()
windows=pygame.display.set_mode((280,300),0,32)
pygame.display.set_caption(“我是飞机大战”)
icon=pygame.image.load(’.\img\icon72x72.png’)
background=pygame.image.load(’.\img\background.png’)
pygame.display.set_icon(icon)
pygame.key.set_repeat(30,30)
heroPalne=HeroPalne(windows,480//2-100//2,600-124)
enemyplane=EnemyPalne(windows,480//2-69,0)

start=0

while True:
# t = Rect(0, start, 280, 300)
# windows.blit(background,(0,0),t)
# start+=20
# time.sleep(1)
heroPalne.draw()
enemyplane.draw()
heroPalne.pzjc(enemyplane.zdList)
enemyplane.pzjc(heroPalne.zdList)
enemyplane.move()
control(heroPalne)
pygame.display.update()
pass

猜你喜欢

转载自blog.csdn.net/weixin_43582101/article/details/84711808