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
|
import tkinter as tk
from random import *
def init_game():
global code
code=[]
while len(code)<4:
nb=randint(1,8)
if nb not in code:
code.append(nb)
def check_play(code_entre):
global black
global white
black=0
white=0
for el in code_entre :
if el in code:
if code.index(el) == code_entre.index(el):
black+=1
else:
white+=1
def start():
tentatives = 0
init_game()
print(code)
while tentatives <12 :
entree = input("jouez 4 couleurs parmi [1,8] séparéés d'un espace : ")
possible=["1","2","3","4","5","6","7","8"," "]
code_entre=[]
for ele in entree:
if ele not in possible or entree.count(ele) > 1 :
entree= input(" entrer des valeurs valides svp ")
elif ele != " ":
ele=int(ele)
code_entre.append(ele)
check_play(code_entre)
if black == 4:
print("gagné ! tu es très intelligent ! tu as utilisé ", tentatives, "essais")
break
print("=> black " ,black , "white ", white)
tentatives+=1
print("tu as utilisé ", tentatives ," essais sur 12")
tkcolors=["white", "black", "red", "blue", "green", "yellow", "purple",
"orange"]
def choix(event,couleur):
canvas.itemconfigure(carre,fill=couleur)
def send():
print("send")
def reset_row():
print("reset")
app=tk.Tk()
app.title("Mastermind")
new_game=tk.Button(app,text="new game",command=init_game,width=10)
new_game.grid(row=0,column=0,columnspan=8,sticky=tk.W)
canvas=tk.Canvas(app,width=300,height=450,bg="white")
canvas.grid(row=1 ,column=0,columnspan=8)
send=tk.Button(app, text="send",command=send,width=10)
send.grid(row=40 , column=0,columnspan=4 ,sticky=tk.E)
reset = tk.Button(app,text="reset", command=reset_row,width=10)
reset.grid(row=40,column=4 , columnspan=4,sticky=tk.E)
for i, color in enumerate(tkcolors):
b = tk.Button(app, text='', width=3, bg=color, activebackground=color)
b.grid(row=20, column=i)
def click_bt(event,couleur = color):
return choix(event,couleur)
b.bind("<Button-1>",click_bt)
##creation grille carré:
x0=10
y0=10
x1=40
y1=40
for a in range(11):
for c in range(4):
carre=canvas.create_rectangle(x0,y0,x1,y1,fill="grey")
x0+=40
x1+=40
y0+=40
y1+=40
x0=10
x1=40
## creation cercles:
i=170
j=10
k=180
l=20
for a in range(10):
for c in range(4):
cercle = canvas.create_oval(i,j,k,l)
i+=15
k+=15
j+=40
l+=40
k=180
i=170
ligne= canvas.create_line(0,405,300,405)
init_game()
app.mainloop() |
Partager