-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
110 lines (88 loc) · 3.79 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pygame
import sys, getopt
import numpy as np
from service.setting import Setting
from field.field import Field
from tree.tree import Tree
from dashboard.dashboard import Dashboard
import service.game_function as gf
import os
setting = Setting
arguments = {'non_gui': False,
'step_mode': False,
'trees_count': 10,
'itter_save': None,
'folder': 'TreesGenom',
'genoms_folder': None}
arguments, setting = gf.create_arguments(arguments, sys.argv[1:], setting)
itteration = 1
map = np.array(
[[0 for _ in range(setting.width // setting.pixel_size)] for _ in range(setting.height // setting.pixel_size)])
trees = gf.create_trees(arguments, setting, map)
if not arguments['non_gui']:
delay = 0
pygame.font.init()
pygame.init()
screen = pygame.display.set_mode((setting.width, setting.height)) # создание экрана
field = Field(screen, setting)
dashboard = Dashboard(screen, setting)
dashboard.switches[0].pressed = arguments['step_mode']
next_step = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for button in dashboard.buttons: # отработка нажатий кнопок и выключателей
button.button_down(event)
for switcher in dashboard.switches:
switcher.press(event)
if event.type == pygame.KEYDOWN: # следующий шаг в step mode при нажатии пробела или стрелки
if event.key == pygame.K_SPACE or event.key == pygame.K_RIGHT:
next_step = True
if dashboard.buttons[0].is_pressed(): # изменение задержки
if delay > 0:
delay -= 10
if dashboard.buttons[1].is_pressed():
delay += 10
if dashboard.buttons[3].is_pressed(): # новая симуляция
trees = gf.create_trees(arguments, setting, map)
if arguments['itter_save']:
for folder in os.listdir(arguments['folder']):
for file in os.listdir(arguments['folder'] + '/' + folder):
os.remove(arguments['folder'] + '/' + folder + '/' + file)
os.rmdir(arguments['folder'] + '/' + folder)
itteration = 0
if dashboard.buttons[2].is_pressed():
file = input("enter file_name : ") # соханение генома
with open(file + '.txt', 'w') as f:
for tree in trees:
f.write(str(tree.genom) + '\n\n\n')
gf.save_genom(arguments, trees, itteration)
if not dashboard.switches[0].is_pressed() or (
dashboard.switches[0].is_pressed() and next_step): # рост деревьев
new_trees = []
for tree in trees:
new_tree = tree.grow(map)
new_trees += new_tree
trees = new_trees
itteration += 1
next_step = False
field.fill() # заливка поля и отрисовка объектов
dashboard.draw()
dashboard.text_areas[0].text = "itteration: " + str(itteration)
dashboard.blit((0, 0))
for tree in trees:
field.draw_pixels(tree.get_pixels())
for button in dashboard.buttons: # отработка разжатия кнопок
button.button_up()
pygame.display.flip()
pygame.time.wait(delay) # задержка
else:
while True:
new_trees = []
for tree in trees:
new_tree = tree.grow(map)
new_trees += new_tree
trees = new_trees
itteration += 1
gf.save_genom(arguments, trees, itteration)