-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.py
54 lines (45 loc) · 1.93 KB
/
env.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
"""This module contains Env class"""
import pygame as pg
from tqdm import tqdm
from obstackles import Obstackles
from params import Parameters
from rockets import Rockets
class Env():
"""This the environment class"""
def __init__(self, args):
"""Constructor"""
self.params = Parameters(args)
self.set_up_objects()
self.counter = 0
self.generation_counter = 0
self.generation_pbar = tqdm(leave=True, desc='Generation: ',
bar_format='{desc}{n}', disable=False)
self.iteration_pbar = tqdm(total=self.params.iteration_duration, leave=False, desc='Living a life', disable=False)
def set_up_objects(self):
"""Set up the objects"""
w, h = pg.display.get_surface().get_size()
self.start = pg.Vector2(w / 2, h - 20)
self.rockets = Rockets(self.params.grid_scale, self.params.amount, self.start)
self.obstackles = Obstackles()
self.target = pg.Vector2(w / 2, 20)
def add_standart_obstackles(self):
self.obstackles.add_standart_obstackles()
def update(self):
"""Update the environment"""
if self.counter < self.params.iteration_duration:
self.rockets.update(self.obstackles, self.target)
self.iteration_pbar.update()
self.counter += 1
else:
self.iteration_pbar.close()
self.iteration_pbar = tqdm(total=self.params.iteration_duration, leave=False, desc='Living a life', disable=False)
self.generation_pbar.update()
self.counter = 0
self.rockets.evolve(self.params.mutation_scale, self.target)
def draw(self):
"""Draw everything"""
# Dependency inversion principle
self.rockets.draw()
self.obstackles.draw()
pg.draw.circle(pg.display.get_surface(), pg.color.Color('lightcyan4'),
(int(self.target.x), int(self.target.y)), 20)