#-*- coding: UTF-8 -*- import pygame import sys import random import time from settings import Settings from hero import Hero from airplane import Airplane from enemy import Enemy from award import Award from flyingObject import FlyingObject #https://blog.csdn.net/caozewei/article/details/81015192 START = 0 RUNNING = 1 PAUSE = 2 GAMEOVER = 3 state = START sets = Settings() screen = pygame.display.set_mode((sets.bgImageWidth,sets.bgImageHeight),0,32) #创建窗口 print("bg image's width:%f,height:%f" % (sets.bgImageWidth,sets.bgImageHeight)) print("heroImage is %s" % sets.heroImages) hero=Hero(screen,sets.heroImages) flyings=[] bullets=[] score=0 def hero_blitme(): #画英雄机 global hero hero.blitme() def bullets_blitme(): for b in bullets: b.blitme() def flyings_blitme(): global sets for fly in flyings: fly.blitme() def score_blitme(): pygame.font.init() fontObj = pygame.font.Font("Tahoma.ttf",20) textSurfaceObj=fontObj.render(u'生命值: %d\n分数:%d\n火力值: %d' % (hero.getLife(),score,hero.isDoubleFire()),False,(135,100,184)) textRectObj = textSurfaceObj.get_rect() textRectObj.center=(300,40) screen.blit(textSurfaceObj,textRectObj) def state_blitme(): global sets global state if state==START: screen.blit(sets.start,(0,0)) elif state==PAUSE: screen.blit(sets.pause,(0,0)) elif state==GAMEOVER: screen.blit(sets.gameover,(0,0)) def blitmes(): hero_blitme() flyings_blitme() bullets_blitme() score_blitme() state_blitme() def nextOne(): type=random.randint(0,20) if type<4: return Bee(screen,sets.beeImage) elif type==5: return Bee(screen,sets.beeImage) else: return Airplane(screen,sets.airImage) flyEnteredIndex=0 def enterAction(): global flyEnteredIndex flyEnteredIndex+=1 if flyEnteredIndex % 40 == 0: flyingobj=nextOne() flyings.append(flyingobj) shootIndex=0 def shootAction(): if shootIndex % 10 == 0: heroBullet=hero.shoot(sets.heroBullet) for bb in heroBullet: bullets.append(bb) def stepAction(): hero.step() for flyobj in flyings: flyobj.step() global bullets for b in bullets: b.step() def outOfBoundAction(): global flyings flyingLives=[] index=0 for f in flyings: print (f) print ("lds:") if f.outOfBounds()==True: flyingLives.insert(index,f) index+=1 flyings=flyingLives index=0 global bullets bulletsLive=[] for b in bullets: if b.outOfBounds()==True: bulletsLive.insert(index,b) index+=1 bullets=bulletsLive j=0 def bangAction(): for b in bullets: bang(b) def bang(b): index=-1 for x in range(0,len(flyings)): f=flyings[x] if f.shootBy(b): index=x break if index != -1: one=flyings[index] if isinstance(one,Enemy): global score score+=one.getScore() flyings.remove(one) bullets.remove(b) def checkGameOverAction(): if isGameOver(): global state state=GAMEOVER hero.reLife() def isGameOver(): for f in flyings: if hero.hit(f): hero.sublife() hero.clearDoubleFire() flyings.remove(f) return hero.getLife()<=0 def action(): x,y = pygame.mouse.get_pos() blitmes() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: flag=pygame.mouse.get_pressed()[0] rflag=pygame.mouse.get_pressed()[2] global state if flag==True and (state==START or state==PAUSE): state=RUNNING if flag==True and state==GAMEOVER: state=START if rflag==True: state=PAUSE if state==RUNNING: hero.move(x,y) enterAction() shootAction() stepAction() outOfBoundAction() bangAction() checkGameOverAction() def main(): pygame.display.set_caption("飞机大战") while True: print("screen blit...") screen.blit(sets.bgImage,(0,0)) print("start action") action() print("display update") pygame.display.update() print("sleep 0.1") time.sleep(0.1) if __name__ == '__main__': main()