Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3. root1 = Tk()
  4. root1.title("New Game")
  5. root1.geometry("250x250")
  6.  
  7. botton = Button(root1, width=12, height=3, text='New Game', font="Arial 10")
  8. botton.place(x=80, y=20)
  9.  
  10. Label(root1, text='Уровень сложности:', width=20, height=3, font="Arial 12").place(x=0, y=80)
  11.  
  12. hard = DoubleVar()   # Заводится переменная
  13. hard.set(0.2)  # Этот метод устанавлявает var в 0.2 (активна вторая кнопка)
  14. rad0 = Radiobutton(root1, text="Легко", variable=hard, value=0.15).place(x=0, y=130)
  15. rad1 = Radiobutton(root1, text="Средне", variable=hard, value=0.2).place(x=0, y=170)
  16. rad2 = Radiobutton(root1, text="Сложно", variable=hard, value=0.25).place(x=0, y=210)
  17.  
  18.  
  19. def new_game(event):
  20.     root1.destroy()
  21.     root = Tk()
  22.     root.title("New Game")
  23.     my_list =[]
  24.     all_buttons = []
  25.     v = hard.get()
  26.     polerow = 9
  27.     polecolumn = 9
  28.     matrix = []
  29.     pole = []
  30.     game_pole = []
  31.  
  32.     my_list = [[0 for i in range(polerow)] for j in range(polecolumn)]
  33.     all_buttons = []
  34.     pole = [[0 for i in range(polerow)] for j in range(polecolumn)]
  35.     matrix = [['*' if random.random() < 0.15 else '' for i in range(9)] for j in range(9)]
  36.  
  37.     def creaMatriz():
  38.         for y, row in enumerate(my_list):
  39.             buttons_row = []  # пустой список
  40.             for x, element in enumerate(row):
  41.                 boton2 = Button(root, text="", width=6, height=3, command=lambda a=x, b=y: onButtonPressed(a, b))
  42.                 boton2.grid(row=y, column=x)  # Упаковщик
  43.                 buttons_row.append(boton2)
  44.             all_buttons.append(buttons_row)
  45.  
  46.     def game_polle():
  47.         for a in range(polerow):  # a=0,1,2,3,4,5
  48.             for b in range(polecolumn):  # b=0,1,2,3,4,5
  49.                 if matrix[a][b] == "*":  # Если какой-то элемент звездочка
  50.                     for c in range((a-1), (a+2)):  # для каждого элемента вокруг -1:6
  51.                         for d in range((b-1), (b+2)):  # т.е. матрица 3*3
  52.                             if c >= 0 and d >=0 and c <= (polerow-1) and d <= (polecolumn-1) and matrix[c][d] != '*':  # важно поставить условия в нужном порядке
  53.                                 pole[c][d] = pole[c][d] + 1
  54.                             elif c >= 0 and d >= 0 and c <= (polerow-1) and d <= (polecolumn-1):
  55.                                 pole[c][d] = '*'
  56.         return pole
  57.  
  58.     qwer = game_polle()
  59.  
  60.     def onButtonPressed(x, y):
  61.         all_buttons[y][x]['text'] = str(qwer[x][y])
  62.  
  63.     creaMatriz()
  64.     root.mainloop()
  65.  
  66. botton.bind("<Button-1>", new_game)
  67. root1.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement