-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
123 lines (115 loc) · 3.65 KB
/
play.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
from pynput.keyboard import Key, Controller
import time
import cv2 as cv
from Frame import Frames
FPS = 30
def playWithOutModel():
frame = Frames()
try:
fps = FPS
score = 0
while True:
img, x, y, vx, vy, score, stage = frame.oneFrame()
cv.imshow('playing', img)
if cv.waitKey(1) & 0xFF == ord('q'):
cv.destroyAllWindows()
break
time.sleep(1/fps)
except KeyboardInterrupt:
print("KeyboardInterrupt")
cv.destroyAllWindows()
pass
finally:
cv.destroyAllWindows()
pass
return score
def play(left_model=None, right_model=None):
if left_model is None or right_model is None:
return playWithOutModel()
keyboard = Controller()
frame = Frames()
fps = FPS
bestscore = 0
num_l_move = 0
num_r_move = 0
state_prime = [0, 0, 0, 0]
left = 0
right = 0
try:
while True:
img, x, y, vx, vy, score, stage = frame.oneFrame()
if score > bestscore:
bestscore = score
# print("score", bestscore)
cv.imshow('playing', img)
if stage == 1:
# game to restart
restart()
bestscore = 0
elif stage == 2:
# ball at start, press space to start
start()
elif stage == 3:
# game over
time.sleep(5)
img, x, y, vx, vy, score, stage = frame.oneFrame()
frame.findCancel()
return bestscore
else:
# train model based on last action and this state
state = state_prime
state_prime = [x, y, vx, vy]
reward = get_reward(score, num_l_move, num_r_move)
left_model.memory.put((state, left, reward, state_prime))
right_model.memory.put((state, right, reward, state_prime))
# get action from model
left = left_model.get_action(state_prime, False)
right = right_model.get_action(state_prime, False)
print(left, right)
if left > 0.5 and right > 0.5:
# press letter Z and /
keyboard.press('z')
keyboard.press('/')
time.sleep(0.1)
keyboard.release('z')
keyboard.release('/')
num_l_move += 1
num_r_move += 1
elif left > 0.5:
# press letter Z
keyboard.press('z')
time.sleep(0.1)
keyboard.release('z')
num_l_move += 1
elif right > 0.5:
# press letter /
keyboard.press('/')
time.sleep(0.1)
keyboard.release('/')
num_r_move += 1
if cv.waitKey(1) & 0xFF == ord('q'):
cv.destroyAllWindows()
break
time.sleep(1/fps)
except KeyboardInterrupt:
print("KeyboardInterrupt")
cv.destroyAllWindows()
pass
finally:
cv.destroyAllWindows()
pass
return bestscore
def start():
keyboard = Controller()
keyboard.press(Key.space)
time.sleep(3)
keyboard.release(Key.space)
def restart():
keyboard = Controller()
# press F2
keyboard.press(Key.f2)
time.sleep(0.01)
keyboard.release(Key.f2)
time.sleep(1)
def get_reward(score, num_l_move, num_r_move):
return score - num_l_move - num_r_move