PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/breedingpitauto.py

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