main.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #-*- coding: UTF-8 -*-
  2. import pygame
  3. import sys
  4. import random
  5. import time
  6. from settings import Settings
  7. from hero import Hero
  8. from airplane import Airplane
  9. from enemy import Enemy
  10. from award import Award
  11. from flyingObject import FlyingObject
  12. #https://blog.csdn.net/caozewei/article/details/81015192
  13. START = 0
  14. RUNNING = 1
  15. PAUSE = 2
  16. GAMEOVER = 3
  17. state = START
  18. sets = Settings()
  19. screen = pygame.display.set_mode((sets.bgImageWidth,sets.bgImageHeight),0,32) #创建窗口
  20. print("bg image's width:%f,height:%f" % (sets.bgImageWidth,sets.bgImageHeight))
  21. print("heroImage is %s" % sets.heroImages)
  22. hero=Hero(screen,sets.heroImages)
  23. flyings=[]
  24. bullets=[]
  25. score=0
  26. def hero_blitme():
  27. #画英雄机
  28. global hero
  29. hero.blitme()
  30. def bullets_blitme():
  31. for b in bullets:
  32. b.blitme()
  33. def flyings_blitme():
  34. global sets
  35. for fly in flyings:
  36. fly.blitme()
  37. def score_blitme():
  38. pygame.font.init()
  39. fontObj = pygame.font.Font("Tahoma.ttf",20)
  40. textSurfaceObj=fontObj.render(u'生命值: %d\n分数:%d\n火力值: %d' % (hero.getLife(),score,hero.isDoubleFire()),False,(135,100,184))
  41. textRectObj = textSurfaceObj.get_rect()
  42. textRectObj.center=(300,40)
  43. screen.blit(textSurfaceObj,textRectObj)
  44. def state_blitme():
  45. global sets
  46. global state
  47. if state==START:
  48. screen.blit(sets.start,(0,0))
  49. elif state==PAUSE:
  50. screen.blit(sets.pause,(0,0))
  51. elif state==GAMEOVER:
  52. screen.blit(sets.gameover,(0,0))
  53. def blitmes():
  54. hero_blitme()
  55. flyings_blitme()
  56. bullets_blitme()
  57. score_blitme()
  58. state_blitme()
  59. def nextOne():
  60. type=random.randint(0,20)
  61. if type<4:
  62. return Bee(screen,sets.beeImage)
  63. elif type==5:
  64. return Bee(screen,sets.beeImage)
  65. else:
  66. return Airplane(screen,sets.airImage)
  67. flyEnteredIndex=0
  68. def enterAction():
  69. global flyEnteredIndex
  70. flyEnteredIndex+=1
  71. if flyEnteredIndex % 40 == 0:
  72. flyingobj=nextOne()
  73. flyings.append(flyingobj)
  74. shootIndex=0
  75. def shootAction():
  76. if shootIndex % 10 == 0:
  77. heroBullet=hero.shoot(sets.heroBullet)
  78. for bb in heroBullet:
  79. bullets.append(bb)
  80. def stepAction():
  81. hero.step()
  82. for flyobj in flyings:
  83. flyobj.step()
  84. global bullets
  85. for b in bullets:
  86. b.step()
  87. def outOfBoundAction():
  88. global flyings
  89. flyingLives=[]
  90. index=0
  91. for f in flyings:
  92. print (f)
  93. print ("lds:")
  94. if f.outOfBounds()==True:
  95. flyingLives.insert(index,f)
  96. index+=1
  97. flyings=flyingLives
  98. index=0
  99. global bullets
  100. bulletsLive=[]
  101. for b in bullets:
  102. if b.outOfBounds()==True:
  103. bulletsLive.insert(index,b)
  104. index+=1
  105. bullets=bulletsLive
  106. j=0
  107. def bangAction():
  108. for b in bullets:
  109. bang(b)
  110. def bang(b):
  111. index=-1
  112. for x in range(0,len(flyings)):
  113. f=flyings[x]
  114. if f.shootBy(b):
  115. index=x
  116. break
  117. if index != -1:
  118. one=flyings[index]
  119. if isinstance(one,Enemy):
  120. global score
  121. score+=one.getScore()
  122. flyings.remove(one)
  123. bullets.remove(b)
  124. def checkGameOverAction():
  125. if isGameOver():
  126. global state
  127. state=GAMEOVER
  128. hero.reLife()
  129. def isGameOver():
  130. for f in flyings:
  131. if hero.hit(f):
  132. hero.sublife()
  133. hero.clearDoubleFire()
  134. flyings.remove(f)
  135. return hero.getLife()<=0
  136. def action():
  137. x,y = pygame.mouse.get_pos()
  138. blitmes()
  139. for event in pygame.event.get():
  140. if event.type == pygame.QUIT:
  141. sys.exit()
  142. if event.type == pygame.MOUSEBUTTONDOWN:
  143. flag=pygame.mouse.get_pressed()[0]
  144. rflag=pygame.mouse.get_pressed()[2]
  145. global state
  146. if flag==True and (state==START or state==PAUSE):
  147. state=RUNNING
  148. if flag==True and state==GAMEOVER:
  149. state=START
  150. if rflag==True:
  151. state=PAUSE
  152. if state==RUNNING:
  153. hero.move(x,y)
  154. enterAction()
  155. shootAction()
  156. stepAction()
  157. outOfBoundAction()
  158. bangAction()
  159. checkGameOverAction()
  160. def main():
  161. pygame.display.set_caption("飞机大战")
  162. while True:
  163. print("screen blit...")
  164. screen.blit(sets.bgImage,(0,0))
  165. print("start action")
  166. action()
  167. print("display update")
  168. pygame.display.update()
  169. print("sleep 0.1")
  170. time.sleep(0.1)
  171. if __name__ == '__main__':
  172. main()