-
Notifications
You must be signed in to change notification settings - Fork 3
/
game_map_objects.py
130 lines (102 loc) · 3.45 KB
/
game_map_objects.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
class MovableGameMapObject(object):
"""Movable game map object."""
def __init__(self, position, direction):
self.position = position
self.direction = direction
@classmethod
def from_ram(cls, position, ram):
direction = cls._get_direction(ram)
return MovableGameMapObject(position, direction)
@classmethod
def _get_direction(cls, ram):
direction_ram = ram & 3
direction = \
[-1, 0] if direction_ram == 0 else \
[0, 1] if direction_ram == 1 else \
[1, 0] if direction_ram == 2 else \
[0, -1]
return direction
class Ghost(MovableGameMapObject):
"""Ghost."""
BAD = 0
GOOD = 1
def __init__(self, position, direction, state):
super(Ghost, self).__init__(position, direction)
self.state = state
@classmethod
def from_ram(cls, position, ram):
direction = cls._get_direction(ram)
edible_ram = (ram >> 7) & 1
state = \
cls.GOOD if edible_ram == 1 else \
cls.BAD
return cls(position, direction, state)
class Fruit(MovableGameMapObject):
"""Fruit."""
def __init__(self, position, direction, exists):
super(Fruit, self).__init__(position, direction)
self.exists = exists
@classmethod
def from_ram(cls, position, ram, exists):
# Direction is probably bad...
direction = cls._get_direction(ram)
return cls(position, direction, exists)
class GameMapObjects(object):
"""Game map object enumerations."""
EMPTY = 0
WALL = 1
PELLET = 2
POWER_UP = 3
GOOD_GHOST = 4
BAD_GHOST = 5
FRUIT = 6
MS_PACMAN = 7
@classmethod
def to_reward(cls, classification):
"""Converts a GameMapObject to a reward.
Args:
classification: GameMapObject.
Returns:
Reward.
"""
reward = 0
if classification == GameMapObjects.WALL:
reward = 0
elif classification == GameMapObjects.PELLET:
reward = 10
elif classification == GameMapObjects.POWER_UP:
reward = 50
elif classification == GameMapObjects.GOOD_GHOST:
reward = 200
elif classification == GameMapObjects.BAD_GHOST:
reward = -100
elif classification == GameMapObjects.FRUIT:
reward = 100
elif classification == GameMapObjects.MS_PACMAN:
reward = 0
return reward
@classmethod
def to_color(cls, classification):
"""Converts a GameMapObject to an BGR color.
Args:
classification: GameMapObject.
Returns:
BGR color.
"""
color = [136, 28, 0] # Dark blue.
if classification == GameMapObjects.WALL:
color = [111, 111, 228] # Pink-ish.
elif classification == GameMapObjects.PELLET:
color = [255, 255, 255] # White.
elif classification == GameMapObjects.POWER_UP:
color = [255, 255, 0] # Cyan.
elif classification == GameMapObjects.GOOD_GHOST:
color = [0, 255, 0] # Green.
elif classification == GameMapObjects.BAD_GHOST:
color = [0, 0, 255] # Red.
elif classification == GameMapObjects.FRUIT:
color = [255, 0, 255] # Magenta.
elif classification == GameMapObjects.MS_PACMAN:
color = [0, 255, 255] # Yellow.
return color