dgfhdfgh
dfhdfh
Рис. 2. Изображения шариков в png формате.
Рис. Прозрачное изображение blank.png размером 50х50 px.
from tkinter import * # графическая библиотека
from random import shuffle # перемешать список suffle(A)
column = 5 # столбцы
row = 5 # строки
btn = [] # список кнопок
img = []
files = ['red.png', 'yellow.png', 'gold.png',
'green.png', 'emerald.png', 'cyan.png',
'blue.png', 'pink.png','azure.png',
'bronze.png', 'purple.png', 'scarlet.png',
'steel.png', 'silver.png']
def play(n): # функция обработчик нажатия на кнопку
shuffle(img)
btn[n].config(image = img[0])
tk = Tk()
imgBL = PhotoImage(file="blank.png")
for i in files:
img += [PhotoImage(file=i)]
for i in range(row):
f = Frame()
f.pack(expand=YES, fill=BOTH)
for j in range(column):
btn += [Button(f, image=imgBL)]
btn[-1].pack(expand=YES, fill=BOTH, side=LEFT)
btn[-1].config(command=lambda n=i*column+j:play(n))
mainloop()
Лист. 1.
from tkinter import * # графическая библиотека
from random import shuffle # перемешать список suffle(A)
column = 9 # столбцы
row = 9 # строки
post = 3 # количество новых шариков
btn = [] # список кнопок
img = [] # список объектов PhotoImage
fromTo = False # откуда или куда
m = 0 # куда
blank = [i for i in range(column*row)] # список пустых кнопок
files = ['gold.png', 'emerald.png', 'pink.png','azure.png',
'bronze.png', 'scarlet.png', 'steel.png']
def play(n): # функция обработчик нажатия на кнопку
global m, fromTo, blank
if not fromTo:
if n in blank: return
m = n
btn[m].config(state=DISABLED)
else:
if n not in blank: return
btn[m].config(state=NORMAL)
btn[n].config(image = btn[m].cget('image'))
btn[m].config(image=imgBL)
blank.remove(n)
blank += [m]
if not win(n): place()
fromTo = not fromTo
def win(n):
global blank
line = []
for i in range(n//column*column, (n//column+1)*column):
if btn[i].cget('image') == btn[n].cget('image'):
line += [1]
else:
line += [0]
if '1, 1, 1, 1, 1' in str(line).strip('[]') or '1, 1, 1, 1, 1, 1' in str(
line).strip('[]') or '1, 1, 1, 1, 1, 1' in str(line).strip('[]'):
s = [i for i in range(n//column*column, (n//column+1)*column)]
for i in range (1,column):
if n+i in s and btn[n+i].cget('image') == btn[n].cget('image'):
btn[n+i].config(image=imgBL)
blank += [n+i]
else: break
for i in range (1,column):
if n-i in s and btn[n-i].cget('image') == btn[n].cget('image'):
btn[n-i].config(image=imgBL)
blank += [n-i]
else: break
btn[n].config(image=imgBL)
blank += [n]
return True
return False
def place(n=post):
shuffle(img)
shuffle(blank)
for i in range(min(n, len(blank)-1)):
btn[blank[i]].config(image = img[i])
blank.remove(blank[i])
tk = Tk()
imgBL = PhotoImage(file="blank.png")
for i in files:
img += [PhotoImage(file=i)]
for i in range(row):
f = Frame()
f.pack(expand=YES, fill=BOTH)
for j in range(column):
btn += [Button(f, image=imgBL)]
btn[-1].pack(expand=YES, fill=BOTH, side=LEFT)
btn[-1].config(command=lambda n=i*column+j:play(n))
place()
mainloop()
Лист. 2.
from tkinter import * # графическая библиотека
from random import shuffle # перемешать список suffle(A)
column = 9 # столбцы
row = 9 # строки
post = 3 # количество новых шариков
btn = [] # список кнопок
img = [] # список объектов PhotoImage
fromTo = False # откуда или куда
m = 0 # куда
blank = [i for i in range(column*row)] # список пустых кнопок
files = ['gold.png', 'emerald.png', 'pink.png','azure.png',
'bronze.png', 'scarlet.png', 'steel.png']
def play(n): # функция обработчик нажатия на кнопку
global m, fromTo, blank
if not fromTo:
if n in blank: return
m = n
btn[m].config(state=DISABLED)
else:
if n not in blank: return
btn[m].config(state=NORMAL)
btn[n].config(image = btn[m].cget('image'))
btn[m].config(image=imgBL)
blank.remove(n)
blank += [m]
if not win(n): place()
fromTo = not fromTo
def win(n):
global blank
line = []
r = False
for i in range(n//column*column, (n//column+1)*column):
if btn[i].cget('image') == btn[n].cget('image'):
line += [1]
else:
line += [0]
if '1, 1, 1, 1, 1' in str(line).strip('[]'):
s = [i for i in range(n//column*column, (n//column+1)*column)]
for i in range (1,column):
if n+i in s and btn[n+i].cget('image') == btn[n].cget('image'):
btn[n+i].config(image=imgBL)
blank += [n+i]
else: break
for i in range (1,column):
if n-i in s and btn[n-i].cget('image') == btn[n].cget('image'):
btn[n-i].config(image=imgBL)
blank += [n-i]
else: break
r = True
line = []
for i in range(n%column, column*row, row):
if btn[i].cget('image') == btn[n].cget('image'):
line += [1]
else:
line += [0]
if '1, 1, 1, 1, 1' in str(line).strip('[]'):
for i in range(n+row, row*column, row):
if btn[i].cget('image') == btn[n].cget('image'):
btn[i].config(image=imgBL)
blank += [i]
else: break
for i in range(n-row, -1, -row):
if btn[i].cget('image') == btn[n].cget('image'):
btn[i].config(image=imgBL)
blank += [i]
else: break
r = True
if r:
btn[n].config(image=imgBL)
blank += [n]
return r
def place(n=post):
shuffle(img)
shuffle(blank)
for i in range(min(n, len(blank))):
btn[blank[0]].config(image = img[i])
blank.remove(blank[0])
tk = Tk()
imgBL = PhotoImage(file="blank.png")
for i in files:
img += [PhotoImage(file=i)]
for i in range(row):
f = Frame()
f.pack(expand=YES, fill=BOTH)
for j in range(column):
btn += [Button(f, image=imgBL)]
btn[-1].pack(expand=YES, fill=BOTH, side=LEFT)
btn[-1].config(command=lambda n=i*column+j:play(n))
place()
mainloop()
Лист. 3.
Рис. 16