import pygame, sys, traceback
from pygame.locals import *
from random import *
import plane
import time
import asteroid
# initialize
pygame.init()
transparent = (0, 0, 0, 0)
# pygame.mixer.init() # for music
bg_size = width, height = 800, 600
#screen = pygame.display.set_mode(bg_size,FULLSCREEN|DOUBLEBUF|HWSURFACE)
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Firebase")
background = pygame.image.load("assets/space2.png").convert() # alpha = 0
pygame.mouse.set_visible(False)
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
WHITE = (255,255,255)
SKY_BLUE = (202,235,216)
GREY = (192,192,192)
def add_asteroid(posy1, group, timer):
for i in range(24):
num = choice([1,2,3])
if num == 1:
ast = asteroid.Comet1(bg_size, posy1)
elif num == 2:
ast = asteroid.Comet2(bg_size, posy1)
elif num == 3:
ast = asteroid.Comet3(bg_size, posy1)
group.add(ast)
posy1 += 25
pygame.time.set_timer(timer, 4 * 1000)
# def reset(group):
# random.shuffle(group)
# main module
def main():
# 用于延迟
delay = 100
running = True
clock = pygame.time.Clock()
# 用于得分计时
addscore = True
ADDS = USEREVENT
pygame.time.set_timer(ADDS, 1 * 80)
#pygame.time.set_timer(ADDS, 0)
# 用于陨石计时
SHOWT = USEREVENT + 1
# pygame.time.set_timer(SHOWT, 4 * 1000)
# 用于计算trial
trial = 0
last_asteroid = 0;
# 用于生成asteroid
stepy = 10
# 生成飞机
fire = plane.Fire(bg_size)
me = plane.MyPlane(bg_size)
# 生成陨石
all_asteroid = pygame.sprite.Group()
add_asteroid(stepy, all_asteroid, SHOWT)
# 统计得分
score = 0
adds = 2
score_font1 = pygame.font.SysFont('arvo', 21)
score_font2 = pygame.font.SysFont('arial', 18)
# 生成shield
shield = 1
shield_font = pygame.font.SysFont('arvo', 25)
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_q:
pygame.quit()
sys.exit()
elif event.type == ADDS:
if addscore:
score += 10
elif event.type == SHOWT:
for each in all_asteroid:
each.active = True
pygame.time.set_timer(SHOWT, 0)
fire.update()
me.update()
screen.blit(background, (0,0))
# 绘制火焰
if fire.active:
screen.blit(fire.image2, fire.rect2)
# 绘制飞机
if me.active:
screen.blit(me.image1, me.rect)
# 绘制得分
score_text1 = score_font1.render("Score:", True, WHITE)#拒绝锯齿
score_text2 = score_font2.render("%s" % str(score), True, WHITE)#拒绝锯齿
screen.blit(score_text1, (25,100))
screen.blit(score_text2, (25,120))
# 绘制shield
shield_text = shield_font.render("Shields:", True, WHITE)#拒绝锯齿
shield1 = pygame.draw.line(screen, GREY, (25,70), (150,70), 18 )
shield2 = pygame.draw.line(screen, SKY_BLUE, (25,70), (150,70), 18 )
screen.blit(shield_text, (25,40))
# 绘制asteroid
for each in all_asteroid:
if each.active:
each.move()
screen.blit(each.image, each.rect)
if each.cl:
last_asteroid += 1
each.cl = False
#print(last_asteroid)
# reset
if last_asteroid == 24:
last_asteroid = 0
del all_asteroid
all_asteroid = pygame.sprite.Group()
add_asteroid(stepy, all_asteroid, SHOWT)
# pygame.time.set_timer(SHOWT, 4 * 1000)
clock.tick(60)
pygame.display.flip()
if __name__ == '__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()