-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (104 loc) · 2.86 KB
/
main.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
import pygame
import sys
import numpy as np
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
SQUARE_LENGTH = SCREEN_WIDTH / 3
pygame.display.set_caption("Tic Tac Toe")
clock = pygame.time.Clock()
# colors
TURQUOISE = pygame.Color("#14bdac")
DARK_TURQUOISE = pygame.Color("#0da192")
COLOR1 = (84, 84, 84)
COLOR2 = (242, 235, 211)
# surfaces
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# board
board = np.zeros((3, 3))
def draw_board():
screen.fill(TURQUOISE)
draw_lines()
for row in range(3):
for col in range(3):
# if board[row][col] == 1:
# # draw_cross()
if board[row][col] == 2:
pygame.draw.circle(
screen,
COLOR2,
(
col * (SQUARE_LENGTH) + SQUARE_LENGTH / 2,
row * (SQUARE_LENGTH) + SQUARE_LENGTH / 2,
),
SQUARE_LENGTH / 2.5,
10,
)
def draw_lines():
# left vertical
pygame.draw.line(
screen,
DARK_TURQUOISE,
(SCREEN_WIDTH / 3, 0),
(SCREEN_WIDTH / 3, SCREEN_HEIGHT),
width=15,
)
# right vertical
pygame.draw.line(
screen,
DARK_TURQUOISE,
(2 * SCREEN_WIDTH / 3, 0),
(2 * SCREEN_WIDTH / 3, SCREEN_HEIGHT),
width=15,
)
# top horizontal
pygame.draw.line(
screen,
DARK_TURQUOISE,
(0, SCREEN_HEIGHT / 3),
(SCREEN_WIDTH, SCREEN_HEIGHT / 3),
width=15,
)
# bottom horizontal
pygame.draw.line(
screen,
DARK_TURQUOISE,
(0, 2 * SCREEN_HEIGHT / 3),
(SCREEN_WIDTH, 2 * SCREEN_HEIGHT / 3),
width=15,
)
def mark_square(row, col, player):
board[row][col] = player
def is_available(row, col):
return board[row][col] == 0
# maps pygame's screen coordinates to board coordinates
def pygame_to_board(pgX, pgY):
return int(pgY // (SQUARE_LENGTH)), int(pgX // (SQUARE_LENGTH))
PLAYER1 = True
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
pgX, pgY = pygame.mouse.get_pos()
boardX, boardY = pygame_to_board(pgX, pgY)
print(boardX, boardY)
if is_available(boardX, boardY):
if PLAYER1:
mark_square(boardX, boardY, 1)
PLAYER1 = False
else:
mark_square(boardX, boardY, 2)
PLAYER1 = True
draw_board()
# pygame.draw.circle(
# screen,
# COLOR2,
# (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2),
# SCREEN_WIDTH / 7,
# 15,
# )
pygame.display.flip()
clock.tick(60)