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