-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (49 loc) · 2.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pygame
from Environment import *
from constants import *
DISPLAY = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
ENV = None
CLOCK = None
class Window:
def __init__(self):
self.is_running = True
def setup(self):
pygame.init()
pygame.display.set_caption("Genetic Snake")
DISPLAY.fill(pygame.Color("WHITE"))
pygame.display.update()
ENV = Environment(POPS_NUMBER, POP_SIZE)
CLOCK = pygame.time.Clock()
def draw(self):
self.show_info()
def clear_text(self, x: int, y: int, diff: int):
pygame.draw.rect(DISPLAY, pygame.Color("White"), (x, y, 10, 15))
pygame.draw.rect(DISPLAY, pygame.Color("White"), (x + diff, y, 10, 15))
pygame.draw.rect(DISPLAY, pygame.Color("White"), (x + 2 * diff, y, 10, 15))
pygame.draw.rect(DISPLAY, pygame.Color("White"), (x + 3 * diff, y, 10, 15))
def show_info(self):
self.clear_text(590, 105, 5)
self.clear_text(550, 135, 10)
my_font = pygame.font.SysFont('Arial', 20)
text_surf_gen = my_font.render("Generation: " + str(ENV.no_generations), False, pygame.Color("Black"))
DISPLAY.blit(text_surf_gen, (500, 100))
text_surf_len = my_font.render("Score: " + str(ENV.best_snake_len), False, pygame.Color("Black"))
DISPLAY.blit(text_surf_len, (500, 130))
pygame.draw.line(DISPLAY, pygame.Color("BLACK"), (PLAYABLE_AREA_WIDTH, 0), (PLAYABLE_AREA_WIDTH, PLAYABLE_AREA_HEIGHT))
pygame.display.update(PLAYABLE_AREA_WIDTH, 0, WINDOW_WIDTH - PLAYABLE_AREA_WIDTH, WINDOW_HEIGHT)
def main():
clock = pygame.time.Clock()
win = Window()
win.setup()
while win.is_running:
win.draw()
if not ENV.is_pop_extinct(): # if any snake is alive
ENV.update()
else: # none snake is alive
ENV.run_genetic()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
win.is_running = False
if __name__ == "__main__":
main()