-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
58 lines (43 loc) · 1.55 KB
/
world.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
import stage
import merk
import lifeanddeath
import numpy
import random
import math
import matplotlib as plt
import matplotlib.pyplot as plt
from globalvars import WIN_X
from globalvars import WIN_Y
from globalvars import NB_AREAS
from globalvars import PATCHSIZE
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
class World:
# ------------------------------------------------ #
def __init__(self, merks_num=0):
self.stage_ = stage.Stage()
self.merklist = []
self.merks_num = merks_num
for i in range(merks_num):
self.merklist.append(merk.Merk())
self.merklist[-1].build_random_merk()
# ------------------------------------------------ #
def step_forward(self):
self.stage_.move_areas()
self.stage_.refresh_costmap()
for mek in self.merklist:
actions = update_activations(mek, self.stage_)
mek.svars.set_activations(actions)
mek.action()
lifeanddeath.aging(mek, self.stage_)
lifeanddeath.deathandrecycle(self.merklist)
# ------------------------------------------------ #
def update_sprites(spritelist, world):
for (ix, merk) in enumerate(spritelist):
x, y, angle = world.merklist[ix].svars.get_pos()
merk.update(x, y, angle)
def update_activations(mek, stage_):
x, y, angle = mek.svars.x, mek.svars.y, mek.svars.angle
sensorsdata = numpy.array(
list(stage_.get_local_fields(x, y, angle)) + [1.0, -1.0])
actions = mek.nn.update_activations(sensorsdata)
return(actions)