-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
167 lines (133 loc) · 6.2 KB
/
Copy pathmain.py
File metadata and controls
167 lines (133 loc) · 6.2 KB
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
import pygame, sys
from pyvidplayer import Video
pygame.init()
WIDTH, HEIGHT = 900, 900
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tic Tac Toe!")
BOARD = pygame.image.load("assets/Board.png")
X_IMG = pygame.image.load("assets/X.png")
O_IMG = pygame.image.load("assets/O.png")
BG_COLOR = (214, 201, 227)
board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
graphical_board = [[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]]]
to_move = 'X'
def render_board(board, ximg, oimg):
global graphical_board
for i in range(3):
for j in range(3):
if board[i][j] == 'X':
# Create an X image and rect
graphical_board[i][j][0] = ximg
graphical_board[i][j][1] = ximg.get_rect(center=(j*300+150, i*300+150))
elif board[i][j] == 'O':
graphical_board[i][j][0] = oimg
graphical_board[i][j][1] = oimg.get_rect(center=(j*300+150, i*300+150))
def render_board(board, ximg, oimg):
global graphical_board
for i in range(3):
for j in range(3):
if board[i][j] == 'X':
graphical_board[i][j][0] = ximg
graphical_board[i][j][1] = ximg.get_rect(center=(j*300+150, i*300+150))
elif board[i][j] == 'O':
graphical_board[i][j][0] = oimg
graphical_board[i][j][1] = oimg.get_rect(center=(j*300+150, i*300+150))
def add_XO(board, graphical_board, to_move):
current_pos = pygame.mouse.get_pos()
converted_x = (current_pos[0]-65)/835*2
converted_y = current_pos[1]/835*2
if board[round(converted_y)][round(converted_x)] != 'O' and board[round(converted_y)][round(converted_x)] != 'X':
board[round(converted_y)][round(converted_x)] = to_move
if to_move == 'O':
to_move = 'X'
else:
to_move = 'O'
render_board(board, X_IMG, O_IMG)
for i in range(3):
for j in range(3):
if graphical_board[i][j][0] is not None:
SCREEN.blit(graphical_board[i][j][0], graphical_board[i][j][1])
return board, to_move
def check_win(board):
winner = None
for row in range(0, 3):
if((board[row][0] == board[row][1] == board[row][2]) and (board [row][0] is not None)):
winner = board[row][0]
for i in range(0, 3):
graphical_board[row][i][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[row][i][0], graphical_board[row][i][1])
pygame.display.update()
return winner
for col in range(0, 3):
if((board[0][col] == board[1][col] == board[2][col]) and (board[0][col] is not None)):
winner = board[0][col]
for i in range(0, 3):
graphical_board[i][col][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[i][col][0], graphical_board[i][col][1])
pygame.display.update()
return winner
if (board[0][0] == board[1][1] == board[2][2]) and (board[0][0] is not None):
winner = board[0][0]
graphical_board[0][0][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[0][0][0], graphical_board[0][0][1])
graphical_board[1][1][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[1][1][0], graphical_board[1][1][1])
graphical_board[2][2][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[2][2][0], graphical_board[2][2][1])
pygame.display.update()
return winner
if (board[0][2] == board[1][1] == board[2][0]) and (board[0][2] is not None):
winner = board[0][2]
graphical_board[0][2][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[0][2][0], graphical_board[0][2][1])
graphical_board[1][1][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[1][1][0], graphical_board[1][1][1])
graphical_board[2][0][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[2][0][0], graphical_board[2][0][1])
pygame.display.update()
return winner
if winner is None:
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] != 'X' and board[i][j] != 'O':
return None
return "DRAW"
vid = Video("Tic Tac Toe Video.mp4")
vid.set_size((900, 900))
def intro():
while True:
vid.draw(SCREEN, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
vid.close()
main_game()
def main_game():
global board, graphical_board, to_move
game_finished = False
SCREEN.fill(BG_COLOR)
SCREEN.blit(BOARD, (64, 64))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
board, to_move = add_XO(board, graphical_board, to_move)
if game_finished:
board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
graphical_board = [[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]]]
to_move = 'X'
SCREEN.fill(BG_COLOR)
SCREEN.blit(BOARD, (64, 64))
game_finished = False
pygame.display.update()
if check_win(board) is not None:
game_finished = True
pygame.display.update()
intro()