Guest User

Untitled

a guest
May 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. from __future__ import division
  2. import math, sys, os, time, random
  3. import pygame
  4. from pygame import *
  5.  
  6. from Tank import Tank
  7. from Objects import Food
  8.  
  9. class Game(object):
  10.  
  11.     NUM_OF_FOOD = 50
  12.     NUM_OF_TANK = 40
  13.     NUM_OF_WINNERS = 5
  14.     MUTATION_RATE = 0.15 # Out of 100
  15.     ROUND_TIME = 120 # in seconds
  16.  
  17.     def __init__(self):
  18.         self.window = pygame.display.set_mode((1280, 1024))
  19.         self.surface = pygame.display.get_surface()
  20.         self.entitylist = []
  21.  
  22.         self.clock = pygame.time.Clock()
  23.         self.clock.tick(200)
  24.         self.time = 0
  25.         self.generation = 1
  26.  
  27.         self.text_renderer = pygame.font.Font(None, 25)
  28.  
  29.         for i in range(self.NUM_OF_FOOD):
  30.             self.createfood()
  31.  
  32.         for i in range(self.NUM_OF_TANK):
  33.             x = random.randint(0, self.surface.get_width())
  34.             y = random.randint(0, self.surface.get_height())
  35.             tank = Tank(self, x, y)
  36.  
  37.     def createfood(self):
  38.         x = random.randint(0, self.surface.get_width())
  39.         y = random.randint(0, self.surface.get_height())
  40.         food = Food(self, x, y)
  41.  
  42.     def restartmatch(self):
  43.         highscores = self.get_highscores()
  44.  
  45.         for tank in highscores:
  46.             highscores[highscores.index(tank)] = tank.brain.weights
  47.  
  48.         for entity in self.entitylist: # Destroy everything
  49.             entity.destroy(self)
  50.  
  51.         for i in range(self.NUM_OF_TANK):
  52.             weightlist = []
  53.             for weight in highscores[random.randint(0, self.NUM_OF_WINNERS-1)]:# Take a brain from a random winner
  54.                 if random.random()*100 <= self.MUTATION_RATE:
  55.                     weight += 1-(random.random()*2)
  56.                 weightlist.append(weight)
  57.  
  58.             x = random.randint(0, self.surface.get_width())
  59.             y = random.randint(0, self.surface.get_height())
  60.             tank = Tank(self, x, y, weightlist)
  61.  
  62.         self.time = 0
  63.         self.generation += 1
  64.  
  65.     def run(self):
  66.         for entity in self.entitylist:
  67.             entity.step(self)
  68.  
  69.         self.draw()
  70.  
  71.         print len(self.entitylist)
  72.  
  73.         self.clock.tick(200)
  74.         self.time += self.clock.get_time()
  75.         if self.time > self.ROUND_TIME*1000:
  76.             self.restartmatch()
  77.  
  78.     def get_highscores(self):
  79.         highscores = []
  80.         for entity in self.entitylist:
  81.             if isinstance(entity, Tank):
  82.                 if len(highscores) < self.NUM_OF_WINNERS:# If the highscores aren't even filled, no point in comparing
  83.                     highscores.append(entity)
  84.                 else:
  85.                     for tank in highscores:
  86.                         if entity.score > tank.score:# If the new tank was more successfull then the old one
  87.                             highscores[highscores.index(tank)] = entity# Replace it
  88.                             break
  89.         return highscores
  90.  
  91.     def draw(self):
  92.         self.surface.fill((0, 0, 0))# Fill the screen with white
  93.         for entity in self.entitylist:
  94.             entity.draw(self)# self == game
  95.  
  96.         rendered_text = self.text_renderer.render("Generation: "+str(self.generation)+" Time left until change:"+str(round((self.ROUND_TIME*1000-self.time)/1000)), 1, (0, 0, 255))
  97.         textpos = rendered_text.get_rect(centerx=self.window.get_width()/2)
  98.         self.surface.blit(rendered_text, textpos)
  99.  
  100.         pygame.display.flip()
Add Comment
Please, Sign In to add comment