Table des matières:

Jeu Python Tic Tac Toe : 4 étapes
Jeu Python Tic Tac Toe : 4 étapes

Vidéo: Jeu Python Tic Tac Toe : 4 étapes

Vidéo: Jeu Python Tic Tac Toe : 4 étapes
Vidéo: [Pygame] Créer un jeu Tic Tac Toe #6 Recommencer le Jeu 2024, Juillet
Anonim
Jeu Python Tic Tac Toe
Jeu Python Tic Tac Toe

jeu de tic tac toe en python

ce jeu est fait en python c'est un langage informatique

j'ai utilisé un éditeur python appelé: pycharm vous pouvez également utiliser un éditeur de code python normal également

Étape 1: Importer

Importer
Importer

à partir de l'importation tkinter *

Étape 2: Racine

Racine
Racine

racine = Tk()

root.title(" 3T TIC TAC TOE") # Titre

text = Entry(root, font=("ds-digital", 15)) text.pack(fill=X, padx=5, pady=5, ipadx=5, ipady=5) # Texte

tableau = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Planche

Étape 3: Fonction après avoir mis dans l'éditeur, il affiche les avertissements Cliquez sur les avertissements et donnez tout à Assambel

# Les fonctions

player = bouton "X" = stop_game = False stop_game_tie = False

def Turn(): # Impression Turn text.delete(0, END) text.insert(0, "{}'S TURN".format(player))

def Exit(): root.destroy() exit()

def Game_Over(): # Vérifier si le jeu est terminé Check_Winner()

if stop_game == True: Hplayer() text.delete(0, END) text.insert(0, "'{}' WINNER".format(player))

sinon: Check_Tie()

if stop_game_tie == True: text.delete(0, END) text.insert(0, "TIE MATCH")

def Check_Winner(): # Vérifie s'il y a un stop_game global gagnant

rangée_1 = planche[0] == planche[1] == planche[2] != "-" rangée_2 = planche[3] == planche[4] == planche[5] != "-" rangée_3 = planche[6] == board[7] == board[8] != "-" si row_1 ou row_2 ou row_3: stop_game = True

colonne_1 = tableau[0] == tableau[3] == tableau[6] != "-" colonne_2 = tableau[1] == tableau[4] == tableau[7] != "-" colonne_3 = tableau[2] == board[5] == board[8] != "-" si column_1 ou column_2 ou column_3: stop_game = True

diagonal_1 = planche[0] == planche[4] == planche[8] != "-" diagonal_2 = planche[2] == planche[4] == planche[6] != "-" si diagonal_1 ou diagonal_2: stop_game = Vrai

def Check_Tie(): # Vérifie s'il s'agit d'un match nul global stop_game_tie

si "-" n'est pas dans le tableau: stop_game_tie = True

def Hplayer(): # Gestion du joueur global Turn

si joueur == "X": joueur = "O"

sinon: joueur = "X"

def Add_Text(pos, play): # Bouton global Ajouter du texte

si pos pas dans le bouton et stop_game == False et stop_game_tie == False et pos != 9: Turn()

def_buttons[pos].configure(text=play) board[pos] = player

button.append(pos) Hplayer() Turn() Game_Over()

Étape 4: le code final donnera-t-il un look final ?

Le code final donnera-t-il un look final ?
Le code final donnera-t-il un look final ?
Le code final donnera-t-il un look final ?
Le code final donnera-t-il un look final ?

# LA GRILLE

def New_Match (): # Fonction du nouveau bouton de match tableau global joueur global bouton global stop_game global stop_game_tie

button_1.configure(text="") button_2.configure(text="") button_3.configure(text="") button_4.configure(text="") button_5.configure(text="") button_6.configure(text ="") button_7.configure(text="") button_8.configure(text="") button_9.configure(text="") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = bouton "X" = stop_game = False stop_game_tie = False Turn()

# Boutons

Tourner()

frame = Frame(root) frame.pack(side=TOP, anchor=NW)

frame1 = Frame(frame) frame1.pack()

button_1 = Button(frame1, text="", command=lambda: Add_Text(0, player), bg = 'pink') button_1.pack(side=LEFT)

button_2 = Button(frame1, text="", command=lambda: Add_Text(1, player), bg = 'pink') button_2.pack(side=LEFT)

button_3 = Button(frame1, text="", command=lambda: Add_Text(2, player), bg = 'pink') button_3.pack(side=LEFT)

frame2 = Frame(frame) frame2.pack()

button_4 = Button(frame2, text="", command=lambda: Add_Text(3, player), bg = 'blue') button_4.pack(side=LEFT)

button_5 = Button(frame2, text="", command=lambda: Add_Text(4, player), bg = 'blue') button_5.pack(side=LEFT)

button_6 = Button(frame2, text="", command=lambda: Add_Text(5, player), bg = 'blue') button_6.pack(side=LEFT)

frame3 = Frame(frame) frame3.pack()

button_7 = Button(frame3, text="", command=lambda: Add_Text(6, player), bg = 'orange') button_7.pack(side=LEFT)

button_8 = Button(frame3, text="", command=lambda: Add_Text(7, player), bg = 'orange') button_8.pack(side=LEFT)

button_9 = Button(frame3, text="", command=lambda: Add_Text(8, player), bg = 'orange') button_9.pack(side=LEFT)

frame4 = Frame(frame) frame4.pack()

button_clear = Button(frame4, text="NEW MATCH", command=lambda: New_Match(), bg = 'violet', foreground = 'red') button_clear.pack(side=LEFT)

exit_button = Button(frame4, text="EXIT", command=lambda: Exit(), bg = 'green', foreground = 'orange') exit_button.pack(side=LEFT)

def_buttons = [bouton_1, bouton_2, bouton_3, bouton_4, bouton_5, bouton_6, bouton_7, bouton_8, bouton_9]

root.mainloop()

Conseillé: