Fix marks correspondance.

This commit is contained in:
Emil Shanaty
2025-04-11 00:34:54 +03:00
parent e6735af700
commit 766e6fb7e2
2 changed files with 58 additions and 51 deletions
+28 -25
View File
@@ -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
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 # 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 = 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)
+20 -16
View File
@@ -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,20 +44,22 @@ 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],
@@ -69,9 +68,14 @@ def get_features(horse):
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
@@ -81,7 +85,7 @@ while True:
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