- 帖子
- 3
- 精华
- 0
- 积分
- 15
- 阅读权限
- 10
- 注册时间
- 2017-10-30
- 最后登录
- 2017-11-3
|
import pygame
import random
import sys
class Bullet:
def __init__(self):
self.x=0
self.y=-1
self.image=pygame.image.load("bullet.png").convert_alpha()
self.active=False
def move(self):
#激活状态下,向上移动
if self.active:
self.y-=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 Enemy:
def __init__(self):
self.level=1
self.restart()
self.image=pygame.image.load("enemy.png").convert_alpha()
def move(self):
if self.y<650: #800为屏幕的高度
#向下移动
self.y+=self.speed
#self.y+=0.09
else:
self.restart()
def restart(self):
self.x=random.randint(50,400)
self.y=random.randint(-200,-50)
self.speed=random.uniform(0.01,0.1)+0.1*self.level
def setLeval(self,level):
self.level=level
class Plane:
def restart(self):
self.x=200
self.y=450
def __init__(self):
self.restart()
self.image=pygame.image.load("plane.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
def checkHit(enemy,bullet,score):
if ((bullet.x>enemy.x and bullet.x<enemy.x+enemy.image.get_width())
and (bullet.y>enemy.y and bullet.y<enemy.y+enemy.image.get_height())):
if(score %1000 ==0 ):
enemy.setLeval(score/1000+1)
enemy.restart()
else:
enemy.restart()
#enemy.restart()
bullet.active=False
return True
return False
def checkCrash(enemy, plane):
if (plane.x + 0.7*plane.image.get_width() > enemy.x) and (plane.x + 0.3*plane.image.get_width() < enemy.x + enemy.image.get_width()) and (plane.y + 0.7*plane.image.get_height() > enemy.y) and (plane.y + 0.3*plane.image.get_width() < enemy.y + enemy.image.get_height()):
return True
return False
pygame.init()
screen=pygame.display.set_mode((450,650),0,32)
pygame.display.set_caption("Hello,World")
#加载并转换图像
background=pygame.image.load("back.jpg").convert()
plane=Plane()
bullets=[]
for i in range(5):
bullets.append(Bullet())
#子弹总数
count_b=len(bullets)
#即将激活的子弹序号
index_b=0
#发射子弹的间隔
interval_b=0
enemies=[]
for i in range(5):
enemies.append(Enemy())
gameover=False
score=0
font=pygame.font.Font(None,32)
while True:
for event in pygame.event.get():
#接收到退出事件后退出程序
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if gameover and event.type==pygame.MOUSEBUTTONUP:
plane.restart()
for e in enemies:
e.setLeval(1)
e.restart()
for b in bullets:
b.active=False
score=0
gameover=False
screen.blit(background,(0,0))
if gameover==False:
interval_b-=1
if(interval_b<0):
bullets[index_b].restart()
interval_b=100
index_b=(index_b+1)%count_b
#判断每个子弹的状态
for b in bullets:
if b.active:
for e in enemies:
if checkHit(e,b,score):
score+=100
b.move()
screen.blit(b.image,(b.x,b.y))
for e in enemies:
e.move()
screen.blit(e.image,(e.x,e.y))
for b in bullets:
if b.active:
b.move()
screen.blit(b.image,(b.x,b.y))
for e in enemies:
#如果撞上敌机,设gameover为True
if checkCrash(e, plane):
gameover = True
e.move()
screen.blit(e.image, (e.x, e.y))
plane.move()
#再画飞机
screen.blit(plane.image,(plane.x,plane.y))
text=font.render("Socre: %d" %score,1,(0,0,0))
screen.blit(text, (0, 0))
else:
text=font.render("Socre: %d" %score,1,(0,0,0))
screen.blit(text, (170, 300))
#pass
#把飞机画到屏幕上
pygame.display.update()
程序可以运行,但是多运行几次就会出现在问题。
|
|