Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import *
- def on_click(event):
- global gameOver
- global tmp
- global count
- global maxcount
- t = event.widget
- i = int(t.grid_info()['row'])
- j = int(t.grid_info()['column'])
- if gameOver == False:
- if bombfield[i][j] == 1:
- t.config(text = "*", bg = 'red')
- tmp.config(text = 'Вы взорвались!')
- gameOver = True
- else:
- t.config(text = countfield[i][j], bg = 'orange')
- count += 1
- tmp.config(text = 'Безопасных клеток: '+ str(maxcount - count))
- if count == maxcount:
- tmp.config(text = 'Вы открыли все безопасные клетки, победа!')
- def create_bombfield(A, n):
- global maxcount
- for i in range(n):
- row = []
- for j in range(n):
- x = randint(0,10)
- if x < 4:
- row.append(1)
- maxcount += 1
- else:
- row.append(0)
- A.append(row)
- def create_countfield(A, n):
- for i in range(n):
- row = []
- for j in range(n):
- tmp = 0
- if i > 0:
- tmp += bombfield[i - 1][j]
- if i < n - 1:
- tmp += bombfield[i + 1][j]
- if j > 0:
- tmp += bombfield[i][j - 1]
- if j < n - 1:
- tmp += bombfield[i][j + 1]
- if i > 0 and j > 0:
- tmp += bombfield[i - 1][j - 1]
- if i > 0 and j < n - 1:
- tmp += bombfield[i - 1][j + 1]
- if i < n - 1 and j > 0:
- tmp += bombfield[i + 1][j - 1]
- if i < n - 1 and j < n - 1:
- tmp += bombfield[i + 1][j + 1]
- row.append(tmp)
- A.append(row)
- def create_field(n):
- global tmp
- for i in range(n):
- for j in range(n):
- if (i + j) % 2 == 0:
- square = Label(text = '', bg = 'skyblue', width = 2, font = 'Verdana 20')
- else:
- square = Label(text = '', bg = 'blue', width = 2, font = 'Verdana 20')
- square.bind('<Button-1>', on_click)
- square.grid(row = i, column = j)
- tmp = Label(text = 'Безопасных клеток: ' + str(maxcount), bg = 'yellow', fg = 'green', font = 'Verdana 18')
- tmp.grid(row = n, columnspan = n, sticky = W + E)
- root = Tk()
- n = 10
- count = 0
- maxcount = 0
- gameOver = False
- bombfield = []
- countfield = []
- squares = []
- create_bombfield(bombfield, n)
- create_countfield(countfield, n)
- create_field(n)
- for i in range(n):
- print(bombfield[i])
- root.mainloop()
Add Comment
Please, Sign In to add comment