Add game loop. Add fitness counter
This commit is contained in:
@@ -117,6 +117,7 @@ class Barrier(GameObject):
|
|||||||
self.scale_by(5)
|
self.scale_by(5)
|
||||||
|
|
||||||
class UI:
|
class UI:
|
||||||
|
iteration_num = 0
|
||||||
horses = [] # List of child objects
|
horses = [] # List of child objects
|
||||||
horizontal_offset = 25 # Horizontal spacing between marks
|
horizontal_offset = 25 # Horizontal spacing between marks
|
||||||
vertical_offset = -25 # Vertical spacing between marks
|
vertical_offset = -25 # Vertical spacing between marks
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ import sys
|
|||||||
from gameobjects import *
|
from gameobjects import *
|
||||||
from genetic_alg import GeneticAlgorithm
|
from genetic_alg import GeneticAlgorithm
|
||||||
|
|
||||||
POPULATION_SIZE = 50
|
POPULATION_SIZE = 5
|
||||||
MUTATION_RATE = 0.5
|
MUTATION_RATE = 0.5
|
||||||
POPULATION_BEST = 0.2
|
POPULATION_BEST = 0.2
|
||||||
|
FRAME_RATE = 160
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
@@ -15,39 +16,55 @@ pygame.display.set_caption("NIC_Project") # Set window title
|
|||||||
WHITE = (255, 255, 255) # Define white color
|
WHITE = (255, 255, 255) # Define white color
|
||||||
|
|
||||||
UI = UI() # Initialize UI
|
UI = UI() # Initialize UI
|
||||||
grass = Background("images/Grass.jpg", 0, 0) # Create background
|
gameobjects = []
|
||||||
horse1 = Horse("images/Horse_1.png", 50, 50) # Create a horse
|
horses = []
|
||||||
barrier1 = Barrier("images/Barrier.png", 400, 300) # Create a barrier
|
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
|
def init_game():
|
||||||
horses = [Horse("images/Horse_1.png", 50, HEIGHT/2 + 0 * i) for i in range(50)] # Create 50 horses
|
global gameobjects, horses, barriers, upper_bound_rect, lower_bound_rect, spawner, last_barrier, BARRIER_SPEED
|
||||||
barriers = [barrier1] # List of barriers
|
|
||||||
|
|
||||||
gameobjects.extend([grass]) # Add background to game objects
|
grass = Background("images/Grass.jpg", 0, 0)
|
||||||
gameobjects.extend(horses) # Add horses to game objects
|
grass.set_size(WIDTH, HEIGHT)
|
||||||
gameobjects.extend(barriers) # Add barriers to game objects
|
|
||||||
|
|
||||||
upper_bound_rect = pygame.Rect(0, 0, WIDTH, -10),
|
BARRIER_SPEED = 1
|
||||||
lower_bound_rect = pygame.Rect(0, HEIGHT, WIDTH, 10),
|
|
||||||
|
|
||||||
spawner = Spawner("images/Barrier.png") # Initialize spawner
|
gameobjects = [grass]
|
||||||
last_barrier = barriers[0]
|
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):
|
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],
|
||||||
HEIGHT, BARRIER_SPEED, 0]
|
HEIGHT, BARRIER_SPEED]
|
||||||
return features
|
return features
|
||||||
|
|
||||||
genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, 10)
|
genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, 10)
|
||||||
|
horses = [Horse("images/Horse_1.png", 50, HEIGHT/2 + 0 * i) for i in range(POPULATION_SIZE)]
|
||||||
UI.add_horses(horses) # Add horses to UI
|
init_game()
|
||||||
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
|
||||||
@@ -91,12 +108,16 @@ while True:
|
|||||||
horse.stop() # Stop the horse
|
horse.stop() # Stop the horse
|
||||||
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()
|
||||||
|
|
||||||
|
|
||||||
for object in gameobjects:
|
for object in gameobjects:
|
||||||
object.draw(screen) # Draw all game objects
|
object.draw(screen) # Draw all game objects
|
||||||
UI.draw_marks(screen) # Draw UI marks
|
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.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
|
||||||
Reference in New Issue
Block a user