打飞机丧心病狂版
这是在打飞机正常版上修改的结果(我是有多桑心病狂。。。)。
# -*- coding: utf-8 -*-
import pygame
import random
#导入pygame库
from sys import exit #向sys模块借一个exit函数来退出程序
def checkHit(bullet,enimy):
if((bullet.x>enimy.x and bullet.x<enimy.x+enimy.image.get_width())and (bullet.y>enimy.y and bullet.y<enimy.y+enimy.image.get_height())):
enimy.restart()
bullet.active=False
return True
return False
def checkCrash(plane,enimy):
if ( (plane.x + 0.7*plane.image.get_width() > enimy.x) and (plane.x + 0.3*plane.image.get_width() < enimy.x + enimy.image.get_width()) and (plane.y + 0.7*plane.image.get_height() > enimy.y) and (plane.y + 0.3*plane.image.get_height() < enimy.y + enimy.image.get_height())):
enimy.restart()
plane.restart()
return True
return False
class Bullet():
def __init__(self):
self.x=0
self.y=-1
self.image=pygame.image.load('zidan.png').convert_alpha()
self.active=False
def move(self):
if self.active:
self.y-=0.3
if self.y<0:
self.active=False
# def restart(self):
# mouseX, mouseY = pygame.mouse.get_pos()
# self.x = mouseX - self.image.get_width() / 2
# self.y = mouseY - self.image.get_height() / 2
# self.active=True
class Bullet1(Bullet):
def __init__(self):
Bullet.__init__(self)
self.image=pygame.image.load('zidan2.png').convert_alpha()
def move(self):
if self.active:
self.x-=0.2
if self.x<0 or self.x>333:
self.active=False
def restart(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = mouseX - self.image.get_width() / 2
self.y = mouseY - self.image.get_height() / 2
self.active=True
class Bullet2(Bullet):
def __init__(self):
Bullet.__init__(self)
self.image=pygame.image.load('zidan2.png').convert_alpha()
def move(self):
if self.active:
self.x+=0.2
if self.x<0 or self.x>333:
self.active=False
def restart(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = mouseX - self.image.get_width() / 2
self.y = mouseY - self.image.get_height() / 2
self.active=True
class BulletR(Bullet):
def __init__(self):
Bullet.__init__(self)
def move(self):
Bullet.move(self)
def restart(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = (mouseX - self.image.get_width() / 2)+22
self.y = mouseY - self.image.get_height() / 2
self.active=True
class BulletL(Bullet):
def __init__(self):
Bullet.__init__(self)
def move(self):
Bullet.move(self)
def restart(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = (mouseX - self.image.get_width() / 2)-20
self.y = mouseY - self.image.get_height() / 2
self.active=True
class Enimy():
def restart(self):
self.x = random.randint(0, 400)
self.y = random.randint(-200, -50)
self.speed = random.random()/6 + 0.05
def __init__(self):
self.restart()
self.image = pygame.image.load('enimy.png').convert_alpha()
def move(self):
if self.y < 500:
self.y += self.speed
else:
self.restart()
class Plane():
def restart(self):
self.x=167
self.y=500
def __init__(self):
self.restart()
self.image=pygame.image.load('dafeiji.png').convert_alpha()
def move(self):
x,y=pygame.mouse.get_pos()
x-=self.image.get_width()/2
y-=self.image.get_height()/2
self.x=x
self.y=y
pygame.init()
score=0
f=file('highscore.txt')
hscore=int(f.read())
f.close()
#初始化pygame,做硬件准备
screen=pygame.display.set_mode((333,497),0,32)
#创建了一个窗口,窗口大小和背景图片一样
pygame.display.set_caption("Fghting!")
#设置窗口标题
background=pygame.image.load('back.jpg').convert()
plane=Plane()
bulletsR=[]
bulletsL=[]
bullets1=[]
bullets2=[]
enimys=[]
for i in range(5):
bulletsR.append(BulletR())
bulletsL.append(BulletL())
bullets1.append(Bullet1())
bullets2.append(Bullet2())
enimys.append(Enimy())
count_b=len(bulletsR)
index_b=0
interval_b=0
gameover = False
font=pygame.font.Font(None,32)
while True:
#游戏主循环
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit()
if gameover and event.type == pygame.MOUSEBUTTONUP:
plane.restart()
for e in enimys:
e.restart()
for bR in bulletsR:
bR.active = False
for bL in bulletsL:
bL.active = False
score = 0
gameover = False
screen.blit(background, (0, 0))
if not gameover:
interval_b-=1
if interval_b<0:
bulletsR[index_b].restart()
bulletsL[index_b].restart()
bullets1[index_b].restart()
bullets2[index_b].restart()
interval_b=800
index_b=(index_b+1)%count_b
for bR in bulletsR:
if bR.active:
for e in enimys:
if checkHit(bR,e):
score+=100
bR.move()
screen.blit(bR.image,(bR.x,bR.y))
for bL in bulletsL:
if bL.active:
for e in enimys:
if checkHit(bL,e):
score+=100
bL.move()
screen.blit(bL.image,(bL.x,bL.y))
for b1 in bullets1:
if b1.active:
for e in enimys:
if checkHit(b1,e):
score+=100
b1.move()
screen.blit(b1.image,(b1.x,b1.y))
for b2 in bullets2:
if b2.active:
for e in enimys:
if checkHit(b2,e):
score+=100
b2.move()
screen.blit(b2.image,(b2.x,b2.y))
for e in enimys:
if checkCrash(plane,e):
gameover=True
e.move()
screen.blit(e.image,(e.x,e.y))
plane.move()
screen.blit(plane.image, (plane.x,plane.y))
test = font.render('Score: %d' % score, 1, (0, 0, 0))
screen.blit(test, (0, 0))
text=font.render('Highscore: %d'%hscore,1,(0,0,0))
screen.blit(text,(155,0))
if score>hscore:
hscore=score
highscore='%d'%hscore
out=open('highscore.txt','w')
out.write(highscore)
out.close()
else:
test=font.render('Score: %d'%score,1,(0,0,0))
screen.blit(test,(110,250))
pass
pygame.display.update()
#刷新画面