-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardinals.py
52 lines (42 loc) · 1.14 KB
/
cardinals.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
from pygame.locals import *
from vector import Vector2
from enum import enum
from random import choice
class Cardinal(object):
directions = enum('NORTH', 'EAST', 'WEST', 'SOUTH')
key_map = {}
key_map[K_UP] = directions.NORTH
key_map[K_DOWN] = directions.SOUTH
key_map[K_LEFT] = directions.WEST
key_map[K_RIGHT] = directions.EAST
names = {}
names[directions.NORTH] = "North"
names[directions.SOUTH] = "South"
names[directions.WEST] = "West"
names[directions.EAST] = "East"
values = {}
values[directions.NORTH] = Vector2(0, -1)
values[directions.SOUTH] = Vector2(0, 1)
values[directions.WEST] = Vector2(-1, 0)
values[directions.EAST] = Vector2(1, 0)
@staticmethod
def choice():
return choice(Cardinal.values.values())
@staticmethod
def toward(pos, tar):
diff = Vector2(tar)
diff -= pos
if pos == tar:
return None
if abs(diff[0]) > abs(diff[1]):
if diff[0] < 0:
return Cardinal.directions.WEST
else:
return Cardinal.directions.EAST
else:
if diff[1] < 0:
return Cardinal.directions.NORTH
else:
return Cardinal.directions.SOUTH
if __name__ == '__main__' :
print K_UP in Cardinal.key_map.keys()