Skip to content

Commit fb5b69f

Browse files
committed
Move Colors into one place and General Cleanup
- Moved Colors into self.COLOR dict - Commented Rotation number - Added whitespace - Removed one line if statements
1 parent 5cefd7a commit fb5b69f

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

tictactoe.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,38 @@
1414

1515
class Game():
1616
def __init__(self, assetsPath, saveToCloud=True):
17+
# For the messagebox, to prevent a Tkinter window from popping up
18+
root = Tk()
19+
root.withdraw()
20+
1721
self.grid = Grid()
22+
self.AI_DELAY = 1
23+
self.selectAI()
24+
# self.AIPlayer = AIPlayer(2, delay=self.AI_DELAY, name="Minimax AI")
25+
26+
self.COLOR = {
27+
'BLACK': (000, 000, 000),
28+
'BLUE': (145, 195, 220),
29+
'WHITE': (255, 255, 255)
30+
}
31+
32+
self.WINDOW_WIDTH = 400
33+
self.WINDOW_HEIGHT = 450
34+
35+
self.assets = {}
36+
self.loadAssets(assetsPath)
1837

1938
pygame.init()
2039
pygame.font.init()
21-
self.Font = pygame.font.SysFont('Calibri', 35)
40+
self.Font = pygame.font.SysFont('Calibri', 30)
2241
self.clock = pygame.time.Clock()
23-
self.assets = {}
24-
self.loadAssets(assetsPath)
2542

2643
pygame.display.set_caption('Tic Tac Toe')
2744
pygame.display.set_icon(self.assets['icon'])
2845

29-
self.WINDOW_WIDTH = 400
30-
self.WINDOW_HEIGHT = 450
3146
self.window = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT))
3247

3348
self.setupUI()
34-
35-
self.AIDelay = 1
36-
self.AIPlayer = AIPlayer(2, delay=self.AIDelay, name="Minimax AI")
3749
self.redraw()
3850

3951
self.running = True
@@ -65,22 +77,22 @@ def selectAI(self):
6577
try:
6678
AI = self.toggleButtonPool.get_selected().get_text()
6779
if AI == 'Random AI': self.AIPlayer = RandomAIPlayer(2, delay=0.5, name="Random AI") # Set to Random AI
68-
else: self.AIPlayer = AIPlayer(2, delay=self.AIDelay, name="Minimax AI") # Set to Minimax AI
80+
else: self.AIPlayer = AIPlayer(2, delay=self.AI_DELAY, name="Minimax AI") # Set to Minimax AI
6981
except AttributeError:
70-
self.AIPlayer = AIPlayer(2, delay=self.AIDelay, name="Minimax AI") # Set to Minimax AI if any error occurs
82+
self.AIPlayer = AIPlayer(2, delay=self.AI_DELAY, name="Minimax AI") # Set to Minimax AI if any error occurs
7183
except Exception as e:
7284
messagebox.showerror(e.__class__.__name__, e)
73-
self.AIPlayer = AIPlayer(2, delay=self.AIDelay, name="Minimax AI")
85+
self.AIPlayer = AIPlayer(2, delay=self.AI_DELAY, name="Minimax AI")
7486

7587
def setupUI(self):
7688
analyticsButton = thorpy.make_button(' Analytics ', func=lambda: Process(target=displayData).start())
77-
analyticsButton.set_main_color((145, 195, 220))
89+
analyticsButton.set_main_color(self.COLOR['BLUE'])
7890

7991
toggleButtons = [thorpy.Togglable('Minimax AI'), thorpy.Togglable('Random AI')]
8092
self.toggleButtonPool = thorpy.TogglablePool(toggleButtons, first_value=toggleButtons[0], always_value=True)
8193

8294
self.box = thorpy.Box(elements=toggleButtons + [analyticsButton])
83-
self.box.set_main_color((255, 255, 255))
95+
self.box.set_main_color(self.COLOR['WHITE'])
8496

8597
self.menu = thorpy.Menu(self.box)
8698

@@ -119,7 +131,7 @@ def saveGameData(self):
119131

120132
def redraw(self):
121133
self.window.fill((255, 255, 255))
122-
pygame.draw.rect(self.window, (255, 255, 255), (50, 75, 300, 300))
134+
pygame.draw.rect(self.window, self.COLOR['WHITE'], (50, 125, 300, 300))
123135
self.window.blit(self.assets['VLine'], (50 - 5 + 100, 125))
124136
self.window.blit(self.assets['VLine'], (50 - 5 + 200, 125))
125137
self.window.blit(self.assets['HLine'], (50, 125 + 100))
@@ -132,14 +144,18 @@ def redraw(self):
132144

133145
def displayTurn(self):
134146
playerName = "Your" if self.grid.currentPlayer == 1 else f"{self.AIPlayer.name}'s"
135-
currentTurnText = self.Font.render(f"{playerName} turn", True, (0, 0, 0))
147+
currentTurnText = self.Font.render(f"{playerName} turn", True, self.COLOR['BLACK'])
136148
rect = currentTurnText.get_rect(center=(self.WINDOW_WIDTH // 2, 60))
137149
self.window.blit(currentTurnText, rect)
138150

139151
def handleEvents(self):
140152
for event in pygame.event.get():
141-
if event.type == pygame.QUIT: self.running = False
142-
if event.type == pygame.MOUSEBUTTONDOWN: self.handleClick(pygame.mouse.get_pos())
153+
if event.type == pygame.QUIT:
154+
self.running = False
155+
156+
elif event.type == pygame.MOUSEBUTTONDOWN:
157+
mousePosition = pygame.mouse.get_pos()
158+
self.handleClick(mousePosition)
143159
self.menu.react(event)
144160

145161
def handleClick(self, mousePos):
@@ -156,9 +172,11 @@ def handleClick(self, mousePos):
156172

157173
def loadAssets(self, assetsPath):
158174
self.assets['VLine'] = pygame.image.load(path.join(*assetsPath, 'line.png'))
159-
self.assets['HLine'] = pygame.transform.rotate(self.assets['VLine'], 90)
175+
self.assets['HLine'] = pygame.transform.rotate(self.assets['VLine'], 90) # Rotate 90 degrees
176+
160177
self.assets['X'] = pygame.image.load(path.join(*assetsPath, 'X.png'))
161178
self.assets['O'] = pygame.image.load(path.join(*assetsPath, 'O.png'))
179+
162180
self.assets['icon'] = pygame.image.load(path.join(*assetsPath, 'icon.png'))
163181

164182

0 commit comments

Comments
 (0)