Add iterations and max fitness counters.

This commit is contained in:
Emil Shanaty
2025-04-14 20:13:47 +03:00
parent a7100082c7
commit 136f888631
2 changed files with 23 additions and 4 deletions
+3 -1
View File
@@ -3,6 +3,7 @@ import random
WIDTH, HEIGHT = 800, 600
HUD_HEIGHT = 150
BARRIER_SEED = 20
class GameObject:
x = 0
y = 0
@@ -186,6 +187,7 @@ class Spawner:
self.tick_counter = 0 # Reset tick counter
def spawn(self):
random_y = random.randint(0, HEIGHT - 100) # Random Y position for the barrier
random_y = random.randint(0, HEIGHT - 100, ) # Random Y position for the barrier
new_barrier = Barrier(self.barrier_image_path, WIDTH, random_y) # Create a new barrier
return new_barrier
+20 -3
View File
@@ -3,20 +3,26 @@ import sys
from gameobjects import *
from genetic_alg import GeneticAlgorithm
POPULATION_SIZE = 100
POPULATION_SIZE = 300
MUTATION_RATE = 0.5
POPULATION_NEW = 0.1
POPULATION_BEST = 0.3
FRAME_RATE = 300 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION
BARRIER_SPEED = 10
BARRIER_DELAY = 100
MAX_ITERATIONS = 50
current_iteration = 1
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT + HUD_HEIGHT)) # Initialize the screen
pygame.display.set_caption("NIC_Project") # Set window title
font = pygame.font.SysFont("Arial", 36)
WHITE = (255, 255, 255) # Define white color
BLACK = (0, 0, 0)
UI = UI("images/HUD.png") # Initialize UI
gameobjects = []
@@ -30,7 +36,7 @@ last_barrier = None
def init_game():
global gameobjects, horses, barriers, upper_bound_rect, lower_bound_rect, spawner, last_barrier, BARRIER_SPEED
random.seed(BARRIER_SEED)
grass = Background("images/Grass.jpg", 0, 0)
grass.set_size(WIDTH, HEIGHT)
@@ -125,15 +131,26 @@ while True:
for object in gameobjects:
object.draw(screen) # Draw all game objects
object.draw(screen) # Draw all game objects
if all(horse.stopped for horse in horses):
UI.iteration_num += 1
genecticAlg.learn([x.fitness for x in horses])
current_iteration += 1
if current_iteration == MAX_ITERATIONS:
pygame.quit()
sys.exit()
init_game()
UI.draw(screen)
UI.draw_marks(screen)
iteration_num_txt = font.render(f"iteration: {current_iteration}", True, BLACK)
best_fitness_txt = font.render(f"Best: {max(genecticAlg.fitnessBest) if len(genecticAlg.fitnessBest) != 0 else 0}",True, BLACK)
screen.blit(iteration_num_txt, (WIDTH/2 - 90, HEIGHT + HUD_HEIGHT - 120))
screen.blit(best_fitness_txt, (WIDTH/2 - 90, HEIGHT + HUD_HEIGHT - 80))
pygame.display.flip() # Update the display
pygame.time.Clock().tick(FRAME_RATE) # Limit the frame rate to 160 FPS