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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| from random import *
from copy import *
from tkinter import *
def creegrille(n, x):
res=[0]*n
for i in range(n):
res[i]= [x]*n
return res
def taille(grille):
return len(grille)
def cherche(grille, val):
return[(i,j) for i in range (len(grille)) for j in range (len(grille)) if grille[i][j]==val]
def grille(n): # n taille de la grille p nombre d'objets
m=creegrille(n,'_')
#print(m)
cases=[(x,y) for x in range(n) for y in range(n) ]#if x!=0 or y!=0]
#print(cases)
objets= cases
for (i,j) in objets:
if (i==8 and j!=8) or j==0:
m[i][j]="b"
for (i,j) in objets:
if (i==0 and j!=0) or j==8:
m[i][j]="w"
return m
#variables globales
c=40 #taille de chaque carré
nb=9 #nombre de carrés par ligne
x0,y0=50,5 #coordonnées du point en haut à gauche de la grille
def dessineGrille(m):
can.delete(ALL)
n=len(m)
for i in range(nb+1):
can.create_line(x0,y0+c*i,x0+nb*c,y0+c*i)
can.create_line(x0+c*i,y0,x0+c*i,y0+nb*c)
for i in range(n):
for j in range(n):
x=m[i][j]
if x=="b":
col="black"
can.create_oval(x0 +c*j,y0+c*i,x0 +c*(j+1),y0+c*(i+1),fill=col)
elif x=="w":
col="white"
can.create_oval(x0 +c*j,y0+c*i,x0 +c*(j+1),y0+c*(i+1),fill=col)
def taille(grille):
return len(grille)
def position(e):
n=taille(m)
(i,j)=correspond(e.x,e.y)
if i in range(nb) and j in range(nb):
print(i,j)
return(i,j)
def position1(e):# deplacemnt de d à patir de la position i j
global m
pos=position(e)
print(m[pos[0]][pos[1]])
return m[pos[0]][pos[1]]
def position2(e):
global m
pos1=position(e)
print(m[pos1[0]][pos1[1]])
return m[pos1[0]][pos1[1]]
def deplacement(e):
global m
m=position1(e)
if pos[0] in range(nb) and pos[1] in range(nb):
if position1(e)=="b":
m[pos[0]][pos[1]]="_"
m=position2(e)
m[pos1[0]][pos1[1]]="b"
dessineGrille(m)
def supr(e):
global m
pos=position(e)
m[pos[0]][pos[1]]="_"
dessineGrille(m)
def clic(e):
global m
pos1=position(e)
m[pos1[0]][pos1[1]]="b"
dessineGrille(m)
def ecrire(e): #fonction qui décrit chacun des évènements
text1.delete("0.0",END)
text2.delete("0.0",END)
(i,j)=correspond(e.x,e.y)
if i in range(nb) and j in range(nb):
text1.insert(END,"click dans la case"+" ("+str(i)+","+str(j)+")")
text2.insert(END,"click dans la case"+" ("+str(i)+","+str(j)+")")
else:
text1.insert(END,"vous avez cliqué hors du plateau")
text2.insert(END,"vous avez cliqué hors du plateau")
def correspond(x,y): #fonctions permettant d'associer à chaque case un couple d'entier (i,j)
return (((y-y0)//c,(x-x0)//c))
def new():
global m
m=grille(9)
dessineGrille(m)
bdem.pack_forget()
#fonctions associées aux widgets
detect=False
def monquitter():
fen.quit()
fen.destroy()
def restart():
return
fen= Tk()
fen.title("Ming Mang")
fen.geometry("800x600")
can=Canvas(fen,height=500,width=500,bg="white")
can.focus_set()
can.bind("<c>",ecrire)
can.bind("<Button-3>",supr)
can.bind("<Button-1>",clic)
can.pack(side=LEFT)
#Les widgets
#boutton de depart
bdem=Button(fen,text="départ",command=new,font=("Ubuntu",15,"bold"))
bdem.pack(side=TOP)
#boutton quitter
bq=Button(fen,text="quitter",command=monquitter,font=("Ubuntu",15,"bold"))
bq.pack(side=BOTTOM)
#boutton recommencer
brec=Button(fen,text="reset",command=restart,font=("Ubuntu",15,"bold"))
brec.pack(side=TOP)
#partie Joueur1
t1=Label(fen,text="JOUEUR1",fg="red",)
t1.pack(pady=15)
text1=Text(bg="grey",height=2,width=32)
text1.pack()
#partie Joueur2
t2=Label(fen,text="JOUEUR2",fg="blue")
t2.pack(pady=10)
text2=Text(bg="grey",height=2,width=32)
text2.pack()
fen.mainloop() |
Partager