-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameInterface.py
183 lines (134 loc) · 6.2 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from autopy import mouse, bitmap, color
from gameState import Point, GameState, Gem, RGB
import time
import os
# Image directory
IMG_DIR = os.path.join(os.getcwd(), 'images')
# Path to the calibration image
CALIBRATION_IMAGE_PATH = os.path.join(IMG_DIR, 'hint.bmp')
# Path to the submit button image
SUBMIT_IMAGE_PATH = os.path.join(IMG_DIR, 'submit.bmp')
# Path to the replay button image
REPLAY_IMAGE_PATH = os.path.join(IMG_DIR, 'replay.bmp')
# Game offset relative to the calibration image
GAME_OFFSET = Point(-66, -354)
# Total size of the game
GAME_SIZE = Point(640, 480)
# Board offset relative to the game
BOARD_OFFSET = Point(198, 60)
# Distance between pieces
PIECE_OFFSET = Point(47, 47)
# Color constants
COLOR_CONSTANTS = {'white': RGB(232, 232, 232),
'blue': RGB(12, 120, 235),
'orange': RGB(242, 155, 65),
'yellow': RGB(253, 240, 33),
'red': RGB(247, 27, 56),
'purple': RGB(215, 20, 215),
'green': RGB(37, 205, 70)}
# Provides an abstract way to interact with the game
class GameInterface:
# Takes boardDim, a Point representing the board dimensions (8x8 board is represented by Point(8,8))
def __init__(self, boardDim):
self.gameOffset = Point()
self.boardOffset = Point()
self.gemImgs = {}
self.gameState = GameState(boardDim)
self.calibrate()
self.submitImg = bitmap.Bitmap.open(SUBMIT_IMAGE_PATH)
self.replayImg = bitmap.Bitmap.open(REPLAY_IMAGE_PATH)
# Sets the location of the top left of the board - all other points are represented relative to the gameOffset
def calibrate(self):
calibrationImg = bitmap.Bitmap.open(CALIBRATION_IMAGE_PATH)
bmp = bitmap.capture_screen()
(x, y) = bmp.find_bitmap(calibrationImg)
self.gameOffset = Point(x, y) + GAME_OFFSET
self.boardOffset = self.gameOffset + BOARD_OFFSET
# Reads the board from the screen and returns a GameState
def readGame(self):
bmp = bitmap.capture_screen()
submitPt = bmp.find_bitmap(self.submitImg)
replayPt = bmp.find_bitmap(self.replayImg)
if submitPt != None or replayPt != None:
if submitPt != None:
print 'submit found!'
mouse.move(submitPt[0], submitPt[1])
mouse.click()
time.sleep(10)
self.replayGame()
return self.gameState
for y in range(self.gameState.boardDim.y):
for x in range(self.gameState.boardDim.x):
gem = self.getGem(bmp, Point(x, y))
self.gameState.board.board[y][x] = gem
return self.gameState
# Returns a Gem given a screenshot and board coordinates
def getGem(self, bmp, point):
absPt = self.boardToAbsPt(point)
halfSize = 5
total = 0
totalColor = RGB()
for x in range(absPt.x - halfSize, absPt.x + halfSize):
for y in range(absPt.y - halfSize, absPt.y + halfSize):
hexColor = bmp.get_color(x, y)
r, g, b = color.hex_to_rgb(hexColor)
rgb = RGB(r, g, b)
totalColor += rgb
total += 1
avgRGB = totalColor / total
gemColor = self.RGBToGem(avgRGB)
return Gem(gemColor, 'status', point)
# Finds the gem color closest to the given RGB value
def RGBToGem(self, RGB):
minDistance = None;
color = 'none'
for key, value in COLOR_CONSTANTS.items():
if RGB.distSquared(value) < minDistance or minDistance == None:
color = key
minDistance = RGB.distSquared(value)
return color
# Click and drag the mouse to make a move - takes a Move object
# Attempts to place the cursor back where it found it
def makeMove(self, move):
self.gameState.makeMove(move)
firstPt, secondPt = move.pointTuple()
absFirst = self.boardToAbsPt(firstPt)
absSecond = self.boardToAbsPt(secondPt)
lastX, lastY = mouse.get_pos()
mouse.move(absFirst.x, absFirst.y)
mouse.toggle(True)
mouse.move(absSecond.x, absSecond.y)
mouse.toggle(False)
mouse.move(lastX, lastY)
# Move mouse off the board
def moveOffBoard(self):
mouse.move(self.gameOffset.x - 10, self.gameOffset.y - 10)
# Returns True if the mouse is in the exit box (10x10 pix block at the top left of the game)
def isMouseAtExit(self):
(x, y) = mouse.get_pos()
#return x > self.gameOffset.x and x < self.gameOffset.x + GAME_SIZE.x and y > self.gameOffset.y and y < self.gameOffset.y + GAME_SIZE.y
return x > self.gameOffset.x and x < self.gameOffset.x + 10 and y > self.gameOffset.y and y < self.gameOffset.y + 10
# Converts board coordinates to absolute screen coordinates (the center of the tile)
def boardToAbsPt(self, boardPt):
absX = self.boardOffset.x + boardPt.x * PIECE_OFFSET.x + PIECE_OFFSET.x / 2
absY = self.boardOffset.y + boardPt.y * PIECE_OFFSET.y + PIECE_OFFSET.y / 2
return Point(absX, absY)
def replayGame(self):
print 'replay!!!'
replayPt = None
time.sleep(5)
while replayPt == None:
bmp = bitmap.capture_screen()
replayPt = bmp.find_bitmap(self.replayImg)
mouse.move(replayPt[0], replayPt[1])
mouse.click()
time.sleep(60)
mouse.move(self.gameOffset.x + GAME_SIZE.x / 2, self.gameOffset.y + GAME_SIZE.y / 2)
mouse.click()
time.sleep(2)
mouse.move(self.gameOffset.x + 100, self.gameOffset.y + 100)
mouse.click()
if __name__ == '__main__':
#time.sleep(2)
gi = GameInterface(Point(8, 8))
print gi.replayGame()