Guest User

Untitled

a guest
Feb 17th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. from random import randint
  4.  
  5. class setupwindow():
  6. def __init__(window): #window is the master object of the setup window
  7. window.root = Tk()
  8. window.root.title("Setup")
  9. window.root.grid()
  10.  
  11. window.finish = "N"
  12.  
  13. labels = ["Height:", "Width:", "Mines:"]
  14. window.label = ["","",""]
  15. window.entry = ["","",""]
  16.  
  17. for i in range(3):
  18. window.label[i] = Label(text = labels[i])
  19. window.label[i].grid(row = i, column = 1)
  20. window.entry[i] = Entry()
  21. window.entry[i].grid(row = i, column = 2)
  22.  
  23. window.startbutton = Button(text = "Start", command = lambda: setupwindow.onclick(window))
  24. window.startbutton.grid(column = 2)
  25. window.root.mainloop()
  26.  
  27. def onclick(window):
  28. setupwindow.verification(window)
  29. if window.verf == "Y":
  30. window.finish = "Y"
  31. window.root.destroy()
  32. return window
  33.  
  34. def verification(window):
  35. height = window.entry[0].get()
  36. width = window.entry[1].get()
  37. mines = window.entry[2].get()
  38.  
  39. window.verf = "N"
  40. if height.isdigit() and width.isdigit() and mines.isdigit():
  41. height = int(height)
  42. width = int(width)
  43. mines = int(mines)
  44.  
  45. if height > 0 and height <= 24:
  46. totalsquares = height * width
  47.  
  48. if width > 0 and width <= 48:
  49.  
  50. if mines > 0:
  51. if mines < totalsquares:
  52. window.verf = "Y"
  53. window.height = height
  54. window.width = width
  55. window.mines = mines
  56.  
  57. else:
  58. messagebox.showerror("Invalid", "You cannot have more mines than squares!")
  59. else:
  60. messagebox.showerror("Invalid", "You can't play minesweeper without mines!")
  61. else:
  62. messagebox.showerror("Invalid", "Width must be between 1 and 48 inclusive")
  63. else:
  64. messagebox.showerror("Invalid", "Height must be between 1 and 24 inclusive")
  65. else:
  66. messagebox.showerror("Invalid", "All values must be integers")
  67.  
  68.  
  69. class gamewindow():
  70. def __init__(s, setup): #s is the master object of the main game
  71. s.height = setup.height
  72. s.width = setup.width
  73. s.mines = setup.mines
  74.  
  75. s.root = Tk()
  76. s.root.title("Minesweeper")
  77. s.root.grid()
  78.  
  79. s.finish = "N"
  80. s.maingrid = list()
  81. for i in range(s.height):
  82. s.maingrid.append([])
  83. for x in range(s.width):
  84. s.maingrid[i].append(" ")
  85. s.maingrid[i][x] = Button(height = 0, width = 3, font = "Calibri 15 bold", text = "", bg = "gray90", command = lambda i=i, x=x: gamewindow.onclick(s, i, x))
  86.  
  87. s.maingrid[i][x].bind("<Button-3>", lambda event="<Button-3>", i=i, x=x: gamewindow.rightclick(event, s, i, x))
  88. s.maingrid[i][x].grid(row = i, column = x)
  89. s.maingrid[i][x].mine = "False"
  90.  
  91. totalsquares = s.height * s.width
  92. s.scoreneeded = totalsquares - s.mines
  93. s.score = 0
  94.  
  95. indexlist = list()
  96. for i in range(totalsquares):
  97. indexlist.append(i)
  98.  
  99. spaceschosen = list() #where the mines are going to be
  100. for i in range(s.mines):
  101. chosenspace = randint(0, len(indexlist) - 1)
  102. spaceschosen.append(indexlist[chosenspace])
  103. del indexlist[chosenspace]
  104.  
  105. for i in range(len(spaceschosen)):
  106. xvalue = int(spaceschosen[i] % s.width)
  107. ivalue = int(spaceschosen[i] / s.width)
  108.  
  109. s.maingrid[ivalue][xvalue].mine = "True"
  110.  
  111. s.root.mainloop()
  112.  
  113.  
  114. def onclick(s, i, x):
  115. colourlist = ["PlaceHolder", "Blue", "Green", "Red", "Purple", "Black", "Maroon", "Gray", "Turquoise"]
  116.  
  117. if s.maingrid[i][x]["text"] != "F" and s.maingrid[i][x]["relief"] != "sunken":
  118. if s.maingrid[i][x].mine == "False":
  119. s.score += 1
  120.  
  121. combinationsi = [1, -1, 0, 0, 1, 1, -1, -1]
  122. combinationsx = [0, 0, 1, -1, 1, -1, 1, -1] #All the surrounding spaces
  123.  
  124. minecount = 0
  125. for combinations in range(len(combinationsi)):
  126. tempi = i + combinationsi[combinations]
  127. tempx = x + combinationsx[combinations]
  128.  
  129. if tempi < s.height and tempx < s.width and tempi >= 0 and tempx >= 0:
  130. if s.maingrid[tempi][tempx].mine == "True":
  131. minecount = minecount + 1
  132.  
  133. if minecount == 0:
  134. minecount = ""
  135.  
  136. s.maingrid[i][x].configure(text = minecount, relief = "sunken", bg = "gray85")
  137.  
  138. if str(minecount).isdigit():
  139. s.maingrid[i][x].configure(fg = colourlist[minecount])
  140.  
  141. if minecount == "":
  142. for z in range(len(combinationsi)):
  143. if s.finish == "N":
  144. ivalue = i + int(combinationsi[z])
  145. xvalue = x + int(combinationsx[z])
  146.  
  147. if ivalue >= 0 and ivalue < s.height and xvalue >=0 and xvalue < s.width:
  148. if s.maingrid[ivalue][xvalue]["relief"] != "sunken":
  149. gamewindow.onclick(s, ivalue, xvalue)
  150.  
  151. if s.score == s.scoreneeded and s.finish == "N":
  152. messagebox.showinfo("Congratulations", "A winner is you!")
  153. s.finish = "Y"
  154. s.root.destroy()
  155.  
  156.  
  157. else:
  158. s.maingrid[i][x].configure(bg = "Red", text = "*")
  159. for a in range(len(s.maingrid)):
  160. for b in range(len(s.maingrid[a])):
  161. if s.maingrid[a][b].mine == "True":
  162. if s.maingrid[a][b]["text"] == "F":
  163. s.maingrid[a][b].configure(bg = "Green")
  164. elif s.maingrid[a][b]["bg"] != "Red":
  165. s.maingrid[a][b].configure(bg = "Pink", text = "*")
  166.  
  167. elif s.maingrid[a][b]["text"] == "F":
  168. s.maingrid[a][b].configure(bg = "Yellow")
  169.  
  170. messagebox.showinfo("GAME OVER", "You have lost")
  171. s.root.destroy()
  172.  
  173. def rightclick(event, s, i, x):
  174. if s.maingrid[i][x]["relief"] != "sunken":
  175. if s.maingrid[i][x]["text"] == "":
  176. s.maingrid[i][x].config(text = "F")
  177. elif s.maingrid[i][x]["text"] == "F":
  178. s.maingrid[i][x].config(text = "?")
  179. else:
  180. s.maingrid[i][x].config(text = "")
  181.  
  182.  
  183. if __name__ == "__main__":
  184. setup = setupwindow()
  185. if setup.finish == "Y":
  186. game = gamewindow(setup)
  187. quit()
Add Comment
Please, Sign In to add comment