diff --git a/gameobjects.py b/gameobjects.py index d433158..9c24911 100644 --- a/gameobjects.py +++ b/gameobjects.py @@ -131,36 +131,39 @@ class UI: def add_horses(self, children): new_horses = [child for child in children if child not in self.horses] # Add new children self.horses.extend(new_horses) + def draw_marks(self, screen): - active_marks = [] # List of active horses - unactive_marks = [] # List of inactive horses - for i in range(len(self.horses)): - if isinstance(self.horses[i], Horse): - if (self.horses[i].stopped == False): - active_marks.append(self.horses[i]) # Add active horses - else: - unactive_marks.append(self.horses[i]) # Add inactive horses - + active_marks = [horse for horse in self.horses if not horse.stopped] + unactive_marks = [horse for horse in self.horses if horse.stopped] + width = 10 # Number of marks per row - active_marks_section_pos = (0 - self.mark_size, HEIGHT + HUD_HEIGHT - self.mark_size) # Starting position for active marks - xa = active_marks_section_pos[0] - ya = active_marks_section_pos[1] - for i in range(len(active_marks)): - xa += self.horizontal_offset - pygame.draw.rect(screen, self.horses[i].color, (xa, ya, self.mark_size, self.mark_size)) # Draw active marks - if xa >= width * (self.mark_size - 3): # Move to the next row if the row is full + + # Отрисовка активных меток + xa = 0 # Horizontal position for active marks + ya = HEIGHT + HUD_HEIGHT - self.mark_size # Vertical position for active marks + + for i, horse in enumerate(active_marks): + pygame.draw.rect(screen, horse.color, (xa, ya, self.mark_size, self.mark_size)) + xa += self.horizontal_offset # Move to the next position + + # Move to the next row if the row is full + if (i + 1) % width == 0: # Check if the next mark would be on a new row ya += self.vertical_offset - xa = active_marks_section_pos[0] - - unactive_marks_section_pos = (WIDTH, HEIGHT + HUD_HEIGHT - self.mark_size) # Starting position for inactive marks - xu = unactive_marks_section_pos[0] - yu = unactive_marks_section_pos[1] - for i in range(len(unactive_marks)): - xu -= self.horizontal_offset - pygame.draw.rect(screen, unactive_marks[i].color, (xu, yu, self.mark_size, self.mark_size)) # Draw inactive marks - if WIDTH - xu >= width * self.mark_size: # Move to the next row if the row is full + xa = 0 # Reset horizontal position for the new row + + # Отрисовка неактивных меток + xu = WIDTH - self.mark_size # Horizontal position for inactive marks + yu = HEIGHT + HUD_HEIGHT - self.mark_size # Vertical position for inactive marks + + for i, horse in enumerate(unactive_marks): + pygame.draw.rect(screen, horse.color, (xu, yu, self.mark_size, self.mark_size)) + xu -= self.horizontal_offset # Move to the next position + + # Move to the next row if the row is full + if (i + 1) % width == 0: # Check if the next mark would be on a new row yu += self.vertical_offset - xu = unactive_marks_section_pos[0] + xu = WIDTH - self.mark_size # Reset horizontal position for the new row + def draw(self, surface): surface.blit(self.hud_image, self.hud_rect) diff --git a/main.py b/main.py index 8fccbef..4163487 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ POPULATION_SIZE = 50 MUTATION_RATE = 0.5 POPULATION_NEW = 0.1 POPULATION_BEST = 0.2 -FRAME_RATE = 160 #TODO: FIX THE INCORRECT FRAME RATE CORRELATION +FRAME_RATE = 160 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION BARRIER_SPEED = 10 BARRIER_DELAY = 100 @@ -22,24 +22,21 @@ UI = UI("images/HUD.png") # Initialize UI gameobjects = [] horses = [] barriers = [] - upper_bound_rect = None lower_bound_rect = None - spawner = None last_barrier = None + def init_game(): global gameobjects, horses, barriers, upper_bound_rect, lower_bound_rect, spawner, last_barrier, BARRIER_SPEED grass = Background("images/Grass.jpg", 0, 0) grass.set_size(WIDTH, HEIGHT) - gameobjects = [] - + gameobjects = [grass] barriers = [] - gameobjects = [grass] for horse in horses: horse.set_position(50, HEIGHT/2) horse.stopped = False @@ -47,31 +44,38 @@ def init_game(): horse.set_vspeed(0) horse.frame_counter = 0 horse.fitness = 0 + spawner = Spawner("images/Barrier.png", BARRIER_DELAY) new_barrier = spawner.spawn() barriers.append(new_barrier) + gameobjects.extend(horses) gameobjects.extend(barriers) - + upper_bound_rect = pygame.Rect(0, 0, WIDTH, -10) lower_bound_rect = pygame.Rect(0, HEIGHT, WIDTH, 10) - last_barrier = barriers[0] UI.add_horses(horses) + def get_features(horse): features = [last_barrier.rect.topleft[0], last_barrier.rect.topleft[1], - last_barrier.rect.bottomright[0], last_barrier.rect.bottomright[1], - horse.rect.topleft[0], horse.rect.topleft[1], - horse.rect.bottomright[0], horse.rect.bottomright[1], - 0, HEIGHT, BARRIER_SPEED] - return features + last_barrier.rect.bottomright[0], last_barrier.rect.bottomright[1], + horse.rect.topleft[0], horse.rect.topleft[1], + horse.rect.bottomright[0], horse.rect.bottomright[1], + 0, HEIGHT, BARRIER_SPEED] + return features + horses = [Horse("images/Horse_1.png", 50, HEIGHT/2 + 0 * i) for i in range(POPULATION_SIZE)] + init_game() -genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, POPULATION_NEW, len(get_features(horses[0]))) + +genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, POPULATION_NEW, + len(get_features(horses[0]))) + while True: for event in pygame.event.get(): if event.type == pygame.QUIT: # Handle window close event @@ -79,9 +83,9 @@ while True: sys.exit() keys = pygame.key.get_pressed() # Get pressed keys - + for i, horse in enumerate(horses): - if horse.stopped == False: # If the horse is not stopped + if not horse.stopped: # If the horse is not stopped data = get_features(horse) res = genecticAlg.predict(data, i) if res == 0: @@ -109,26 +113,26 @@ while True: for horse in horses: horse.update_animation() - if horse.stopped == False: # Check for collisions between horses and barriers + + if not horse.stopped: for barrier in barriers: - if horse.rect.colliderect(barrier.rect): # If collision detected - horse.stop() # Stop the horse + if horse.rect.colliderect(barrier.rect): + horse.stop() if horse.rect.colliderect(upper_bound_rect) or horse.rect.colliderect(lower_bound_rect): horse.stop() horse.count_fitness() - # print(str(horse.color) + ': ' + str(horse.fitness)) - + for object in gameobjects: object.draw(screen) # Draw all game objects - UI.draw(screen) - UI.draw_marks(screen) # Draw UI marks - if all(horse.stopped for horse in horses): UI.iteration_num += 1 genecticAlg.learn([x.fitness for x in horses]) init_game() + UI.draw(screen) + UI.draw_marks(screen) + pygame.display.flip() # Update the display - pygame.time.Clock().tick(FRAME_RATE) # Limit the frame rate to 160 FPS \ No newline at end of file + pygame.time.Clock().tick(FRAME_RATE) # Limit the frame rate to 160 FPS