-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.py
162 lines (122 loc) · 4.09 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
#import modules
import pygame
from pygame.locals import *
pygame.init()
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 300
screen = pygame.display.set_mode((SCREEN_HEIGHT,SCREEN_WIDTH))
pygame.display.set_caption("TicTacToe")
#define variables
LINE_WIDTH=7
MARKERS=[]
clicked=False
pos=[]
player=1
winner = 0
game_over=False
#define colors
green = (0,255,0)
red = (255,0,0)
blue = (0,0,255)
#define font
font = pygame.font.SysFont(None,40)
#create play again rectangle
again_rect= Rect(SCREEN_WIDTH//2-80,SCREEN_HEIGHT//2,160,50)
def draw_grid():
background=(255,255,200)
grid=(50,50,50)
screen.fill(background)
for x in range(1,3):
pygame.draw.line(screen,grid,(0,x*100),(SCREEN_WIDTH,x*100),LINE_WIDTH)
pygame.draw.line(screen,grid,(x*100,0),(x*100,SCREEN_HEIGHT),LINE_WIDTH)
for x in range(3):
row = [0,0,0] #or: [0]*3
MARKERS.append(row)
def draw_markers():
x_pos = 0
for x in MARKERS:
y_pos = 0
for y in x:
if y == 1:
pygame.draw.line(screen,green, (x_pos * 100+15,y_pos*100+15),(x_pos*100+85,y_pos*100+85),LINE_WIDTH)
pygame.draw.line(screen,green, (x_pos * 100+15,y_pos*100+85),(x_pos*100+85,y_pos*100+15),LINE_WIDTH)
if y == -1:
pygame.draw.circle(screen,red,(x_pos*100+50,y_pos*100+50),38,LINE_WIDTH)
y_pos += 1
x_pos+=1
def check_winner():
global winner
global game_over
y_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][y_pos] + MARKERS[1][y_pos]+MARKERS[2][y_pos] == 3:
winner = 1
game_over = True
if MARKERS[0][y_pos] + MARKERS[1][y_pos]+MARKERS[2][y_pos] == -3:
winner = 2
game_over = True
y_pos += 1
#check cross
if MARKERS[0][0]+MARKERS[1][1]+MARKERS[2][2] == 3 or MARKERS[2][2]+MARKERS[1][1]+MARKERS[0][0] == -3:
winner = 1
game_over = True
if MARKERS[0][0]+MARKERS[1][1]+MARKERS[2][2] == -3 or MARKERS[2][2]+MARKERS[1][1]+MARKERS[0][0] == 3:
winner = 2
game_over = True
def draw_winner(winner):
win_text = "Player "+str(winner) + " wins!"
win_img = font.render(win_text,True,blue)
pygame.draw.rect(screen,green,(SCREEN_WIDTH//2-100,SCREEN_HEIGHT//2-60,200,50))
screen.blit(win_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))
run=True
while run:
draw_grid()
draw_markers()
#add event handlers
for event in pygame.event.get():
if event.type==pygame.QUIT:
run = False
if game_over == 0:
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]
cell_y = pos[1]
if MARKERS[cell_x//100][cell_y//100]==0:
MARKERS[cell_x//100][cell_y//100] = player
player *= -1
check_winner()
if game_over == True:
draw_winner(winner)
#check for mouseclick to see if user has 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
MARKERS=[]
pos=[]
player=1
winner = 0
game_over=False
for x in range(3):
row = [0,0,0] #or: [0]*3
MARKERS.append(row)
pygame.display.update()
pygame.quit()