morpion.py

Created by lamhassni-bilal

Created on May 03, 2023

8.19 KB


from turtle import *
from ion import *
from kandinsky import *
 
speed(100)
matrice = []
gagnant = fin_partie = 0
H, L = 320, 222
 
draw_string("Appuyez sur EXE pour continuer", 20, 40, (255, 128, 0))
draw_string(
    "Règles du jeu : \n\n  ·Chaque joueur possède un \n  symbole différent.\n  ·Le premier avec 3 symboles\n  alignés gagne.",
    0, 75, (0, 0, 0))
while not (keydown(KEY_EXE)):
    True
 
fill_rect(0, 25, 350, 190, 'white')
 
draw_string("TOUCHES :", 95, 0, (255, 0, 0))
draw_string(
    "-Chaque case est numérotée de\n 1 à 9,la première est en bas\n à gauche, la 9ème en haut \n à droite, appuyez sur ces\n chiffres pour poser\n votre symbole. \n-Le joueur1 commence la partie \n-EXE (à la fin) : relancer",
    0, 20, (0, 0, 0))
draw_string("Appuyez sur OK pour commencer", 15, 200, (255, 128, 0))
while not (keydown(KEY_OK)):
    True
 
fill_rect(0, 0, 400, 250, 'white')
 
 
def parent():
    while 1:
        if keydown(52) or keydown(4):
            fill_rect(0, 0, 320, 222, "white")
            game()
 
 
def cercle():  # dessin du cercle
    penup()
    forward(20)
    pendown()
    left(90)
    circle(20)
    setheading(0)
    penup()
    left(90)
    penup()
    backward(20)
 
 
def croix():  # dessin de la croix
    pensize(3)
    right(45)
    pendown()
    for i in range(4):
        right(90)
        forward(40)
        penup()
        backward(40)
        pendown()
    setheading(90)
    penup()
 
 
def victoire_cercle():
    global fin_partie
    draw_string("Joueur2 a gagné la partie", 50, 0, (255, 0, 0))
    fin_partie = 1
 
 
def victoire_croix():
    global fin_partie
    draw_string("Joueur1 a gagné la partie", 50, 0, (255, 0, 0))
    fin_partie = 1
 
 
def verification_cercle():
    global matrice, gagnant, fin_partie
    for i in range(3):
        if matrice[i][0] == matrice[i][1] == matrice[i][2] == 2:
            gagnant = 2
            victoire_cercle()
    for i in range(3):
        if matrice[0][i] == matrice[1][i] == matrice[2][i] == 2:
            gagnant = 2
            victoire_cercle()
    if matrice[0][0] == matrice[1][1] == matrice[2][2] == 2:
        gagnant = 2
        victoire_cercle()
    if matrice[0][2] == matrice[1][1] == matrice[2][0] == 2:
        gagnant = 2
        victoire_cercle()
 
 
def verification_croix():
    global matrice, gagnant, fin_partie
    for i in range(3):
        if matrice[i][0] == matrice[i][1] == matrice[i][2] == 1:
            gagnant = 1
            victoire_croix()
    for i in range(3):
        if matrice[0][i] == matrice[1][i] == matrice[2][i] == 1:
            gagnant = 1
            victoire_croix()
    if matrice[0][0] == matrice[1][1] == matrice[2][2] == 1:
        gagnant = 1
        victoire_croix()
    if matrice[0][2] == matrice[1][1] == matrice[2][0] == 1:
        gagnant = 1
        victoire_croix()
 
 
    # script pour la grille et def trait trouvé chez Schraf : Maths-info
 
def trait(x, y, a, d):
     penup()
     goto(x, y)
     pendown()
     setheading(a)
     forward(d)
 
 
def grille(nb):  # mise en forme de la grille de jeu
    pensize(2)
    case = min(H, L) // nb
    H_m, L_m = -nb * case / 2, -nb * case / 2
    for i in range(1, nb):
        trait(L_m, H_m + case * i, 0, nb * case)
        trait(L_m + case * i, H_m, 90, nb * case)
    return case, H_m, L_m
 
 
def game():
        global matrice, gagnant, fin_partie
        gagnant = fin_partie = 0
        grille(3)
        penup()
        goto(0, 0)
        penup()
        matrice = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]  # création de la matrice
        j = 1  # joueur numéro 1 commence à jouer
 
        while fin_partie == 0:  # partie
                if j == 1:
                    if keydown(KEY_ONE) and matrice[0][0] == " ":
                        goto(-75, -74)
                        croix()
                        matrice[0][0] = j
                        j = 2
                        verification_croix()
                    if keydown(KEY_TWO) and matrice[0][1] == " ":
                        goto(0, -74)
                        croix()
                        matrice[0][1] = j
                        j = 2
                    verification_croix()
                    if keydown(KEY_THREE) and matrice[0][2] == " ":
                        goto(75, -74)
                        croix()
                        matrice[0][2] = j
                        j = 2
                        verification_croix()
                    if keydown(KEY_FOUR) and matrice[1][0] == " ":
                        goto(-75, 0)
                        croix()
                        matrice[1][0] = j
                        j = 2
                        verification_croix()
                    if keydown(KEY_FIVE) and matrice[1][1] == " ":
                        goto(0, 0)
                        croix()
                        matrice[1][1] = j
                        j = 2
                        verification_croix()
                    if keydown(KEY_SIX) and matrice[1][2] == " ":
                        goto(75, 0)
                        croix()
                        matrice[1][2] = j
                        j = 2
                        verification_croix()
                    if keydown(KEY_SEVEN) and matrice[2][0] == " ":
                        goto(-75, 74)
                        croix()
                        matrice[2][0] = j
                        j = 2
                        verification_croix()
                    if keydown(KEY_EIGHT) and matrice[2][1] == " ":
                        goto(0, 74)
                        croix()
                        matrice[2][1] = j
                        j = 2
                        verification_croix()
                    if keydown(KEY_NINE) and matrice[2][2] == " ":
                        goto(75, 74)
                        croix()
                        matrice[2][2] = j
                        j = 2
                        verification_croix()
 
                if j == 2:
                    if keydown(KEY_ONE) and matrice[0][0] == " ":
                        goto(-75, -74)
                        cercle()
                        matrice[0][0] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_TWO) and matrice[0][1] == " ":
                        goto(0, -74)
                        cercle()
                        matrice[0][1] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_THREE) and matrice[0][2] == " ":
                        goto(75, -74)
                        cercle()
                        matrice[0][2] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_FOUR) and matrice[1][0] == " ":
                        goto(-75, 0)
                        cercle()
                        matrice[1][0] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_FIVE) and matrice[1][1] == " ":
                        goto(0, 0)
                        cercle()
                        matrice[1][1] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_SIX) and matrice[1][2] == " ":
                        goto(75, 0)
                        cercle()
                        matrice[1][2] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_SEVEN) and matrice[2][0] == " ":
                        goto(-75, 74)
                        cercle()
                        matrice[2][0] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_EIGHT) and matrice[2][1] == " ":
                        goto(0, 74)
                        cercle()
                        matrice[2][1] = j
                        j = 1
                        verification_cercle()
                    if keydown(KEY_NINE) and matrice[2][2] == " ":
                        goto(75, 74)
                        cercle()
                        matrice[2][2] = j
                        j = 1
                        verification_cercle()
        print('oout')
 
 
game()
print('out')
parent()