morpion.py

Created by corentin-katic84

Created on May 03, 2023

8.51 KB

ce script permet de jouer au morpion


from turtle import *
from ion import *
from kandinsky import *
from time import*

speed(100)

draw_string("Appuyez sur EXE pour continuer",20,40,(255,128,0))
draw_string("Règles du jeu : \n\n  ·Chaques joueurs possèdent un \n  symbole différent.\n  ·Le premier avec 3 symboles\n  sur une ligne 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("-Chaques cases 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",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 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():  
    draw_string("Joueur2 a gagné la partie !",100,0,(255,0,0))
def victoire_croix():
    draw_string("Joueur1 a gagné la partie !",100,0,(255,0,0))
    
def verification_cercle():
            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()
            elif ' ' not in [case for ligne in matrice for case in ligne]:
                draw_string("Match nul !", 100, 0, (255, 0, 0))
                fin_partie = 1
            else:
                fin_partie = 0
                
                
def verification_croix():
            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()
            elif ' ' not in [case for ligne in matrice for case in ligne]:
                draw_string("Match nul !", 100, 0, (255, 0, 0))
                fin_partie = 1
            else:
                fin_partie = 0
                
                
fin_partie = 0


while 1:
    # script pour la grille et def trait trouvé chez Schraf : Maths-info
    H, L =320 , 222
    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
        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
    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
        indice = 9
        while indice == 9: #tour
            if j == 1:
                if keydown(KEY_ONE) and matrice[0][0] == " ":
                    indice = 0
                    goto(-75,-74)
                    croix()
                    matrice[0][0] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_TWO) and matrice[0][1] == " ":
                    indice = 1
                    goto(0,-74)
                    croix()
                    matrice[0][1] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_THREE) and matrice[0][2] == " ":
                    indice = 2
                    goto(75,-74)
                    croix()
                    matrice[0][2] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_FOUR) and matrice[1][0] == " ":
                    indice = 3
                    goto(-75,0)
                    croix()
                    matrice[1][0] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_FIVE) and matrice[1][1] == " ":
                    indice = 4
                    goto(0,0)
                    croix()
                    matrice[1][1] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_SIX) and matrice[1][2] == " ":
                    indice = 5
                    goto(75,0)
                    croix()
                    matrice[1][2] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_SEVEN) and matrice[2][0] == " ":
                    indice = 6
                    goto(-75,74)
                    croix()
                    matrice[2][0] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_EIGHT) and matrice[2][1] == " ":
                    indice = 7
                    goto(0,74)
                    croix()
                    matrice[2][1] = j
                    j = 2
                    verification_croix()
                if keydown(KEY_NINE) and matrice[2][2] == " ":
                    indice = 8
                    goto(75,74)
                    croix()
                    matrice[2][2] = j
                    j = 2
                    verification_croix()

            if j == 2:
                if keydown(KEY_ONE) and matrice[0][0] == " ":
                    indice == 0
                    goto(-75,-74)
                    cercle()
                    matrice[0][0] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_TWO) and matrice[0][1] == " ":
                    indice == 1
                    goto(0,-74)
                    cercle()
                    matrice[0][1] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_THREE) and matrice[0][2] == " ":
                    indice == 2
                    goto(75,-74)
                    cercle()
                    matrice[0][2] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_FOUR) and matrice[1][0] == " ":
                    indice == 3
                    goto(-75,0)
                    cercle()
                    matrice[1][0] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_FIVE) and matrice[1][1] == " ":
                    indice == 4
                    goto(0,0)
                    cercle()
                    matrice[1][1] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_SIX) and matrice[1][2] == " ":
                    indice == 5
                    goto(75,0)
                    cercle()
                    matrice[1][2] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_SEVEN) and matrice[2][0] == " ":
                    indice == 6
                    goto(-75,74)
                    cercle()
                    matrice[2][0] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_EIGHT) and matrice[2][1] == " ":
                    indice == 7
                    goto(0,74)
                    cercle()
                    matrice[2][1] = j
                    j = 1
                    verification_cercle()
                if keydown(KEY_NINE) and matrice[2][2] == " ":
                    indice == 8
                    goto(75,74)
                    cercle()
                    matrice[2][2] = j
                    j = 1
                    verification_cercle()