Advertisement
here2share

# Tk_MineSweeper.py ZZZ

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