Esempio n. 1
0
def main_game(genomes, config):
  gameSpeed = 5
  gameOver = False

  nets = []
  genomes_track = []
  snakes = []

  for _, genome in genomes:
    net = neat.nn.FeedForwardNetwork.create(genome, config)
    nets.append(net)
    snake_initial_coord = unique_coords(snakes)
    snakes.append(Snake(snake_initial_coord, gameSpeed))
    genome.fitness = 0
    genomes_track.append(genome)

  #snake = Snake((int(BOARD_ROWS / 2), int(BOARD_COLS / 2)), gameSpeed)
  tile_coord = unique_coords(snakes)
  tile = Tile(tile_coord)
  board = Board(screen)
  score = Score(screen)
  
  #last_obstacle = pygame.sprite.Group()

  while not gameOver:

    for event in pygame.event.get():
      if event.type == QUIT:
        gameOver = True
        quit() 

      if event.type == KEYDOWN:
        if event.key == K_UP:
          for snake in snakes:
            snake.move_up()
        if event.key == K_DOWN:
          for snake in snakes:
            snake.move_down()
        if event.key == K_RIGHT:
          for snake in snakes:
            snake.move_right()
        if event.key == K_LEFT:
          for snake in snakes:
            snake.move_left()

    for snake in snakes:
      snake.update()

    check_collide(snakes, tile, genomes_track, nets)

    if tile.is_dead():
      score.update()
      #snake.eat_tile()
      tile_coord = unique_coords(snakes)
      tile = Tile(tile_coord)

    if len(snakes) == 0:
      gameOver = True
      quit() 

    board.clean_board()

    for snake in snakes:
      board.display_snake(snake.get_body())

    board.display_tile(tile.get_coord())

    update_fitness(snakes, tile, genomes_track, nets)

    if pygame.display.get_surface() != None:
      screen.fill(BG_COLOR)
      board.draw()
      score.draw()
      pygame.display.update()

    clock.tick(FPS)
Esempio n. 2
0
def main_game(genomes, config):
  gameSpeed = 4
  gameOver = False
  gameQuit = False

  ground = Ground(screen, -1 * gameSpeed)
  score = Score(screen)

  nets = []
  genomes_track = []
  trexs = []

  for _, genome in genomes:
    net = neat.nn.FeedForwardNetwork.create(genome, config)
    nets.append(net)
    trexs.append(TRex(screen, 44, 47))
    genome.fitness = 0
    genomes_track.append(genome)


  clouds = pygame.sprite.Group()
  cactus = pygame.sprite.Group()
  pteros = pygame.sprite.Group()
  last_obstacle = pygame.sprite.Group()

  Cloud.containers = clouds
  Cactus.containers = cactus
  Ptero.containers = pteros

  while not gameOver:
    for event in pygame.event.get():
      if event.type == QUIT:
        gameOver = True
        quit() 
        
      if event.type == KEYDOWN:
        if event.key == K_SPACE or event.key == K_UP:
          for trex in trexs:
            trex.jump()
        if event.key == K_DOWN:
          for trex in trexs:
            trex.duck()

      if event.type == KEYUP:
        for trex in trexs:
          trex.no_duck()

    
    check_cactus_collide(cactus, trexs, genomes_track, nets)
    check_pteros_collide(pteros, trexs, genomes_track, nets)

    create_cactus(cactus, last_obstacle, gameSpeed)
    create_ptero(pteros, last_obstacle, gameSpeed)
    create_clouds(clouds)

    for trex in trexs:
      trex.update()
    
    update_fitness(trexs, cactus, pteros, genomes_track, nets)

    ground.update()
    clouds.update()
    cactus.update()
    pteros.update()
    score.update()

    if pygame.display.get_surface() != None:
      screen.fill(BG_COLOR)
      ground.draw()
      clouds.draw(screen)
      cactus.draw(screen)
      pteros.draw(screen)
      score.draw()

      for trex in trexs:
        trex.draw()

      pygame.display.update()

    if (score.get_score() > 1000 and gameSpeed == 4) or \
      (score.get_score() > 2000 and gameSpeed == 4.5) or \
      (score.get_score() > 3000 and gameSpeed == 5) or \
      (score.get_score() > 4000 and gameSpeed == 5.5):
      gameSpeed += 0.5
      ground.set_speed(-1 * gameSpeed)
    
    if len(trexs) == 0:
      gameOver = True

    clock.tick(FPS)

  """
Esempio n. 3
0
def main_game(genomes, config):
  gameSpeed = 2
  gameOver = False

  nets = []
  genomes_track = []
  birds = []
  pipes_s = []

  for _, genome in genomes:
    net = neat.nn.FeedForwardNetwork.create(genome, config)
    nets.append(net)
    birds.append(Bird(screen))
    genome.fitness = 0
    genomes_track.append(genome)

  bg = BG(screen, -1 * gameSpeed)
  ground = Ground(screen, -1 * gameSpeed)
  score = Score(screen)

  pipes = pygame.sprite.Group()
  last_obstacle = pygame.sprite.Group()

  Pipe.containers = pipes

  while not gameOver:

    for event in pygame.event.get():
      if event.type == QUIT:
        gameOver = True
        quit() 

      if event.type == KEYDOWN:
        if event.key == K_SPACE or event.key == K_UP:
          for bird in birds:
            bird.jump()

    check_collide(birds, pipes, genomes_track, nets)

    create_pipe(pipes_s, last_obstacle, gameSpeed)

    for bird in birds:
      bird.update()
    
    update_fitness(birds, pipes_s, genomes_track, nets)

    bg.update()
    pipes.update()
    ground.update()
    score.update()

    if len(pipes) < len(pipes_s):
      pipes_s.pop(0)
      pipes_s.pop(0)

    if pygame.display.get_surface() != None:
      bg.draw()
      pipes.draw(screen)
      ground.draw()
      score.draw()
      
      for bird in birds:
        bird.draw()

        if DRAW_LINES:
          pygame.draw.line(screen, (255,0,0), 
            (bird.rect.left + bird.image.get_width()/2, bird.rect.top + bird.image.get_height()/2), 
            (pipes_s[0].rect.left + pipes_s[0].image.get_width()/2, pipes_s[0].rect.top), 5)
          pygame.draw.line(screen, (255,0,0), 
            (bird.rect.left + bird.image.get_width()/2, bird.rect.top + bird.image.get_height()/2), 
            (pipes_s[0].rect.left + pipes_s[1].image.get_width()/2, pipes_s[1].rect.bottom), 5)

      pygame.display.update()

    if len(birds) == 0:
      gameOver = True

    clock.tick(FPS)