-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameInterface.py
86 lines (57 loc) · 2.28 KB
/
GameInterface.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
import random
import typing
import cv2
import numpy as np
from Game import GameCore
class GameInterface:
ACTION_NUM = 16
SIMULATE_FPS = 60
FEATURE_MAP_WIDTH, FEATURE_MAP_HEIGHT = 16, 20
def __init__(self) -> None:
self.game = GameCore()
self.action_num = GameInterface.ACTION_NUM
self.action_segment_len = self.game.width / GameInterface.ACTION_NUM
def reset(self, seed: int = None) -> None:
self.game.reset(seed)
def simulate_until_stable(self) -> None:
self.game.update_until_stable(GameInterface.SIMULATE_FPS)
def decode_action(self, action: int) -> typing.Tuple[int, int]:
x = int((action + 0.5) * self.action_segment_len)
return (x, 0)
def next(self, action: int) -> typing.Tuple[np.ndarray, int, bool]:
current_fruit = self.game.current_fruit_type
score_1 = self.game.score
self.game.click(self.decode_action(action))
self.simulate_until_stable()
feature = self.game.get_features(
GameInterface.FEATURE_MAP_WIDTH, GameInterface.FEATURE_MAP_HEIGHT
)
score_2 = self.game.score
score, reward, alive = self.game.score, score_2 - score_1, self.game.alive
reward = reward if reward > 0 else -current_fruit
flatten_feature = feature.flatten().astype(np.float32)
# flatten_feature = np.expand_dims(feature.flatten(), axis=0).astype(np.float32)
return flatten_feature, reward, alive
def auto_play(self):
WINNAME, VIDEO_FPS = "fruit-merger", 5
cv2.namedWindow(WINNAME)
while True:
action = random.randint(0, self.action_num - 1)
feature, reward, alive = self.next(action)
self.game.draw()
cv2.imshow(WINNAME, self.game.__screen)
key = cv2.waitKey(int(1000 / VIDEO_FPS))
print(feature.shape)
if not alive:
self.game.rclick((0, 0))
# if key != -1:
# print(key)
if key == ord("q") or key == 27:
break
# close the window
if cv2.getWindowProperty(WINNAME, cv2.WND_PROP_VISIBLE) <= 0:
break
cv2.destroyAllWindows()
if __name__ == "__main__":
gi = GameInterface()
gi.auto_play()