Guest User

Minesweeper

a guest
Jul 30th, 2014
2,162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from Tkinter import *
  2. import random
  3. import time
  4.  
  5. class Game:
  6.     def __init__(self, size, root):
  7.         self.size = size
  8.         self.grid = [[Tile(False) for r in range(self.size)] for c in range(self.size)]
  9.         self.root = root
  10.         for r in range(len(self.grid)):
  11.             root.rowconfigure(r, weight = 1)
  12.             root.columnconfigure(r, weight = 1)
  13.             root.grid_propagate(0)
  14.             self.bluecolors = ['#9999ff', '#6666ff', '#3333ff', '#0000ff']
  15.  
  16.         def create_board():
  17.  
  18.             def create_random_mines():
  19.                 mines = [[None for c in r]for r in self.grid]
  20.                 available_mines = []
  21.  
  22.                 #create list of available coordinates (available_mines):
  23.                 for r in range(len(self.grid)):
  24.                     for c in range(len(self.grid)):
  25.                         available_mines.append([r,c])  
  26.  
  27.                 #create list of randomly selected coordinates to hide mines in (mines):
  28.                 for i in range((len(self.grid)**2)/6):
  29.                     mine = random.choice(available_mines)
  30.                     available_mines.pop(available_mines.index(mine))
  31.                     self.grid[mine[0]][mine[1]].mine = True
  32.                     self.grid[mine[0]][mine[1]].clicked = True
  33.  
  34.             create_random_mines()  
  35.  
  36.             #create buttons in grid:   
  37.             self.b = [[None for c in r]for r in self.grid] 
  38.             for r in range(len(self.grid)):
  39.                 for c in range(len(self.grid)):
  40.                     if self.grid[r][c].mine:
  41.                         self.b[r][c] = Button(root, text = ' ', bg = 'white')
  42.                         self.b[r][c].bind('<Button-1>', lambda a = '', r = r , c = c, mine = True: on_left_click(r,c,mine))
  43.                     else:
  44.                         self.b[r][c] = Button(root, text = ' ', bg = 'white')
  45.                         self.b[r][c].bind('<Button-1>', lambda a = '', r = r , c = c, mine = False: on_left_click(r,c,mine))
  46.                     self.b[r][c].grid(row = r, column = c, sticky = 'NESW')
  47.                     self.b[r][c].bind('<Button-3>', lambda a = '', r = r , c = c: on_right_click(r,c))     
  48.  
  49.         create_board()
  50.  
  51.         def you_lose():
  52.  
  53.             def playagain():
  54.                 g = Game(self.size, gameboard)
  55.                 fuckyou.destroy()
  56.                 playagain.destroy()
  57.  
  58.             for r in range(len(self.grid)):
  59.                 for c in range(len(self.grid)):
  60.                     self.b[r][c].destroy()
  61.             fuckyou = Label(mainframe, text = 'FUCK YOU', fg = 'white', bg = '#004982')
  62.             fuckyou.pack(side = "top")
  63.  
  64.             playagain = Button(mainframe, text = 'PLAY AGAIN', command = playagain, fg = 'white', bg = '#004982')
  65.             playagain.pack()
  66.  
  67.         def you_win():
  68.  
  69.             def playagain():
  70.                 g = Game(self.size, gameboard)
  71.                 fuckyou.destroy()
  72.                 fuckyouwin.destroy()
  73.                 playagain.destroy()
  74.  
  75.             unclicked_tiles=[]
  76.             for r in range(len(self.grid)):
  77.                 for c in range(len(self.grid)):
  78.                     if self.grid[r][c].clicked == False:
  79.                         unclicked_tiles.append(self.grid[r][c])
  80.  
  81.             if len(unclicked_tiles)==0:
  82.                 for r in range(len(self.grid)):
  83.                     for c in range(len(self.grid)):
  84.                         self.b[r][c].destroy()
  85.                 fuckyouwin = Label(mainframe, text = 'YOU WIN', fg = 'white', bg = '#004982')
  86.                 fuckyouwin.pack()
  87.  
  88.                 fuckyou = Label(mainframe, text = 'FUCK YOU ANYWAYS', fg = 'white', bg = '#004982')
  89.                 fuckyou.pack()
  90.  
  91.                 playagain = Button(mainframe, text = 'PLAY AGAIN', fg = 'white', command = playagain, bg = '#004982')
  92.                 playagain.pack()
  93.  
  94.         def determine_count(r,c,tile,grid):
  95.             count = 0
  96.             for i in range(3):
  97.                 a = (r-1) + i
  98.                 if a >= 0 and a < len(grid):
  99.                     for i in range(3):
  100.                         b = (c-1) + i
  101.                         if b >=0 and b < len(grid):
  102.                             if self.grid[a][b] != tile:
  103.                                 if self.grid[a][b].mine:
  104.                                     count += 1
  105.             return count
  106.  
  107.         def on_left_click(r,c,i):
  108.  
  109.             if i:
  110.                 you_lose()
  111.             else:
  112.                 count = lambda x = self.grid[r][c], y = self.grid, r = r, c = c: determine_count(r,c,x,y)
  113.                 self.b[r][c].config(bg = self.bluecolors[count()], fg = 'white', text = str(count()))
  114.                 self.grid[r][c].clicked = True
  115.                 you_win()
  116.  
  117.         def on_right_click(r,c):
  118.  
  119.             if self.grid[r][c].flagged:
  120.                 if self.grid[r][c].clicked:
  121.                     count = lambda x = self.grid[r][c], y = self.grid, r = r, c = c: determine_count(r,c,x,y)
  122.                     self.b[r][c].config(bg = self.bluecolors[count()], fg = 'white', text = str(count()))
  123.                     self.grid[r][c].flagged = False
  124.                 else:
  125.                     self.grid[r][c].flagged = False
  126.                     self.b[r][c].config(text = '', bg = 'white')
  127.             else:
  128.                 self.grid[r][c].flagged = True
  129.                 self.b[r][c].config(text = '#', fg = 'white', bg = '#00CC66')
  130.  
  131.  
  132. class Tile:
  133.     def __init__(self, mine):
  134.         self.mine = mine
  135.         self.clicked = False
  136.         self.flagged = False
  137.  
  138. def welcome(root):
  139.     welcomelabel = Label(root, text = 'Welcome to MINESWEEPER!', fg = 'white', bg = '#004982')
  140.     instructionslabel = Label(root, text = 'Please type in the size of the board, then click PLAY!', fg = 'white', bg = '#004982', pady = 10)
  141.     welcomelabel.pack()
  142.     instructionslabel.pack()
  143.  
  144.     sizeentry = Entry(root, fg = '#004982', textvariable = StringVar())
  145.     sizeentry.pack()
  146.     sizeentry.focus_set()
  147.  
  148.     def play():
  149.         try:
  150.             size = int(sizeentry.get())
  151.             g = Game(size, gameboard)
  152.             welcomelabel.destroy()
  153.             instructionslabel.destroy()
  154.             sizeentry.destroy()
  155.             playbutton.destroy()
  156.             try:
  157.                 dumbass.destroy()
  158.             except:
  159.                 pass
  160.         except Exception:
  161.             dumbass = Label(gameboard, text = "Enter a number, dumbass", bg = '#004982', fg = 'white')
  162.             dumbass.pack()
  163.  
  164.     playbutton = Button(root, text = "PLAY!", fg = 'white', bg = '#004982', pady = 10, command = play)
  165.     playbutton.pack()
  166.     root.bind('<Return>', play)
  167.  
  168.     howtoplay = Label(gameboard, text = '****************************   HOW TO PLAY   **************************** \n\n Expose all the tiles, but avoid all the mines! \n Left-click to expose a tile. \n Right-click to place a flag if you think a mine is present. \n The numbers show how many mines are hidden in the adjacent tiles. \n Good Luck! \n\n ****************************************************************************', fg = '#d3d3d3', bg = '#004982' )
  169.     howtoplay.pack(side = 'bottom')
  170.  
  171.  
  172. root = Tk()
  173.  
  174. title = Label(text = "MINESWEEPER by J(. )( .)$E", bg = '#004982', fg = 'white')
  175. title.pack(side = 'top', expand = True, fill = 'both')
  176.  
  177. mainframe = Frame(root, height = 550, width = 450, pady = 25, bg = '#004982')
  178. mainframe.pack(expand = True, fill = 'both')
  179. mainframe.pack_propagate(0)
  180.  
  181. gameboard = Frame(mainframe, height = 400, width = 400, bg = '#004982')
  182. gameboard.pack(side = 'bottom')
  183. gameboard.pack_propagate(0)
  184.  
  185. welcome(mainframe)
  186.  
  187. root.mainloop()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×