Allena_Gorskaya

Programma 3

Mar 20th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. from tkinter import *
  2. from random import *
  3.  
  4. def on_click(event):
  5.     global gameOver
  6.     global tmp
  7.     global count
  8.     global maxcount
  9.     t = event.widget
  10.     i = int(t.grid_info()['row'])
  11.     j = int(t.grid_info()['column'])
  12.     if gameOver == False:
  13.         if bombfield[i][j] == 1:
  14.             t.config(text = "*", bg = 'red')
  15.             tmp.config(text = 'Вы взорвались!')
  16.             gameOver = True
  17.         else:
  18.             t.config(text = countfield[i][j], bg = 'orange')
  19.             count += 1
  20.             tmp.config(text = 'Безопасных клеток: '+ str(maxcount - count))
  21.             if count == maxcount:
  22.                 tmp.config(text = 'Вы открыли все безопасные клетки, победа!')
  23.  
  24.  
  25. def create_bombfield(A, n):
  26.     global maxcount
  27.     for i in range(n):
  28.         row = []
  29.         for j in range(n):
  30.             x = randint(0,10)
  31.             if x < 4:
  32.                 row.append(1)
  33.                 maxcount += 1
  34.             else:
  35.                 row.append(0)
  36.         A.append(row)
  37.  
  38. def create_countfield(A, n):
  39.     for i in range(n):
  40.         row = []
  41.         for j in range(n):
  42.             tmp = 0
  43.             if i > 0:
  44.                 tmp += bombfield[i - 1][j]
  45.             if i < n - 1:
  46.                 tmp += bombfield[i + 1][j]
  47.             if j > 0:
  48.                 tmp += bombfield[i][j - 1]                
  49.             if j < n - 1:
  50.                 tmp += bombfield[i][j + 1]
  51.             if i > 0 and j > 0:
  52.                 tmp += bombfield[i - 1][j - 1]
  53.             if i > 0 and j < n - 1:
  54.                 tmp += bombfield[i - 1][j + 1]
  55.             if i < n - 1 and j > 0:
  56.                 tmp += bombfield[i + 1][j - 1]
  57.             if i < n - 1 and j < n - 1:
  58.                 tmp += bombfield[i + 1][j + 1]
  59.             row.append(tmp)
  60.         A.append(row)
  61.  
  62.  
  63. def create_field(n):
  64.     global tmp
  65.     for i in range(n):
  66.         for j in range(n):
  67.             if (i + j) % 2 == 0:
  68.                 square = Label(text = '', bg = 'skyblue', width = 2, font = 'Verdana 20')              
  69.             else:
  70.                 square = Label(text = '', bg = 'blue', width = 2, font = 'Verdana 20')
  71.             square.bind('<Button-1>', on_click)
  72.             square.grid(row = i, column = j)        
  73.     tmp = Label(text = 'Безопасных клеток: ' + str(maxcount), bg = 'yellow', fg = 'green', font = 'Verdana 18')
  74.     tmp.grid(row = n, columnspan = n, sticky = W + E)
  75.    
  76.    
  77.  
  78. root = Tk()
  79. n = 10
  80. count = 0
  81. maxcount = 0
  82. gameOver = False
  83. bombfield = []
  84. countfield = []
  85. squares = []
  86.  
  87. create_bombfield(bombfield, n)
  88. create_countfield(countfield, n)
  89. create_field(n)
  90.  
  91. for i in range(n):
  92.     print(bombfield[i])
  93.  
  94.    
  95.  
  96.  
  97. root.mainloop()
Add Comment
Please, Sign In to add comment