This commit is contained in:
2019-10-31 13:30:50 +01:00
parent 545ffd21ac
commit 2e27d4a0c4
4 changed files with 27 additions and 7 deletions

25
main.py
View File

@@ -2,12 +2,13 @@
import math
import pygame
import random
import sys
import time
from car import Car
from genetics import genetic_selection, genetic_reproduction
from maps import map1
from params import CELL_COLOR, GX, GY, screen
from params import CELL_COLOR, FRAMERATE, GX, GY, screen
# https://medium.com/intel-student-ambassadors/demystifying-genetic-algorithms-to-enhance-neural-networks-cde902384b6e
@@ -19,6 +20,7 @@ all_cars = pygame.sprite.Group()
loop = 0
max_fitness = 0
avg_fitness = 0
fps = FRAMERATE
for x in range(100):
@@ -26,14 +28,29 @@ for x in range(100):
all_cars.add(car)
def process_keys():
global fps
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key in [pygame.K_KP_PLUS, pygame.K_PLUS]:
fps = int(fps * 1.2)
elif event.key in [pygame.K_KP_MINUS, pygame.K_MINUS]:
fps = int(fps / 1.25)
elif event.key in [pygame.K_ESCAPE]:
sys.exit(0)
def run_round(all_cars):
global max_fitness
global avg_fitness
global fps
running_cars = True
while running_cars:
running_cars = False
screen.fill(CELL_COLOR)
all_cars.draw(screen)
process_keys()
for c in all_cars:
c.show_features()
if c.run:
@@ -47,12 +64,14 @@ def run_round(all_cars):
text = font.render(f"Gen # : {loop}", True, (128, 128, 128))
mft = small_font.render(f"max fitness : {max_fitness}", True, (128, 128, 128))
aft = small_font.render(f"avg fitness : {avg_fitness}", True, (128, 128, 128))
fpst = small_font.render(f"FPS : {fps}", True, (128, 128, 128))
screen.blit(text, (GX - 50 - text.get_width(), text.get_height() // 2))
screen.blit(mft, (GX - 50 - text.get_width(), text.get_height() // 2 + 30))
screen.blit(aft, (GX - 50 - text.get_width(), text.get_height() // 2 + 50))
screen.blit(fpst, (GX - 50 - text.get_width(), text.get_height() // 2 + 75))
pygame.display.flip()
clock.tick(48)
clock.tick(fps)
# for c in all_cars :
# print(f"Car {id(c)} Fitness : {c.brain.fitness})")
@@ -75,7 +94,7 @@ def run_round(all_cars):
all_cars.add(Car(brain=b))
print("Waiting before new run")
for x in range(1):
time.sleep(0.5)
time.sleep(0.25)
pygame.display.flip()