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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| # -*- coding: cp1252 -*-
# ==============================================================================
# puissance4 :
# ==============================================================================
""" """
__author__ = ""
__version__ = ""
__date__ = ""
# ------------------------------------------------------------------------------
from Tkinter import *
class Centremoi():
def __init__(self, monwidget):
monwidget.update_idletasks()
monwidget.geometry("%dx%d+%d+%d" % (monwidget.winfo_reqwidth(),monwidget.winfo_reqheight(), (monwidget.winfo_screenwidth()-monwidget.winfo_reqwidth())/2, (monwidget.winfo_screenheight()-monwidget.winfo_reqheight())/2 ) )
monwidget.resizable(width=False, height=False)
class TextGrid(Frame):
config = {'border':1, 'widget':Label, 'text':'' , 'size':(2,2), 'command':''}
def __init__(self, master, **keys):
for key in self.config:
if key not in keys: keys[key] = self.config[key]
self.grid, self.size, self.init = {}, keys['size'], keys['text']
widget, command, (cols,rows) = keys['widget'], keys['command'], keys['size']
del keys['widget'], keys['command'], keys['size']
Frame.__init__(self, master, border=keys['border'], relief=SOLID)
for col in range(cols):
for row in range(rows):
if command:
keys['command'] = lambda cell=(col,row): command(cell)
self.grid[col,row] = widget(self, keys)
self.grid[col,row].grid(column=col, row=row, sticky=N+S+W+E)
for n in range(cols): self.columnconfigure(n, weight=1)
for n in range(rows): self.rowconfigure(n, weight=1)
def __getitem__(self, key):
if isinstance(key, str):
return Frame.__getitem__(self, key)
col, row = key[:2]; prop = key[2] if len(key) == 3 else 'text'
return self.grid[col,row][prop]
def __setitem__(self, key, val):
if isinstance(key, str):
Frame.__setitem__(self, key, val); return
col, row = key[:2]; prop = key[2] if len(key) == 3 else 'text'
self.grid[col,row][prop] = val
class Initiation(Tk):
def __init__(self,parent):
Tk.__init__(self, parent)
self.parent = parent
self.size, self.fnt = (7,7), "Arial 14"
self.colors = ('RED','RED','RED','RED','RED','RED','RED')
self.color1 = 'RED'
self.color2 = 'BLUE'
self.joueur1 = 0
self.joueur2 = 0
self.gridB = TextGrid(self, widget=Button, size=(7,1), font=self.fnt, width=9,
command=Calcul().paint)
for p, color in enumerate(self.colors):
self.gridB[p,0] = 'RED'
self.gridB[p,0,'bg'] = 'GREY'
self.gridB.pack(side=TOP, fill=BOTH, padx=5, pady=5)
self.gridA = TextGrid(self, size=self.size, font=self.fnt, width=4, height=2, relief=SOLID)
for p in range(7):
for k in range(7):
self.gridA[p,k,'bg'] = 'GREY'
self.gridA.pack(side=TOP, fill=BOTH, expand=YES, padx=5, pady=5)
class Calcul():
def paint(self, button):
temp,self.tem=[],[]
val = button[0]
if root.gridB[val,0] == 'Stop':
temp = [7]
for v in range(7):
if root.gridB[v,0] != 'Stop':
self.tem.append(root.gridB[v,0])
self.color = self.tem[0]
if len(temp) == 0:
if self.tem[0] == root.color1:
self.joueur=root.joueur1
else:
self.joueur=root.joueur2
self.remplir(button)
self.gagner(button)
def remplir(self, button):
val = button[0]
k = 6
while k:
if root.gridA[val,k,'bg'] == 'GREY':
root.gridA[val,k,'bg'] = self.color
break
else:
k -= 1
if k == 0:
root.gridA[val,k,'bg'] = self.color
root.gridB[val,0,'bg'] = 'YELLOW'
root.gridB[val,0] = 'Stop'
def change(self):
if self.tem[0] == root.color1:
self.color='BLUE'
else:
self.color='RED'
cols, rows = root.size
for p in range(rows):
if root.gridB[p,0] != 'Stop':
root.gridB[p,0] = self.color
def gagner(self, button): #
val = button[0]
print val
print button
for val in range(7):
for k in range(4):
if (root.gridA[val,k,'bg']== self.color) and (root.gridA[val,k+1,'bg']== self.color) and (root.gridA[val,k+2,'bg']== self.color) and (root.gridA[val,k+3,'bg']== self.color):
self.joueur += 1
break
for val in range (7):
for k in range(4):
if (root.gridA[k,val,'bg']== self.color) and (root.gridA[k+1,val,'bg']== self.color) and (root.gridA[k+2,val,'bg']== self.color) and (root.gridA[k+3,val,'bg']== self.color):
self.joueur += 1
break
for val in range(4):
for k in range(4):
if (root.gridA[val,k,'bg']==self.color) and (root.gridA[val+1,k+1,'bg']==self.color) and (root.gridA[val+2,k+2,'bg']==self.color) and (root.gridA[val+3,k+3,'bg']==self.color):
self.joueur += 1
break
for val in range(4):
for k in range(6,2,-1):
if (root.gridA[val,k,'bg']==self.color) and (root.gridA[val+1,k-1,'bg']==self.color) and (root.gridA[val+2,k-2,'bg']==self.color) and (root.gridA[val+3,k-3,'bg']==self.color):
self.joueur += 1
break
if self.joueur == 3:
self.fenetre()
else: self.change()
def fenetre(self):
scor=Toplevel()
#root.withdraw()
scor.withdraw()
scor.title('scores')
if self.color == 'RED': gagnants = 'rouges'
else: gagnants = 'bleus'
Label(scor, text= 'Les '+gagnants+' ont gagné', font='Arial 50', width=25, border=2, relief=SOLID).pack(side=TOP, fill=BOTH, pady=5, padx=5)
scor.protocol('WM_DELETE_WINDOW', scor.quit) # Cela ne sert a rien...
scor.minsize(scor.winfo_width(), scor.winfo_height())
Centremoi(scor)
scor.deiconify()
# ==============================================================================
if __name__ == "__main__": # testcode for class 'TextGrid'
# ----------------------------------------------------------------------------
root=Initiation(None)
root.withdraw()
root.title("TextGrid")
root.minsize(root.winfo_width(), root.winfo_height())
root.resizable(1,1)
root.protocol("WM_DELETE_WINDOW", root.quit) # Cela ne sert a rien...
Centremoi(root)
root.deiconify()
root.mainloop() |
Partager