-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
677 lines (581 loc) · 26.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
import matplotlib.pyplot as plt
from Statistics import Statistic
from Food import Food
from Creature import Creature
from Cursor import Cursor
from DNA import DNA
from Button import Button
from SubWindow import subwindow
from random import randint as rand
from lib_func import *
from settings import *
count_crt = COUNT_CRT
alive = count_crt
days = 1
brd_info = ""
iterator = 0
focus = -1
redraw_dna = 0
pg.init()
window = pg.display.set_mode((Win_W, Win_H))
screen = window.subsurface((Set_W, 0, SCR_W, SCR_H))
set_pan = window.subsurface((0, 0, Set_W, SCR_H))
btn_mas = [Button(186, 2, "img/pause.png"), Button(218, 2, "img/reset.png"), Button(2, 5, " Statistic", 93, 22)]
stat_w = subwindow(95, 27, 900, 510)
stat = Statistic()
visible = 0
run = 1
fd_gr = pg.sprite.Group()
crtb_gr = pg.sprite.Group()
crts_gr = pg.sprite.Group()
deadb_gr = pg.sprite.Group()
deads_gr = pg.sprite.Group()
curs_gr = pg.sprite.Group()
btn_gr = pg.sprite.Group()
vis_gr = []
crt_mas = [Creature(WCR, HCR, SENS) for i in range(count_crt)]
fd_mas = [Food() for i in range(COUNT_FD)]
curs_mas = [Cursor(10, 160 + 80 * i, [COUNT_FD / 2000, SPEED, SP_GEN_FD][i]) for i in range(3)]
for i in range(count_crt):
crtb_gr.add(crt_mas[i].body)
crts_gr.add(crt_mas[i].sens_circ)
for i in range(COUNT_FD):
fd_gr.add(fd_mas[i])
for i in range(3):
curs_gr.add(curs_mas[i])
for i in range(len(btn_mas)):
btn_gr.add(btn_mas[i])
index = [fn_nrst_trg(crt_mas[i], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr) for i in range(count_crt)]
dna = DNA(15, 70, 430)
time_of_press = pg.time.get_ticks()
# начало программы
while run:
# вывод информации о ситуации
l1 = F1.render("Alive - " + str(alive), 1, (0, 0, 0))
l2 = F1.render("Food - " + str(int(len(fd_gr) / COUNT_FD * 100)) + "%", 1, (0, 0, 0))
l3 = F1.render("Days - " + str(days), 1, (0, 0, 0))
l4 = F1.render("Food - " + str(COUNT_FD), 1, (0, 0, 0))
l5 = F1.render("Speed - " + str(int(curs_mas[1].percent * 100)) + "%", 1, (0, 0, 0))
l6 = F2.render("Speed gener. food - " + str(int(curs_mas[2].percent * 100)) + "%", 1, (0, 0, 0))
screen.fill((0, 0, 0))
set_pan.fill((210, 210, 210))
for event in pg.event.get():
if event.type == pg.QUIT:
run = 0
if event.type == pg.KEYUP and 0 <= focus < count_crt:
index[focus] = fn_nrst_trg(crt_mas[focus], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr)
# перемещение курсора с помощью клавиши TAB
keys = pg.key.get_pressed()
if keys[pg.K_TAB] and pg.time.get_ticks() - time_of_press > 300:
# чтобы курсор не проскакивал
time_of_press = pg.time.get_ticks()
dna.normal()
if 0 <= focus < count_crt:
crt_mas[focus].focus = 0
focus = cycle(focus + 1, count_crt + 2, 0)
while focus < count_crt and not crt_mas[focus].energy:
focus = cycle(focus + 1, count_crt + 2, 0)
if 0 <= focus < count_crt:
crt_mas[focus].focus = 1
dna.redraw(crt_mas[focus])
elif focus > 0:
redraw_cursors(focus, count_crt, curs_mas)
x = curs_mas[focus - count_crt].percent * (Set_W - 20)
curs_mas[focus - count_crt].redraw((x, 0))
# Пауза
if keys[pg.K_SPACE] and pg.time.get_ticks() - time_of_press > 300:
# чтобы курсор не проскакивал
time_of_press = pg.time.get_ticks()
PAUSE = not PAUSE
if PAUSE:
btn_mas[0].redraw("img/play.png")
else:
btn_mas[0].redraw("img/pause.png")
# Закрытие окна статистики
if keys[pg.K_ESCAPE] and visible:
visible = 0
btn_mas[2].unfocus()
PAUSE = 0
btn_mas[0].redraw("img/pause.png")
# отрисовка всех объектов
crts_gr.draw(screen)
fd_gr.draw(screen)
crtb_gr.draw(screen)
btn_gr.draw(set_pan)
# "обнуление" фокуса
if pg.mouse.get_pressed()[0] and not focus >= count_crt:
focus = -1
# снятие фокуса с особей
if not 0 <= focus < count_crt:
if redraw_dna:
dna.normal()
redraw_dna = 0
for j in range(count_crt):
crt_mas[j].focus = 0
# затухание еды
temp_time = pg.time.get_ticks()
if (temp_time - START_TIME) % 10 == 0:
for i in range(len(fd_mas)):
if fd_mas[i].color[0]:
fd_mas[i].fading()
# появление новой еды
if (temp_time - START_TIME) % (int(200 * (1 - SP_GEN_FD)) + 1) == 0 and not PAUSE:
for i in range(COUNT_FD):
if fd_mas[i] not in fd_gr:
fd_mas[i].rect.x = rand(WCR, SCR_W - WCR)
fd_mas[i].rect.y = rand(HCR, SCR_H - HCR)
fd_mas[i].image.fill((0, 255, 0))
fd_mas[i].color = (0, 255, 0)
fd_gr.add(fd_mas[i])
break
# видит ли особь еду
prev_len = 0
for i in range(count_crt):
vis_gr += pg.sprite.spritecollide(crt_mas[i].sens_circ, fd_gr, False)
if index[i] == -1:
while len(vis_gr) != prev_len:
del vis_gr[-1]
if len(vis_gr) - prev_len:
for j in range(len(vis_gr) - prev_len):
vis_gr[j + prev_len].image.fill((255, 0, 0))
prev_len = len(vis_gr)
# окраска еды которую видит особь
for i in range(len(vis_gr)):
vis_gr[i].color = (255, 0, 0)
# передвижение каждой особи
for i in range(count_crt):
tmp = 1
# передвижение выбранной особи
keys = pg.key.get_pressed()
control = 0
if keys[pg.K_RIGHT] and 0 <= focus < count_crt:
control = 1
crt_mas[focus].body.rect.x += 1
crt_mas[focus].sens_circ.rect.x += 1
elif keys[pg.K_UP] and 0 <= focus < count_crt:
control = 1
crt_mas[focus].body.rect.y -= 1
crt_mas[focus].sens_circ.rect.y -= 1
elif keys[pg.K_LEFT] and 0 <= focus < count_crt:
control = 1
crt_mas[focus].body.rect.x -= 1
crt_mas[focus].sens_circ.rect.x -= 1
elif keys[pg.K_DOWN] and 0 <= focus < count_crt:
control = 1
crt_mas[focus].body.rect.y += 1
crt_mas[focus].sens_circ.rect.y += 1
elif keys[pg.K_ESCAPE]:
focus = -1
# Убийство особи
elif keys[pg.K_DELETE] and 0 <= focus < count_crt and crt_mas[focus].energy:
stat.dead_at_day += 1
crt_mas[focus].energy = 0
if 0 <= index[focus] < COUNT_FD:
tmp = crt_mas[focus].go_to_target(fd_mas[index[focus]])
elif 0 <= index[focus]:
tmp = crt_mas[focus].go_to_target(crt_mas[clamp(index[focus] - len(fd_mas), len(crt_mas) - 1)].body)
stat.dead_at_day += 1
index[focus] = -1
deadb_gr.add(crt_mas[focus].body)
deads_gr.add(crt_mas[focus].sens_circ)
crt_mas[focus].color2 = (60, 60, 60)
focus = -1
# ниже функция clamp используется в качестве костыля, потому что без неё почему-то происходит 'index out of range'
if 0 <= index[i] < COUNT_FD and not PAUSE and (focus != i or not control):
# к еде
if (crt_mas[i].body.rect.x != fd_mas[index[i]].rect.x or
crt_mas[i].body.rect.y != fd_mas[index[i]].rect.y) and \
index[i] != -1:
tmp = crt_mas[i].go_to_target(fd_mas[index[i]])
elif 0 <= index[i] and not PAUSE and (focus != i or not control):
# к особи
if (crt_mas[i].body.rect.x != crt_mas[clamp(index[i] - COUNT_FD, count_crt - 1)].body.rect.x or
crt_mas[i].body.rect.y != crt_mas[clamp(index[i] - COUNT_FD, count_crt - 1)].body.rect.y) and \
index[i] != -1:
tmp = crt_mas[i].go_to_target(crt_mas[clamp(index[i] - len(fd_mas), len(crt_mas) - 1)].body)
# регестрация гибели
if tmp == 0:
# Statistics
stat.dead_at_day += 1
index[i] = -1
deadb_gr.add(crt_mas[i].body)
deads_gr.add(crt_mas[i].sens_circ)
crt_mas[i].color2 = (60, 60, 60)
if focus == i:
focus = -1
# обработка энергии
for i in range(count_crt):
# фокус на особь
if crt_mas[i].sens_circ.rect.x <= pg.mouse.get_pos()[0] - Set_W <= crt_mas[i].sens_circ.rect.x + crt_mas[i].sens_circ.rect.w and \
crt_mas[i].sens_circ.rect.y <= pg.mouse.get_pos()[1] <= crt_mas[i].sens_circ.rect.y + crt_mas[
i].sens_circ.rect.h and pg.mouse.get_pressed()[0] and crt_mas[i].energy:
if focus < count_crt:
crt_mas[focus].focus = 0
focus = i
crt_mas[i].focus = 1
dna.normal()
dna.redraw(crt_mas[i])
redraw_dna = 1
break
hit_gr = pg.sprite.spritecollide(crt_mas[i].body, fd_gr, True)
# съела ли особь еду
if len(hit_gr):
for j in range(len(hit_gr)):
crt_mas[i].energy += FD_EN
# съела ли особь другую особь
elif index[i] >= 0:
# живую
hit_gr = pg.sprite.spritecollide(crt_mas[i].body, crtb_gr, False)
if len(hit_gr) > 1 and \
index[i] >= COUNT_FD and \
crt_mas[clamp(index[i] - COUNT_FD, count_crt - 1)].energy and \
crt_mas[i].weight - 10 > crt_mas[clamp(index[i] - COUNT_FD, count_crt - 1)].weight:
# Statistics
stat.dead_at_day += 1
crt_mas[index[i] - COUNT_FD].body.kill()
crt_mas[index[i] - COUNT_FD].sens_circ.kill()
crt_mas[i].energy += 0.5 * crt_mas[index[i] - COUNT_FD].weight / 64
crt_mas[index[i] - COUNT_FD].energy = 0
index[index[i] - COUNT_FD] = -1
# мёртвую
hit_gr = pg.sprite.spritecollide(crt_mas[i].body, deadb_gr, True)
if len(hit_gr):
while crt_mas[iterator].body not in hit_gr:
iterator += 1
crt_mas[i].energy += 0.5 * crt_mas[iterator].weight / 64
crt_mas[iterator].body.kill()
crt_mas[iterator].sens_circ.kill()
iterator = 0
# рождение
if crt_mas[i].energy > 9.8:
if focus >= count_crt:
focus += 1
for j in range(int(9.8 // crt_mas[i].birth_enr)):
# Statistics
stat.birth_at_day += 1
# вычисление координат дочерней особи
cords = (crt_mas[i].body.rect.x + WCR, crt_mas[i].body.rect.y + HCR)
# факт рождения
crt_mas.append(Creature(WCR, HCR, SENS, cords, crt_mas[i].breed+1))
crt_mas[i].copy_to(crt_mas[-1])
# мутация
# скорость
chance = rand(0, 100)
if 90 <= chance < 95 and crt_mas[-1].speed != clamp(crt_mas[-1].speed - 1, 5, 1) and Sp_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].color2 = (clamp(crt_mas[-1].color2[0] - 13),
crt_mas[-1].color2[1],
crt_mas[-1].color2[2])
crt_mas[-1].speed = clamp(crt_mas[-1].speed - 1, 5, 1)
elif chance >= 95 and crt_mas[-1].speed != clamp(crt_mas[-1].speed + 1, 5, 1) and Sp_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].color2 = (clamp(crt_mas[-1].color2[0] + 13),
crt_mas[-1].color2[1],
crt_mas[-1].color2[2])
crt_mas[-1].speed = clamp(crt_mas[-1].speed + 1, 5, 1)
# размножение
chance = rand(0, 100)
if 90 <= chance < 95 and crt_mas[-1].birth_enr != clamp(crt_mas[-1].birth_enr + 1, 5, 1) and R_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].color2 = (crt_mas[-1].color2[0],
clamp(crt_mas[-1].color2[1] - 13),
crt_mas[-1].color2[2])
crt_mas[-1].birth_enr = clamp(crt_mas[-1].birth_enr + 1, 5, 2)
elif chance >= 95 and crt_mas[-1].birth_enr != clamp(crt_mas[-1].birth_enr - 1, 5, 1) and R_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].color2 = (crt_mas[-1].color2[0],
clamp(crt_mas[-1].color2[1] + 13),
crt_mas[-1].color2[2])
crt_mas[-1].birth_enr = clamp(crt_mas[-1].birth_enr - 1, 5, 2)
# размер: ширина
chance = rand(0, 100)
if 90 <= chance < 95 and W_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].w += 2
crt_mas[-1].weight = crt_mas[-1].w * crt_mas[-1].h
elif 95 <= chance and W_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].w -= 2
crt_mas[-1].weight = crt_mas[-1].w * crt_mas[-1].h
# размер: высота
chance = rand(0, 100)
if 90 <= chance < 95 and H_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].h += 2
crt_mas[-1].weight = crt_mas[-1].w * crt_mas[-1].h
elif 95 <= chance and H_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].h -= 2
crt_mas[-1].weight = crt_mas[-1].w * crt_mas[-1].h
# размер видимого поля
chance = rand(0, 100)
if 90 <= chance < 95 and Se_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].expantion(clamp(crt_mas[-1].sens + 2, 45))
elif 95 <= chance and Se_MUT:
# Statistics
stat.mut_at_day += 1
crt_mas[-1].expantion(clamp(crt_mas[-1].sens - 2, 45, 15))
# высчитывание цели для дочерней особи
index.append(fn_nrst_trg(crt_mas[-1], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr, [index[j] for j in range(count_crt) if j != i]))
# отрисовка дочерней особи
crtb_gr.add(crt_mas[-1].body)
crts_gr.add(crt_mas[-1].sens_circ)
count_crt += 1
# затраты на рождение
crt_mas[i].energy = 2.5
# пересчёт следущей цели-еды
for i in range(count_crt):
if index[i] < len(fd_mas):
if fd_mas[index[i]] not in fd_gr and index[i] != -1:
index[i] = fn_nrst_trg(crt_mas[i], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr,
[index[j] for j in range(count_crt) if j != i])
elif crt_mas[clamp(index[i] - COUNT_FD, count_crt - 1)].body not in crtb_gr:
index[i] = fn_nrst_trg(crt_mas[i], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr,
[index[j] for j in range(count_crt) if j != i])
# случай, при котором уда, за которой шла особь, удалилась из-за уменьшения её общего количества
else:
index[i] = fn_nrst_trg(crt_mas[i], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr,
[index[j] for j in range(count_crt) if j != i])
# "Текучая" статистика
stat.avarage_day = 0
stat.oldest_alive_breed = 0
stat.oldest_creature = 0
stat.youngest_alive_breed = 1000000
for i in range(count_crt):
if crt_mas[i].energy:
if crt_mas[i].breed > stat.oldest_alive_breed:
stat.oldest_alive_breed = crt_mas[i].breed
if crt_mas[i].breed < stat.youngest_alive_breed:
stat.youngest_alive_breed = crt_mas[i].breed
if crt_mas[i].days > stat.oldest_creature:
stat.oldest_creature = crt_mas[i].days
stat.avarage_day += 1
stat.avarage_day //= count_crt
stat.avarage_day = clamp(stat.avarage_day, 1000000, 1)
if (pg.time.get_ticks() - START_TIME) % 150 == 0:
stat_w.redraw(stat.oldest_alive_breed,
stat.youngest_alive_breed,
stat.avarage_day,
stat.oldest_creature)
# генерация нового дня
if len(fd_gr) < COUNT_FD // 100 * 6:
stat.new_day(alive)
# stat.get()
# генерация нового поля еды
for i in range(COUNT_FD):
if fd_mas[i] not in fd_gr:
fd_mas[i].rect.x = rand(WCR, SCR_W - WCR)
fd_mas[i].rect.y = rand(HCR, SCR_H - HCR)
fd_mas[i].image.fill((0, 255, 0))
fd_mas[i].color = (0, 255, 0)
fd_gr.add(fd_mas[i])
# навый подсчет целей-еды для каждой живой особи (чтобы не бежали все в одну сторону)
for i in range(count_crt):
if index[i] >= 0:
index[i] = fn_nrst_trg(crt_mas[i], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr)
crt_mas[i].days += 1
# очистка "памяти" от съеденных особей
index = [index[i] for i in range(count_crt) if crt_mas[i].body in crtb_gr]
tmp = []
for i in range(count_crt):
if crt_mas[i].body in crtb_gr:
tmp.append(crt_mas[i])
# коррекция фокуса
elif i < focus or focus > len(crtb_gr):
focus -= 1
crt_mas = tmp
count_crt = len(crt_mas)
# обновление дня
days += 1
# обновление информации
brd_info = ""
vis_gr = []
alive = count_crt - index.count(-1)
# обработка кнопок
for i in range(len(btn_mas)):
# Наведён ли курсор на кнопку
if exist_in(pg.mouse.get_pos(), btn_mas[i].rect):
# Нажата ли кнопка
if pg.mouse.get_pressed()[0] and pg.time.get_ticks() - time_of_press > 200:
time_of_press = pg.time.get_ticks()
if i == 0 and btn_mas[i].active:
btn_mas[i].up()
else:
btn_mas[i].down()
# Пвуза
if i == 0:
if btn_mas[i].active:
btn_mas[i].redraw("img/play.png")
PAUSE = 1
else:
btn_mas[i].redraw("img/pause.png")
PAUSE = 0
# Ресет
if i == 1:
stat.reset()
count_crt = COUNT_CRT
fd_gr = pg.sprite.Group()
crtb_gr = pg.sprite.Group()
crts_gr = pg.sprite.Group()
deadb_gr = pg.sprite.Group()
deads_gr = pg.sprite.Group()
vis_gr = []
crt_mas = [Creature(WCR, HCR, SENS) for j in range(count_crt)]
fd_mas = [Food() for j in range(COUNT_FD)]
for j in range(count_crt):
crtb_gr.add(crt_mas[j].body)
crts_gr.add(crt_mas[j].sens_circ)
for j in range(COUNT_FD):
fd_gr.add(fd_mas[j])
index = [fn_nrst_trg(crt_mas[j], crt_mas, fd_mas, crtb_gr, deadb_gr, fd_gr) for j in
range(count_crt)]
days = 1
# Статистика
if i == 2:
if visible:
visible = 0
btn_mas[0].redraw("img/pause.png")
btn_mas[i].active = 0
PAUSE = 0
else:
visible = 1
btn_mas[0].redraw("img/play.png")
btn_mas[i].active = 1
PAUSE = 1
btn_mas[i].focus()
elif i != 2 or not btn_mas[i].active:
btn_mas[i].unfocus()
# изменение настроек
for i in range(3):
if exist_in(pg.mouse.get_pos(), curs_mas[i].rect) and pg.mouse.get_pressed()[0] and visible == 0:
curs_mas[i].redraw(pg.mouse.get_pos())
# количество еды
if i == 0:
focus = count_crt
COUNT_FD = int(2000 * curs_mas[i].percent)
while COUNT_FD > len(fd_mas):
fd_mas.append(Food())
fd_mas[-1].rect.x = rand(WCR, SCR_W - WCR)
fd_mas[-1].rect.y = rand(HCR, SCR_H - HCR)
fd_mas[-1].image.fill((0, 255, 0))
fd_mas[-1].color = (0, 255, 0)
fd_gr.add(fd_mas[-1])
while COUNT_FD < len(fd_mas):
fd_mas[-1].kill()
fd_mas = fd_mas[:-1]
# скорость
if i == 1:
focus = count_crt + 1
SPEED = curs_mas[i].percent
# скорость генерации новой еды
if i == 2:
focus = count_crt + 2
SP_GEN_FD = curs_mas[i].percent
# фокус на курсор
redraw_cursors(focus, count_crt, curs_mas)
# передвижение выбранного курсора
if focus >= count_crt:
keys = pg.key.get_pressed()
# отрисовка
x = 0
if keys[pg.K_RIGHT]:
x = clamp(curs_mas[focus - count_crt].percent * (Set_W - 20) + (Set_W - 20) // 200, SCR_W, 20)
elif keys[pg.K_LEFT]:
x = clamp(curs_mas[focus - count_crt].percent * (Set_W - 20) - (Set_W - 20) // 200, SCR_W, 20)
elif keys[pg.K_UP] and pg.time.get_ticks() - time_of_press > 300:
# чтобы курсор не проскакивал
time_of_press = pg.time.get_ticks()
focus = cycle(focus - 1, count_crt + 2, count_crt)
x = curs_mas[focus - count_crt].percent * (Set_W - 20)
elif keys[pg.K_DOWN] and pg.time.get_ticks() - time_of_press > 300:
# -//-
time_of_press = pg.time.get_ticks()
focus = cycle(focus + 1, count_crt + 2, count_crt)
x = curs_mas[focus - count_crt].percent * (Set_W - 20)
elif keys[pg.K_ESCAPE]:
focus = -1
redraw_cursors(focus, count_crt, curs_mas)
if x:
curs_mas[focus - count_crt].redraw((x, 0))
# изменение настроек
if focus == count_crt:
COUNT_FD = int(2000 * curs_mas[0].percent)
while COUNT_FD > len(fd_mas):
fd_mas.append(Food())
fd_mas[-1].rect.x = rand(WCR, SCR_W - WCR)
fd_mas[-1].rect.y = rand(HCR, SCR_H - HCR)
fd_mas[-1].image.fill((0, 255, 0))
fd_mas[-1].color = (0, 255, 0)
fd_gr.add(fd_mas[-1])
while COUNT_FD < len(fd_mas):
fd_mas[-1].kill()
fd_mas = fd_mas[:-1]
elif focus == count_crt + 1:
SPEED = curs_mas[1].percent
elif focus == count_crt + 2:
SP_GEN_FD = curs_mas[2].percent
# оформление
pg.draw.line(set_pan, (130, 130, 130), (0, 34), (Set_W, 34))
pg.draw.line(set_pan, (130, 130, 130), (10, 120), (Set_W - 10, 120))
curs_gr.draw(set_pan)
pg.draw.line(set_pan, (130, 130, 130), (10, 380), (Set_W - 10, 380))
pg.draw.rect(set_pan, (0, 0, 0), (25, 405, 190, 265))
# ДНК
for i in range(2):
dna.turning()
dna.draw(set_pan)
set_pan.blit(l1, (Set_W * (1 - 170/Set_W) / 2, 42))
set_pan.blit(l2, (Set_W * (1 - 170/Set_W) / 2 + 1, 65))
set_pan.blit(l3, (Set_W * (1 - 170/Set_W) / 2 + 4, 92))
set_pan.blit(l4, (Set_W * (1 - 140/Set_W) / 2, 135))
set_pan.blit(l5, (Set_W * (1 - 150/Set_W) / 2, 215))
set_pan.blit(l6, (10, 295))
# Отрисовка окна статистики
if visible:
stat_w.draw(window)
for i in range(len(stat_w.buttons)):
if exist_in(pg.mouse.get_pos(), stat_w.buttons[i].rect):
stat_w.buttons[i].focus()
else:
stat_w.buttons[i].unfocus()
for i in range(len(stat_w.buttons)):
if exist_in(pg.mouse.get_pos(), stat_w.buttons[i].rect)\
and pg.mouse.get_pressed()[0]\
and pg.time.get_ticks() - time_of_press > 200:
time_of_press = pg.time.get_ticks()
if i == 0:
plt.plot([i + 1 for i in range(days - 1)], stat.BIRTH_PER_DAY)
plt.show()
if i == 1:
plt.plot([i + 1 for i in range(days)], stat.ALIVE_PER_DAY)
plt.show()
if i == 2:
plt.plot([i + 1 for i in range(days - 1)], stat.DEAD_PER_DAY)
plt.show()
if i == 3:
plt.plot([i + 1 for i in range(days - 1)], stat.MUT_PER_DAY)
plt.show()
if i >= 4:
stat_w.buttons[i].active = not stat_w.buttons[i].active
if i == 4:
Sp_MUT = not Sp_MUT
if i == 5:
R_MUT = not R_MUT
if i == 6:
W_MUT = not W_MUT
if i == 7:
H_MUT = not H_MUT
if i == 8:
Se_MUT = not Se_MUT
pg.time.delay(int(50 * (1 - SPEED)))
pg.display.update()