-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
64 lines (46 loc) · 1.63 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
59
60
61
62
63
64
import random
import operator
from src.being import Being
class World:
def __init__(self, w=128, h=128):
self.w = w
self.h = h
self.alive = 0
self.locations = dict()
def step(self):
self.alive = 0
new_locations = dict()
dead_sprites = []
for location, being in self.locations.items():
if not being.is_alive():
dead_sprites.append(being.sprite_index)
continue
self.alive += 1
action = being.step(location, self.locations)
if action == 'STOP':
being.speed = 0
if action == 'MOVE':
being.speed = 1
if being.speed > 0:
new_location = (
max(0, min(self.w, location[0] + being.direction[0])),
max(0, min(self.h, location[1] + being.direction[1])),
)
if new_location not in self.locations and new_location not in new_locations:
new_locations[new_location] = being
continue
new_locations[location] = being
self.locations = new_locations
return dead_sprites
def spawn(self, sprite_index):
x = random.randint(0, self.w - 1)
y = random.randint(0, self.h - 1)
location = (x, y)
while location in self.locations:
x = random.randint(0, self.w - 1)
y = random.randint(0, self.h - 1)
location = (x, y)
self.alive += 1
being = Being(sprite_index)
self.locations[location] = being
return location, being