def test_game_max_points_threshold(): def game_representation_strategy(game): return Game.get_full_game_representation_strategy(game) snake_length = 5 width = 32 height = 18 snack_eaten_points = 2 input_nodes = width * height + 4 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 max_points_threshold = 200 min_points_threshold = -10 sample_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) sample_phenotype = Phenotype(sample_genotype.weights, input_nodes, hidden_layer_nodes, output_nodes) sample_game = Game(width, height, sample_phenotype, 777, game_representation_strategy, snake_length, snack_eaten_points, max_points_threshold=max_points_threshold, min_points_threshold=min_points_threshold) sample_game.score = 200 next_game_state = Game.get_next_game(sample_game) assert next_game_state.status == GameStatus.ENDED next_game_state.score = -10 next_game_state = Game.get_next_game(next_game_state) assert next_game_state.status == GameStatus.ENDED
def test_snake_points_assignment(): def game_representation_strategy(game): return Game.get_full_game_representation_strategy(game) snake_length = 5 width = 32 height = 18 snack_eaten_points = 2 input_nodes = width * height + 4 hidden_layer_nodes = [64] output_nodes = 3 number_of_nn_weights = get_number_of_nn_weights(input_nodes, hidden_layer_nodes, output_nodes) weight_lower_threshold = -1 weight_upper_threshold = 1 sample_genotype = SimpleGenotype.get_random_genotype( number_of_nn_weights, weight_lower_threshold, weight_upper_threshold) sample_phenotype = Phenotype(sample_genotype.weights, input_nodes, hidden_layer_nodes, output_nodes) sample_game = Game(width, height, sample_phenotype, 777, game_representation_strategy, snake_length, snack_eaten_points) # Hack snack position # Initial snake blocks are [(16, 9), (17, 9), (18, 9), (19, 9), (20, 9)] # So we put snack on snake head sample_game.snack = (16, 9) next_game_object = Game.get_next_game(sample_game) assert next_game_object.score == snack_eaten_points assert next_game_object.snack != (16, 9) assert next_game_object.snack is not None assert len(next_game_object.snake) == snake_length + 1 assert next_game_object.snack_perspective is not None
screen = pygame.display.set_mode((game_screen_width, game_screen_height)) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() # Print game screen.fill(background_color) for index, selected_snake_block in enumerate(game.snake): block_alpha = 255 / (index + 1) snake_block = pygame.Surface((1 * scale, 1 * scale)) snake_block.set_alpha(block_alpha) snake_block.fill(snake_color) top_x = selected_snake_block[0] * scale top_y = selected_snake_block[1] * scale screen.blit(snake_block, (top_x, top_y)) snack_block = pygame.Surface((1 * scale, 1 * scale)) snack_block.fill(snack_color) top_x = game.snack[0] * scale top_y = game.snack[1] * scale screen.blit(snack_block, (top_x, top_y)) pygame.display.update() pygame.time.wait(delay) game = Game.get_next_game(game)