Пишем игру Сапёр (Mines) на Python с использованием библиотеки Tkinter. Следует отметить, что в Windows 3.11 игра winmine уже была. Это задолго до появления Windows 95.
dfhdfh
# This is my version of the game, known as mines or Minesweeper.
#
# Created on October 16, 2023.
# Author: Diorditsa A.
#
# mines.py is distributed in the hope that it will be useful, but
# WITHOUT WARRANTY OF ANY KIND; not even an implied warranty
# MARKETABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See. See the GNU General Public License for more information.
# You can get a copy of the GNU General Public License
# by link http://www.gnu.org/licenses/
from tkinter import * # графическая библиотека
from random import shuffle # перемешать список suffle(A)
column = row = 15 # столбцы и строки
btn = [] # список кнопок
playground = list('ЧТОЕСТЖУ ') # виртуальное игровое поле
mines = column * row * 10 // 64 # количесво мин
playground = [1] * mines + [0] * (column * row - mines)
shuffle(playground)
def roomie(n): # номера клеток соседей
return [i+j for i in (n-column, n, n+column) for j in (-1, 0, 1) if i+j in range(row*column) and (i+j)//column == i//column and (i+j) != n]
def clear(n): # очистить клетки на которых и вокруг которых нет мин
if playground[n] == 0 and btn[n].count == 0 and btn[n].cget('bg') != '#dfd':
btn[n].config(text=' ',bg="#dfd")
for i in roomie(n):
clear(i)
def play(n): # функция обработчик нажатия на кнопку
btn[n].config(text='M' if playground[n] else btn[n].count)
btn[n].config(bg="#fdd" if btn[n].cget('text') == 'M' else "#ccc")
clear(n)
def mark(n): # снять/поставить флажок
if btn[n].cget('bg') == '#ddd':
btn[n].config(text='F', bg='#ffa')
elif btn[n].cget('text') == 'F':
btn[n].config(text=' ', bg='#ddd')
for i in range(row):
f = Frame() # Фрейм
f.pack(expand=YES, fill=BOTH) # Вывод фрейма на экран
for j in range(column):
n = i * column + j
btn += [Button(f)]
btn[n].pack(expand=YES, fill=BOTH, side=LEFT)
btn[n].config(width=3, height=2)
btn[n].config(text=' ', bg='#ddd')
btn[n].config(command=lambda n=n: play(n))
btn[n].bind('<Button-3>', lambda event, n=n: mark(n))
btn[n].count = sum([playground[i] for i in roomie(n)])
mainloop() # главный цикл программы
Прог. 1.
Рис. 1.
# Mines
# This is my version of the game, known as mines or Minesweeper.
#
# Created on October 16, 2023.
# Author: Diorditsa A.
#
# mines.py is distributed in the hope that it will be useful, but
# WITHOUT WARRANTY OF ANY KIND; not even an implied warranty
# MARKETABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See. See the GNU General Public License for more information.
# You can get a copy of the GNU General Public License
# by link http://www.gnu.org/licenses/
from tkinter import *
from random import shuffle
class btn(Button):
number = 0
def __init__(self, container):
super().__init__(container, command=self.play)
self.config(text=' ', bg='#ddd', font=('mono', 12), width=1, height=1)
self.bind('<Button-3>', self.mark)
self.pack(side=LEFT)
self.num = btn.number
btn.number += 1
self.count = sum([playground[i] for i in roomie(self.num)])
def play(self):
self.config(text='M' if playground[self.num] else self.count if self.count else ' ')
self.config(bg="#fdd" if self.cget('text') == 'M' else "#ccc")
self.clear()
def clear(self):
if playground[self.num] == 0 and self.count == 0 and self.cget('bg') != '#dfd':
self.config(bg="#dfd")
for i in roomie(self.num):
btnground[i].clear()
def mark(self, event):
if self.cget('bg') == '#ddd':
self.config(text='F', bg='#ffa')
elif self.cget('text') == 'F':
self.config(text=' ', bg='#ddd')
def roomie(n):
return [i+j for i in (n-column, n, n+column) for j in (-1, 0, 1) if i+j in range(row*column) and (i+j)//column == i//column and (i+j) != n]
column = row = 15
mines = column*row * 10 // 64
playground = [1] * mines + [0] * (column*row - mines)
shuffle(playground)
btnground = [btn(i) for i in [Frame() for i in range(row)] if not i.pack() for j in range(column)]
mainloop()
Прог. 2.