PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/~mission.py

https://github.com/josomebody/evotank
Python | 415 lines | 371 code | 25 blank | 19 comment | 125 complexity | 8ab1ab43aad44136c050e44750afb18d MD5 | raw file
  1. import signal
  2. from classes import *
  3. def mission():
  4. random.seed()
  5. print 'Initializing pygame'
  6. pygame.init()
  7. levelfiles = ['/levels/level1']
  8. levels = []
  9. currentlevel = 0
  10. currentroom= 0
  11. protobullet = projectile()
  12. protobot = robot()
  13. you = player()
  14. thisgame = gamedetails()
  15. print "Opening display 640x480 full-screen"
  16. screen = pygame.display.set_mode((640, 480), FULLSCREEN)
  17. pygame.display.set_caption('EvoTank')
  18. pygame.mouse.set_visible(False)
  19. gameconsole = console()
  20. print "Loading images..."
  21. font = pygame.font.SysFont("Terminal", 14)
  22. armorpic = pygame.image.load('images/armor.png').convert_alpha()
  23. ammopic = pygame.image.load('images/ammo.png').convert_alpha()
  24. fuelpic = pygame.image.load('images/fuel.png').convert_alpha()
  25. barrelpic = pygame.image.load('images/barrel.png').convert_alpha()
  26. cratepic = pygame.image.load('images/crate.png').convert_alpha()
  27. heattx = font.render('HEAT', True, (255, 255, 255))
  28. armortx = font.render('ARMOR', True, (255, 255, 255))
  29. fueltx = font.render('FUEL', True, (255, 255, 255))
  30. ammotx = font.render('AMMO', True, (255, 255, 255))
  31. throttlex = font.render('THROTTLE', True, (255, 255, 255))
  32. bulletpic = []
  33. goodtankpic = []
  34. badtankpic = []
  35. doorpic = []
  36. for i in range(8):
  37. picname = 'images/bullet' + str(i) + '.png'
  38. tempic = pygame.image.load(picname).convert_alpha()
  39. bulletpic.append(tempic)
  40. picname = 'images/goodtank' + str(i) + '.png'
  41. tempic = pygame.image.load(picname).convert_alpha()
  42. goodtankpic.append(tempic)
  43. picname = 'images/badtank' + str(i) + '.png'
  44. tempic = pygame.image.load(picname).convert_alpha()
  45. badtankpic.append(tempic)
  46. for i in range(4):
  47. picname = 'images/door' + str(i) + '.png'
  48. tempic = pygame.image.load(picname).convert_alpha()
  49. doorpic.append(tempic)
  50. gameconsole.write("Loading sounds...")
  51. bang = pygame.mixer.Sound('sounds/bang.wav')
  52. clank = pygame.mixer.Sound('sounds/clank.wav')
  53. ping = pygame.mixer.Sound('sounds/ping.wav')
  54. diesound = pygame.mixer.Sound('sounds/die.wav')
  55. lowammosound = pygame.mixer.Sound('sounds/ammo.wav')
  56. lowarmorsound = pygame.mixer.Sound('sounds/armor.wav')
  57. lowfuelsound = pygame.mixer.Sound('sounds/fuel.wav')
  58. overheatsound = pygame.mixer.Sound('sounds/heat.wav')
  59. latersound = pygame.mixer.Sound('sounds/later.wav')
  60. letsgosound = pygame.mixer.Sound('sounds/letsgo.wav')
  61. # protobot.dead = 0
  62. # protobot.hitstilmating = 5
  63. # protobot.state = 0
  64. # protobot.armor = maxarmor
  65. # protobot.fuel = maxfuel
  66. # protobot.ammo = maxammo
  67. # protobot.offspring = robot()
  68. # robots.append(protobot)
  69. gameconsole.write('Loading levels...')
  70. for i in range(len(levelfiles)):
  71. protolevel = level()
  72. protolevel = protolevel.load(levelfiles[i])
  73. #initialize game coordinates and shit
  74. currentlevel =
  75. ptypes = ['fuel', 'armor', 'ammo']
  76. robotstats = []
  77. robotstatspic = []
  78. bulletcount = 0
  79. lowfuelflag = 0
  80. lowammoflag = 0
  81. lowarmorflag = 0
  82. overheatflag = 0
  83. fps = [0]
  84. fpsavg = 0
  85. frametime = 0
  86. showfps = 1
  87. showconsole = 1
  88. showmeters = 1
  89. code_entry = []
  90. tooslow = 0
  91. dropframe = 0
  92. framedelay = 0
  93. firstframe = 1
  94. #main gameloop
  95. framestarttime = pygame.time.get_ticks()
  96. while 1:
  97. signal.signal(signal.SIGALRM, handler)
  98. signal.alarm(5)
  99. pygame.event.pump()
  100. if fpsavg > 70:
  101. framedelay = (1000 - 1000 / frametime) / 60
  102. #first of all, check for births
  103. for i in range(len(robots)):
  104. if robots[i].gestation == 1:
  105. robots.append(robots[i].givebirth())
  106. gameconsole.write(robots[i].name + ' has given birth to ' + robots[len(robots) - 1].name)
  107. #draw everybody
  108. if fpsavg < 20:
  109. tooslow = tooslow + 1
  110. if tooslow > 3:
  111. print 'Dropped frame (fps: ' + str(fps[len(fps) - 1]) + ') (average fps for last 100 frames:' + str(fpsavg) + ')'
  112. dropframe = 1
  113. tooslow = 0
  114. else:
  115. tooslow = 0
  116. dropframe = 0
  117. if dropframe == 1:
  118. gameconsole.write('frame dropped (' + str(frametime) + ')')
  119. gameconsole.blit(screen, font)
  120. else:
  121. screen.blit(background, (0, 0))
  122. for i in range(len(currentlevel.rooms[currentroom].pickups)):
  123. if currentlevel.rooms[currentroom].pickups[i].pickuptype == 'fuel':
  124. screen.blit(fuelpic, (currentlevel.rooms[currentroom].pickups[i].x, currentlevel.rooms[currentroom].pickups[i].y))
  125. if currentlevel.rooms[currentroom].pickups[i].pickuptype == 'ammo':
  126. screen.blit(ammopic, (currentlevel.rooms[currentroom].pickups[i].x, currentlevel.rooms[currentroom].pickups[i].y))
  127. if currentlevel.rooms[currentroom].pickups[i].pickuptype == 'armor':
  128. screen.blit(armorpic, (currentlevel.rooms[currentroom].pickups[i].x, currentlevel.rooms[currentroom].pickups[i].y))
  129. for i in range(len(currentlevel.rooms[currentroom].bullets)):
  130. screen.blit(bulletpic[currentlevel.rooms[currentroom].bullets[i].rotate], (currentlevel.rooms[currentroom].bullets[i].x, currentlevel.rooms[currentroom].bullets[i].y))
  131. for i in range(len(currentlevel.rooms[currentroom].robots)):
  132. screen.blit(badtankpic[currentlevel.rooms[currentroom].robots[i].rotate], (currentlevel.rooms[currentroom].robots[i].x, currentlevel.rooms[currentroom].robots[i].y))
  133. screen.blit(goodtankpic[you.rotate], (you.x, you.y))
  134. del robotstats[:]
  135. del robotstatspic[:]
  136. for i in range(len(currentlevel.rooms[currentroom].robots)):
  137. robotag = currentlevel.rooms[currentroom].robots[i].name + ' '
  138. if currentlevel.rooms[currentroom].robots[i].state == 10:
  139. robotag = robotag + '(***mating w/ ' + currentlevel.rooms[currentroom].robots[i].partner + '***)'
  140. if currentlevel.rooms[currentroom].robots[i].state == 11:
  141. robotag = robotag + '(***gestating***)'
  142. robotstats.append(robotag)
  143. for i in range(len(robotstats)):
  144. robotstatspic.append(font.render(robotstats[i], True, (255, 0, 0)))
  145. for i in range(len(currentlevel.rooms[currentroom].robots)):
  146. screen.blit(robotstatspic[i], (currentlevel.rooms[currentroom].robots[i].x, currentlevel.rooms[currentroom].robots[i].y))
  147. if showconsole == 1:
  148. gameconsole.blit(screen, font)
  149. if showmeters == 1:
  150. pygame.draw.rect(screen, (64, 0, 0), (0, 470, you.heat / 20, 10))
  151. pygame.draw.rect(screen, (0, 0, 64), (110, 470, you.armor, 10))
  152. pygame.draw.rect(screen, (0, 64, 0), (220, 470, (you.fuel / 50), 10))
  153. pygame.draw.rect(screen, (32, 32, 64), (330, 470, you.ammo, 10))
  154. if you.velocity < -1:
  155. pygame.draw.rect(screen, (64, 64, 0), (450, 470, 25, 10))
  156. if you.velocity < 0:
  157. pygame.draw.rect(screen, (64, 64, 0), (475, 470, 25, 10))
  158. if you.velocity > 0:
  159. pygame.draw.rect(screen, (64, 64, 0), (500, 470, 25, 10))
  160. if you.velocity > 1:
  161. pygame.draw.rect(screen, (64, 64, 0), (525, 470, 25, 10))
  162. screen.blit(heattx, (0, 470))
  163. screen.blit(armortx, (110, 470))
  164. screen.blit(fueltx, (220, 470))
  165. screen.blit(ammotx, (330, 470))
  166. screen.blit(throttlex, (465, 470))
  167. if showfps == 1:
  168. fpsstring = 'fps: ' + str(fpsavg)
  169. fpspic = font.render(fpsstring, True, (255, 128, 0))
  170. screen.blit(fpspic, (640 - len(fpsstring) * 7, 0))
  171. dropframe = 0
  172. if firstframe == 1:
  173. letsgopic = font.render('LET\'S GO!', True, (255, 255, 255))
  174. screen.blit(letsgopic, (257, 233))
  175. pygame.display.flip()
  176. letsgosound.play(0)
  177. for killsometime in range(10):
  178. screen.blit(badtankpic[you.rotate], (you.x, you.y))
  179. pygame.display.flip()
  180. pygame.time.wait(50)
  181. screen.blit(goodtankpic[you.rotate], (you.x, you.y))
  182. pygame.display.flip()
  183. pygame.time.wait(50)
  184. firstframe = 0
  185. pygame.display.flip()
  186. #everybody gets a turn
  187. for event in pygame.event.get():
  188. if event.type == KEYDOWN:
  189. if event.key == K_q:
  190. gameconsole.write('SELF-DESTRUCT')
  191. you.armor = 0
  192. elif event.key == K_f:
  193. if showfps == 0:
  194. showfps = 1
  195. elif showfps == 1:
  196. showfps = 0
  197. else:
  198. showfps = 0
  199. elif event.key == K_c:
  200. if showconsole == 0:
  201. showconsole = 1
  202. else:
  203. showconsole = 0
  204. elif event.key == K_m:
  205. if showmeters == 0:
  206. showmeters = 1
  207. else:
  208. showmeters = 0
  209. elif (event.key == K_UP) or (event.key == K_DOWN) or (event.key == K_LEFT) or (event.key == K_RIGHT) or (event.key == K_SPACE):
  210. you.control(event.key, currentlevel.rooms[currentroom].bullets, bang)
  211. else:
  212. code_entry.append(decoderkey(event.key))
  213. if pygame.time.get_ticks() % 2000 == 0:
  214. if len(entry_code) > 0:
  215. del code_entry[0]
  216. if code_entry == ['i', 'd']:
  217. del code_entry[:]
  218. gameconsole.write('this ain\'t doom, moron!')
  219. for i in range(15):
  220. punishment = open('savedbots/cicte', 'r')
  221. protobot = pickle.load(punishment)
  222. punishment.close
  223. protobot.x = you.x
  224. protobot.y = you.y
  225. protobot.dead = 0
  226. protobot.state = 0
  227. protobot.gestation = 0
  228. currentlevel.rooms[currentroom].robots.append(protobot)
  229. if code_entry == ['j', 'j', 'g', 'u', 'n']:
  230. del code_entry[:]
  231. gameconsole.write('ammo cheat')
  232. you.ammo = maxammo
  233. if code_entry == ['j', 'j', 'g', 'a', 's']:
  234. del code_entry[:]
  235. gameconsole.write('fuel cheat')
  236. you.fuel = maxfuel
  237. if code_entry == ['j', 'j', 's', 'u', 'i', 't']:
  238. del code_entry[:]
  239. gameconsole.write('armor cheat')
  240. you.armor = maxarmor
  241. if code_entry == ['j', 'j', 'k', 'o', 'o', 'l']:
  242. del code_entry[:]
  243. gameconsole.write('heat cheat')
  244. you.heat = 0
  245. if code_entry == ['j', 'j', 'k', 'i', 'l', 'l', 'a', 'l', 'l']:
  246. del code_entry[:]
  247. gameconsole.write('massacre cheat')
  248. for i in range(len(currentlevel.rooms[currentroom].robots)-1):
  249. currentlevel.rooms[currentroom].robots[i].armor = 1
  250. for i in range(len(currentlevel.rooms[currentroom].robots)):
  251. currentlevel.rooms[currentroom].robots[i].letsgo(currentlevel.rooms[currentroom].bullets, currentlevel.rooms[currentroom].pickups, currentlevel.rooms[currentroom].robots, you, bang, i)
  252. #move everybody
  253. if dropframe != 1:
  254. for i in range(len(currentlevel.rooms[currentroom].bullets)):
  255. currentlevel.rooms[currentroom].bullets[i].move()
  256. for i in range(len(currentlevel.rooms[currentroom].robots)):
  257. currentlevel.rooms[currentroom].robots[i].move()
  258. you.move() #if your machine is going too slow, you get bullet-time!
  259. #and now the expensive part: collision checks and shit
  260. if dropframe != 1:
  261. for i in range(len(currentlevel.rooms[currentroom].bullets)):
  262. if currentlevel.rooms[currentroom].bullets[i].offscreen() == 1:
  263. currentlevel.rooms[currentroom].bullets[i].dead = 1
  264. for i in range(len(currentlevel.rooms[currentroom].bullets)):
  265. if (detect_collision(currentlevel.rooms[currentroom].bullets[i], currentlevel.rooms[currentroom].bulletsize, you, tanksize) == 1) and (currentlevel.rooms[currentroom].bullets[i].originator != you.name):
  266. you.armor = you.armor - 5
  267. for j in range(len(currentlevel.rooms[currentroom].robots)):
  268. if currentlevel.rooms[currentroom].bullets[i].originator == currentlevel.rooms[currentroom].robots[j].name:
  269. currentlevel.rooms[currentroom].robots[j].hits = currentlevel.rooms[currentroom].robots[j].hits + 1
  270. currentlevel.rooms[currentroom].robots[j].hitstilmating = currentlevel.rooms[currentroom].robots[j].hitstilmating - 1
  271. if currentlevel.rooms[currentroom].robots[j].hitstilmating < 0:
  272. currentlevel.rooms[currentroom].robots[j].hitstilmating = 5
  273. gameconsole.write((currentlevel.rooms[currentroom].robots[j].name + ' scored hit #' + str(currentlevel.rooms[currentroom].robots[j].hits)))
  274. currentlevel.rooms[currentroom].bullets[i].dead = 1
  275. clank.play(0)
  276. for i in range(len(currentlevel.rooms[currentroom].bullets)):
  277. for j in range(len(currentlevel.rooms[currentroom].robots)):
  278. if (detect_collision(currentlevel.rooms[currentroom].bullets[i], bulletsize, currentlevel.rooms[currentroom].robots[j], tanksize) == 1) and (currentlevel.rooms[currentroom].bullets[i].originator != currentlevel.rooms[currentroom].robots[j].name):
  279. currentlevel.rooms[currentroom].robots[j].armor = currentlevel.rooms[currentroom].robots[j].armor - 5
  280. currentlevel.rooms[currentroom].bullets[i].dead = 1
  281. clank.play(0)
  282. for i in range(len(currentlevel.rooms[currentroom].pickups)):
  283. if detect_collision(currentlevel.rooms[currentroom].pickups[i], pickupsize, you, tanksize) == 1:
  284. if you.cangetpickup(currentlevel.rooms[currentroom].pickups[i].pickuptype):
  285. you.getpickup(currentlevel.rooms[currentroom].pickups[i].pickuptype)
  286. currentlevel.rooms[currentroom].pickups[i].dead = 1
  287. ping.play(0)
  288. for i in range(len(currentlevel.rooms[currentroom].pickups)):
  289. for j in range(len(currentlevel.rooms[currentroom].robots)):
  290. if detect_collision(currentlevel.rooms[currentroom].pickups[i], pickupsize, currentlevel.rooms[currentroom].robots[j], tanksize) == 1:
  291. if currentlevel.rooms[currentroom].robots[j].cangetpickup(currentlevel.rooms[currentroom].pickups[i].pickuptype):
  292. currentlevel.rooms[currentroom].robots[j].getpickup(currentlevel.rooms[currentroom].pickups[i].pickuptype)
  293. currentlevel.rooms[currentroom].pickups[i].dead = 1
  294. ping.play(0)
  295. for i in range(len(currentlevel.rooms[currentroom].robots)):
  296. if detect_collision(currentlevel.rooms[currentroom].robots[i], tanksize, you, tanksize) == 1:
  297. bounce(currentlevel.rooms[currentroom].robots[i], you)
  298. clank.play(0)
  299. for j in range(len(currentlevel.rooms[currentroom].robots)):
  300. if i != j:
  301. if detect_collision(currentlevel.rooms[currentroom].robots[i], tanksize, currentlevel.rooms[currentroom].robots[j], tanksize) == 1:
  302. bounce(currentlevel.rooms[currentroom].robots[i], currentlevel.rooms[currentroom].robots[j])
  303. clank.play(0)
  304. #and now for the sad part: coping with death
  305. if you.armor <= 0:
  306. diesound.play(0)
  307. screen.blit(background,(0,0))
  308. gameconsole.write('you got pwned. continue? y/N')
  309. gameconsole.blit(screen, font)
  310. pygame.display.flip()
  311. pygame.time.wait(1000)
  312. choicekey = 0
  313. signal.alarm(0)
  314. while choicekey != K_y:
  315. for event in pygame.event.get():
  316. if event.type == KEYDOWN:
  317. choicekey = event.key
  318. if event.key == K_y:
  319. signal.alarm(5)
  320. you.armor = 100
  321. firstframe = 1
  322. gameconsole.write('continuing after getting pwned')
  323. elif event.key == K_n:
  324. latersound.play(0)
  325. pygame.time.wait(1500)
  326. return 0
  327. for i in range(len(currentlevel.rooms[currentroom].robots)):
  328. if currentlevel.rooms[currentroom].robots[i].armor <=0:
  329. currentlevel.rooms[currentroom].robots[i].dead = 1
  330. gameconsole.write(currentlevel.rooms[currentroom].robots[i].name + ' died.')
  331. #and getting rid of the bodies
  332. if dropframe != 1:
  333. for i in range(len(currentlevel.rooms[currentroom].robots)):
  334. if i < len(currentlevel.rooms[currentroom].robots):
  335. if currentlevel.rooms[currentroom].robots[i].dead == 1:
  336. del currentlevel.rooms[currentroom].robots[i]
  337. diesound.play(0)
  338. for i in range(len(currentlevel.rooms[currentroom].bullets)):
  339. if i < len(currentlevel.rooms[currentroom].bullets):
  340. if currentlevel.rooms[currentroom].bullets[i].dead == 1:
  341. del currentlevel.rooms[currentroom].bullets[i]
  342. for i in range(len(currentlevel.rooms[currentroom].pickups)):
  343. if i < len(currentlevel.rooms[currentroom].pickups):
  344. if currentlevel.rooms[currentroom].pickups[i].dead ==1:
  345. del currentlevel.rooms[currentroom].pickups[i]
  346. #and playing game warning sounds
  347. if you.armor < 50:
  348. if lowarmorflag == 0:
  349. lowarmorsound.play(0)
  350. lowarmorflag = 1
  351. else:
  352. lowarmorflag = 0
  353. if you.ammo < 10:
  354. if lowammoflag == 0:
  355. lowammosound.play(0)
  356. lowammoflag = 1
  357. else:
  358. lowammoflag = 0
  359. if you.fuel < 1000:
  360. if lowfuelflag == 0:
  361. lowfuelsound.play(0)
  362. lowfuelflag = 1
  363. else:
  364. lowfuelflag = 0
  365. if you.heat > 1000:
  366. if overheatflag == 0:
  367. overheatsound.play(0)
  368. overheatflag = 1
  369. elif you.heat < 950:
  370. overheatflag = 0
  371. #get the fps so we can act accordingly
  372. if (framedelay > 1) and (framedelay < 16):
  373. pygame.time.wait(framedelay)
  374. frametime = pygame.time.get_ticks() - framestarttime
  375. if frametime > 0: #you never know. if a frame is executed in less than 1 ms, don't worry about it
  376. fps.append(1000 / frametime)
  377. if len(fps) > 100:
  378. del fps[0]
  379. fpsavg = sum(fps) / len(fps)
  380. framestarttime = pygame.time.get_ticks()
  381. signal.alarm(0)
  382. def handler(signum, frame):
  383. print "CRASH! Frame got stuck!"
  384. raise SystemExit
  385. def main():
  386. startagain = 1
  387. while startagain == 1:
  388. startagain = mission()
  389. raise SystemExit
  390. main()