class ShardSprite(pygame.sprite.Sprite): scene: 'GameScene' shard: Shard image: Surface rect: Rect hitbox: Rect animation: Animation def __init__(self, scene: 'GameScene', shard: Shard, *groups: AbstractGroup): super().__init__(*groups) self.scene = scene self.shard = shard scale_factor = self.scene.settings.scale_factor # load images and create looping animation frames = load_scaled_surfaces(ANIM_SHARD_IDLE, scale_factor) self.image = frames[0] self.animation = Animation(frames, 75, loop=True) self.animation.start(pygame.time.get_ticks()) center_of_first_tile = ((TILE_SIZE_PX // 2) * scale_factor, (TILE_SIZE_PX // 2) * scale_factor) # set rect and hitbox self.rect = self.image.get_rect(center=center_of_first_tile) self.hitbox = self.rect.copy() self.hitbox.inflate_ip(-3 * scale_factor, -3 * scale_factor) def update(self, *args, **kwargs) -> None: self._update_rectangle_based_on_current_position() self.image = self.animation.get_image(pygame.time.get_ticks()) def _update_rectangle_based_on_current_position(self): scale_factor = self.scene.settings.scale_factor position = self.shard.position x = (position.x * TILE_SIZE_PX) + (TILE_SIZE_PX // 2) x = x * scale_factor y = ((position.y - self.scene.vertical_shift) * TILE_SIZE_PX) + (TILE_SIZE_PX // 2) y = y * scale_factor self.rect.center = (x, y) self.hitbox.center = (x, y)
def test_animation_should_loop_forever(self): dummy_frames = ["0", "1", "2"] dummy_delay = 10 a = Animation(dummy_frames, dummy_delay, loop=True) # start animation at 100-th millisecond a.start(100) self.assertFalse(a.is_over()) self.assertEqual("0", a.get_image(100)) self.assertEqual("0", a.get_image(101)) self.assertFalse(a.is_over()) self.assertEqual("2", a.get_image(120)) self.assertEqual("2", a.get_image(129)) self.assertFalse(a.is_over()) self.assertEqual("0", a.get_image(130)) self.assertFalse(a.is_over()) self.assertEqual("1", a.get_image(145)) self.assertFalse(a.is_over()) self.assertEqual("2", a.get_image(150)) self.assertFalse(a.is_over()) self.assertEqual("1", a.get_image(410)) self.assertFalse(a.is_over())
def the_tsp_problem(): # Initialize modules start_timer("setup") x_large_data = "../data/TSP_Italy_16862.txt" big_data = "../data/TSP_Canada_4663.txt" middle_data = "../data/TSP_Uruguay_734.txt" small_data = "../data/TSP_WesternSahara_29.txt" actual_data = parse(middle_data) # Create Instance tsp = TSP(graph=actual_data, population_size=POP_SIZE, mating_pool_size=MATING_POOL_SIZE, mutation_rate=MUTATION_RATE, num_generations=NUM_GENERATIONS) # Initialize modules initializer = Initialization(tsp, INIT_METHOD) parent_selector = Parent_Selection(tsp, SELECT_METHOD) mutator = Mutation(tsp, MUTATION_METHOD) evaluator = Evaluation(tsp, EVALUATION_METHOD) recombinator = Recombination(tsp, CROSSOVER_METHOD, evaluator) survivor_selector = Survivor_Selection(tsp, SURVIVOR_METHOD) terminator = Termination(NUM_GENERATIONS, TIME_LIMIT, TERMINATOR_METHOD) # Initialize Population and fitness initializer.initialize() evaluator.evaluate() end_timer("setup") print("*" * 20) print("Initial Mean Fitness: {}\t Best Fitness:{}".format( tsp.fitness.mean(), tsp.fitness.max())) # print("Best initial member of Population:\n", tsp.population[np.argmax(tsp.fitness)]) print("*" * 20) current_time = 0 fitness = [] generation = [] while terminator.method(tsp.current_generation, current_time): # select parents and spawn children parent_selector.select() recombinator.recombine() # mutate population and children mutator.mutate_population() mutator.mutate_children() # re-evaluate children and population evaluator.evaluate(use_mask=True) evaluator.evaluate_children() # select from parents and children to form new population survivor_selector.select() # add history and print debugs every 10% tsp.add_history("mean_fitness", tsp.fitness.mean()) tsp.add_history("best_fitness", tsp.fitness.max()) std = tsp.fitness.std() tsp.add_history("std_dev", std) tsp.current_generation += 1 if not (tsp.current_generation % (tsp.num_generations // 10)): # print("Mutation Rate:",tsp.mutation_rate) print( "Generation {:<4} Mean Fitness: {:5.2f}\t Best Fitness:{:5.2f}\t STD DEV: {:.2f}" .format(tsp.current_generation, tsp.fitness.mean(), tsp.fitness.max(), std)) tsp.add_history("best_individual", tsp.population[np.argmax(tsp.fitness.max())]) fitness.append(tsp.fitness.max()) generation.append(tsp.current_generation) # If animation is set to true if ANIMATE: animator = Animation(actual_data, tsp.history["best_individual"], fitness, generation) animator.start() # finished, print results print("*" * 20) # print("Best Member of Population:\n", tsp.population[np.argmax(tsp.fitness)]) print("Final Mean Fitness: {}\t Best Fitness:{}".format( tsp.fitness.mean(), tsp.fitness.max())) print("*" * 10 + "\nFunction Times (in ms):\n") time_sum = 0 for k, v in get_times(): print("{:16}\t{:.2f}".format(k, v * 1000)) time_sum += v print("-" * 20) print("Total Time:\t{:.2f} seconds".format(time_sum)) # plot history tsp.plot_history("mean_fitness") tsp.plot_history("best_fitness") tsp.plot_history("std_dev")