Add game loop. Add fitness counter

This commit is contained in:
Emil Shanaty
2025-04-07 19:43:54 +03:00
parent 0ea4f47eaa
commit 7f95624dc9
2 changed files with 43 additions and 21 deletions
+1
View File
@@ -117,6 +117,7 @@ class Barrier(GameObject):
self.scale_by(5)
class UI:
iteration_num = 0
horses = [] # List of child objects
horizontal_offset = 25 # Horizontal spacing between marks
vertical_offset = -25 # Vertical spacing between marks
+42 -21
View File
@@ -3,9 +3,10 @@ import sys
from gameobjects import *
from genetic_alg import GeneticAlgorithm
POPULATION_SIZE = 50
POPULATION_SIZE = 5
MUTATION_RATE = 0.5
POPULATION_BEST = 0.2
FRAME_RATE = 160
pygame.init()
@@ -15,39 +16,55 @@ pygame.display.set_caption("NIC_Project") # Set window title
WHITE = (255, 255, 255) # Define white color
UI = UI() # Initialize UI
grass = Background("images/Grass.jpg", 0, 0) # Create background
horse1 = Horse("images/Horse_1.png", 50, 50) # Create a horse
barrier1 = Barrier("images/Barrier.png", 400, 300) # Create a barrier
gameobjects = []
horses = []
barriers = []
BARRIER_SPEED = 1 # Speed of the barriers
upper_bound_rect = None
lower_bound_rect = None
grass.set_size(WIDTH, HEIGHT) # Set background size
spawner = None
last_barrier = None
gameobjects = [] # List of all game objects
horses = [Horse("images/Horse_1.png", 50, HEIGHT/2 + 0 * i) for i in range(50)] # Create 50 horses
barriers = [barrier1] # List of barriers
def init_game():
global gameobjects, horses, barriers, upper_bound_rect, lower_bound_rect, spawner, last_barrier, BARRIER_SPEED
gameobjects.extend([grass]) # Add background to game objects
gameobjects.extend(horses) # Add horses to game objects
gameobjects.extend(barriers) # Add barriers to game objects
grass = Background("images/Grass.jpg", 0, 0)
grass.set_size(WIDTH, HEIGHT)
upper_bound_rect = pygame.Rect(0, 0, WIDTH, -10),
lower_bound_rect = pygame.Rect(0, HEIGHT, WIDTH, 10),
BARRIER_SPEED = 1
spawner = Spawner("images/Barrier.png") # Initialize spawner
last_barrier = barriers[0]
gameobjects = [grass]
for horse in horses:
horse.set_position(50, HEIGHT/2)
horse.stopped = False
horse.set_vacceleration(0)
horse.set_vspeed(0)
horse.frame_counter = 0
barriers = [Barrier("images/Barrier.png", 400, 300)]
gameobjects.extend(horses)
gameobjects.extend(barriers)
upper_bound_rect = pygame.Rect(0, 0, WIDTH, -10)
lower_bound_rect = pygame.Rect(0, HEIGHT, WIDTH, 10)
spawner = Spawner("images/Barrier.png")
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],
HEIGHT, BARRIER_SPEED, 0]
HEIGHT, BARRIER_SPEED]
return features
genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, 10)
UI.add_horses(horses) # Add horses to UI
horses = [Horse("images/Horse_1.png", 50, HEIGHT/2 + 0 * i) for i in range(POPULATION_SIZE)]
init_game()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Handle window close event
@@ -91,12 +108,16 @@ while True:
horse.stop() # Stop the horse
if horse.rect.colliderect(upper_bound_rect) or horse.rect.colliderect(lower_bound_rect):
horse.stop()
horse.count_fitness
horse.count_fitness()
for object in gameobjects:
object.draw(screen) # Draw all game objects
UI.draw_marks(screen) # Draw UI marks
if all(horse.stopped for horse in horses):
UI.iteration_num += 1
init_game()
pygame.display.flip() # Update the display
pygame.time.Clock().tick(160) # Limit the frame rate to 160 FPS
pygame.time.Clock().tick(FRAME_RATE) # Limit the frame rate to 160 FPS