-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
48 lines (43 loc) · 1.33 KB
/
utils.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
import math
import os
import sys
def resourcePath(relative):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative)
return os.path.join(relative)
def inputToIndex(userInput):
tokens = userInput.split(" ")
if(len(tokens) == 2 and len(tokens[0]) == 2 and len(tokens[1]) == 2):
firstPos = tokens[0]
secondPos = tokens[1]
atoi = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7}
firstCol = atoi.get(firstPos[0])
secondCol = atoi.get(secondPos[0])
try:
firstRow = int(firstPos[1])
secondRow = int(secondPos[1])
except ValueError:
return None
if(firstCol is None or secondCol is None or
firstRow < 1 or firstRow > 8 or secondRow < 1 or secondRow > 8):
return None
return ((8 - firstRow, firstCol), (8 - secondRow, secondCol))
else:
return None
def pygameToBoardIndex(screenPos):
xnew = math.floor((screenPos[1] - 20) / 50)
ynew = math.floor((screenPos[0] - 20) / 50)
return (xnew, ynew)
def pygameToBoardRect(screenPos):
# Board index is inverted from pygame index.
y, x = pygameToBoardIndex(screenPos)
xScreen = x * 50 + 20
yScreen = y * 50 + 20
width = 50
height = 50
return (xScreen, yScreen, width, height)
def rectOffset(rect, off):
newRect = rect.copy()
newRect.left += off
newRect.top += off
return newRect