-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
174 lines (147 loc) · 5.02 KB
/
tictactoe.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
# import modules
import pygame
from pygame.locals import *
pygame.init()
screen_height = 300
screen_width = 300
line_width = 6
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Tic Tac Toe')
# define colours
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# define font
font = pygame.font.SysFont(None, 40)
# define variables
clicked = False
player = 1
pos = (0, 0)
markers = []
game_over = False
winner = 0
# setup a rectangle for "Play Again" Option
again_rect = Rect(screen_width // 2 - 80, screen_height // 2, 160, 50)
# create empty 3 x 3 list to represent the grid
for x in range(3):
row = [0] * 3
markers.append(row)
def draw_board():
bg = (255, 255, 210)
grid = (50, 50, 50)
screen.fill(bg)
for x in range(1, 3):
pygame.draw.line(screen, grid, (0, 100 * x), (screen_width, 100 * x), line_width)
pygame.draw.line(screen, grid, (100 * x, 0), (100 * x, screen_height), line_width)
def draw_markers():
x_pos = 0
for x in markers:
y_pos = 0
for y in x:
if y == 1:
pygame.draw.line(screen, red, (x_pos * 100 + 15, y_pos * 100 + 15),
(x_pos * 100 + 85, y_pos * 100 + 85), line_width)
pygame.draw.line(screen, red, (x_pos * 100 + 85, y_pos * 100 + 15),
(x_pos * 100 + 15, y_pos * 100 + 85), line_width)
if y == -1:
pygame.draw.circle(screen, green, (x_pos * 100 + 50, y_pos * 100 + 50), 38, line_width)
y_pos += 1
x_pos += 1
def check_game_over():
global game_over
global winner
x_pos = 0
for x in markers:
# check columns
if sum(x) == 3:
winner = 1
game_over = True
if sum(x) == -3:
winner = 2
game_over = True
# check rows
if markers[0][x_pos] + markers[1][x_pos] + markers[2][x_pos] == 3:
winner = 1
game_over = True
if markers[0][x_pos] + markers[1][x_pos] + markers[2][x_pos] == -3:
winner = 2
game_over = True
x_pos += 1
# check cross
if markers[0][0] + markers[1][1] + markers[2][2] == 3 or markers[2][0] + markers[1][1] + markers[0][2] == 3:
winner = 1
game_over = True
if markers[0][0] + markers[1][1] + markers[2][2] == -3 or markers[2][0] + markers[1][1] + markers[0][2] == -3:
winner = 2
game_over = True
# check for tie
if game_over == False:
tie = True
for row in markers:
for i in row:
if i == 0:
tie = False
# if it is a tie, then call game over and set winner to 0 (no one)
if tie == True:
game_over = True
winner = 0
def draw_game_over(winner):
if winner != 0:
end_text = "Player " + str(winner) + " wins!"
elif winner == 0:
end_text = "You have tied!"
end_img = font.render(end_text, True, blue)
pygame.draw.rect(screen, green, (screen_width // 2 - 100, screen_height // 2 - 60, 200, 50))
screen.blit(end_img, (screen_width // 2 - 100, screen_height // 2 - 50))
again_text = 'Play Again?'
again_img = font.render(again_text, True, blue)
pygame.draw.rect(screen, green, again_rect)
screen.blit(again_img, (screen_width // 2 - 80, screen_height // 2 + 10))
# main loop
run = True
while run:
# draw board and markers first
draw_board()
draw_markers()
# handle events
for event in pygame.event.get():
# handle game exit
if event.type == pygame.QUIT:
run = False
# run new game
if game_over == False:
# check for mouseclick
if event.type == pygame.MOUSEBUTTONDOWN and clicked == False:
clicked = True
if event.type == pygame.MOUSEBUTTONUP and clicked == True:
clicked = False
pos = pygame.mouse.get_pos()
cell_x = pos[0] // 100
cell_y = pos[1] // 100
if markers[cell_x][cell_y] == 0:
markers[cell_x][cell_y] = player
player *= -1
check_game_over()
# check if game has been won
if game_over == True:
draw_game_over(winner)
# check for mouseclick to see if we clicked on Play Again
if event.type == pygame.MOUSEBUTTONDOWN and clicked == False:
clicked = True
if event.type == pygame.MOUSEBUTTONUP and clicked == True:
clicked = False
pos = pygame.mouse.get_pos()
if again_rect.collidepoint(pos):
# reset variables
game_over = False
player = 1
pos = (0, 0)
markers = []
winner = 0
# create empty 3 x 3 list to represent the grid
for x in range(3):
row = [0] * 3
markers.append(row)
# update display
pygame.display.update()
pygame.quit()