-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (112 loc) · 4.1 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
129
130
131
132
133
134
135
import pygame
import time
pygame.init()
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 300
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("TicTacToe tu connais")
# Décor du jeu
line_width = 5
background = "antiquewhite"
grid = "black"
# Nombre de coup dans la partie et switch entre tour joueur 1 et joueur 2
coup = 0
tour = 1
# Ajout des polices
font = pygame.font.SysFont(None, 40)
# Position des croix et cercles
T = set()
def draw_background():
screen.fill(background)
for x in range(3):
pygame.draw.line(screen, grid, (0, x * 100), (SCREEN_WIDTH, x * 100), line_width) #line(surface, color, start_pos, end_pos, width)
pygame.draw.line(screen, grid, (x * 100, 0), (x * 100, SCREEN_HEIGHT), line_width)
def croix():
x = col * 100 + 50
y = row * 100 + 50
pygame.draw.line(screen, "red", (x - 40, y - 40), (x + 40, y + 40), line_width)
pygame.draw.line(screen, "red", (x - 40, y + 40), (x + 40, y - 40), line_width)
return x, y
def rond():
x = col * 100 + 50
y = row * 100 + 50
pygame.draw.circle(screen, "blue", (x, y), 40, line_width)
return x, y
def position_jouee():
global coup, tour
x, y = row, col
coord = (x, y, tour)
if coord not in T:
T.add(coord)
coup += 1
print(T)
def check_victoire():
for ligne in range(0, 3): # joueur 1
if (ligne, 0, 1) in T and (ligne, 1, 1) in T and (ligne, 2, 1) in T:
return True
for colonne in range(0, 3):
if (0, colonne, 1) in T and (1, colonne, 1) in T and (2, colonne, 1) in T:
return True
if (0, 0, 1) in T and (1, 1, 1) in T and (2, 2, 1) in T:
return True
if (0, 2, 1) in T and (1, 1, 1) in T and (2, 0, 1) in T:
return True
for ligne in range(0, 3): # joueur 2
if (ligne, 0, 0) in T and (ligne, 1, 0) in T and (ligne, 2, 0) in T:
return True
for colonne in range(0, 3):
if (0, colonne, 0) in T and (1, colonne, 0) in T and (2, colonne, 0) in T:
return True
if (0, 0, 0) in T and (1, 1, 0) in T and (2, 2, 0) in T:
return True
if (0, 2, 0) in T and (1, 1, 0) in T and (2, 0, 0) in T:
return True
return False
def afficher_message_victoire(joueur):
message = font.render(f"Victoire du joueur {joueur} !", True, "black")
screen.blit(message, (10, SCREEN_HEIGHT // 2))
def victoire(joueur):
global run
print(f"Victoire du joueur 1 ! \nNombre de coup : {coup}")
afficher_message_victoire(joueur)
pygame.display.update()
time.sleep(3)
run = False
def egalite():
global coup, run
if coup == 9 and check_victoire() == False:
message_match_nul = font.render(f"Match nul !", True, "black")
screen.blit(message_match_nul, (70, SCREEN_HEIGHT // 2))
pygame.display.update()
time.sleep(3)
run = False
run = True
draw_background()
while run == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 1 c'est clique gauche
col = event.pos[0] // 100 #event.pos[O] c'est pour réccupérer la position x de la souris
row = event.pos[1] // 100 #event.pos[1] c'est pour réccupérer la position y de la souris
if tour == 1:
if (row, col, 1) not in T and (row, col, 0) not in T:
croix()
position_jouee()
if check_victoire() == True:
victoire(1)
egalite()
tour -= 1
#print(tour, coup)
else:
if (row, col, 1) not in T and (row, col, 0) not in T:
rond()
position_jouee()
if check_victoire() == True:
victoire(2)
egalite()
tour += 1
#print(tour, coup)
pygame.display.update()
pygame.quit()