-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_world_advanced.py
executable file
·78 lines (55 loc) · 1.63 KB
/
test_world_advanced.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os.path
import logging
from math import sqrt
from npgameworld.world import World
def get_hero_actions(enemies, hero_x, hero_y):
hero_actions = []
if not enemies:
return hero_actions
distances = [] # to enemies
for e in enemies:
t = (sqrt((e['x']-hero_x)**2 + (e['y']-hero_y)**2), e['enemy_id'])
distances.append(t)
nearest_enemy = sorted(distances)[0][1]
for e in enemies:
if e['enemy_id'] == nearest_enemy:
enemy_x = e['x']
enemy_y = e['y']
# Shoot to nearest enemy
hero_actions.append({'cmd': 'shoot', 'x': enemy_x, 'y': enemy_y})
# Move into reversed direction from nearest enemy
# xd - x direction, yd - y direction
if enemy_x > hero_x:
xd = -1
elif enemy_x < hero_x:
xd = 1
else:
xd = 0
if enemy_y > hero_y:
yd = -1
elif enemy_y < hero_y:
yd = 1
else:
yd = 0
hero_actions.append({'cmd': 'move', 'xd': xd, 'yd': yd})
return hero_actions
def main():
# Init game world
world = World(conf_path=os.path.realpath('config-example.json'))
world.logger.setLevel(logging.DEBUG)
# Start world generator
wg = world.world_gen()
wg.send(None)
while not world.game_over:
# Hero actions
hero_actions = get_hero_actions(world.world_stat['enemies'],
world.hero_x, world.hero_y)
try:
wg.send(hero_actions)
except StopIteration:
continue
print(world.world_stat)
if __name__ == '__main__':
main()