Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- import random
- import time
- class Game:
- def __init__(self, size, root):
- self.size = size
- self.grid = [[Tile(False) for r in range(self.size)] for c in range(self.size)]
- self.root = root
- for r in range(len(self.grid)):
- root.rowconfigure(r, weight = 1)
- root.columnconfigure(r, weight = 1)
- root.grid_propagate(0)
- self.bluecolors = ['#9999ff', '#6666ff', '#3333ff', '#0000ff']
- def create_board():
- def create_random_mines():
- mines = [[None for c in r]for r in self.grid]
- available_mines = []
- #create list of available coordinates (available_mines):
- for r in range(len(self.grid)):
- for c in range(len(self.grid)):
- available_mines.append([r,c])
- #create list of randomly selected coordinates to hide mines in (mines):
- for i in range((len(self.grid)**2)/6):
- mine = random.choice(available_mines)
- available_mines.pop(available_mines.index(mine))
- self.grid[mine[0]][mine[1]].mine = True
- self.grid[mine[0]][mine[1]].clicked = True
- create_random_mines()
- #create buttons in grid:
- self.b = [[None for c in r]for r in self.grid]
- for r in range(len(self.grid)):
- for c in range(len(self.grid)):
- if self.grid[r][c].mine:
- self.b[r][c] = Button(root, text = ' ', bg = 'white')
- self.b[r][c].bind('<Button-1>', lambda a = '', r = r , c = c, mine = True: on_left_click(r,c,mine))
- else:
- self.b[r][c] = Button(root, text = ' ', bg = 'white')
- self.b[r][c].bind('<Button-1>', lambda a = '', r = r , c = c, mine = False: on_left_click(r,c,mine))
- self.b[r][c].grid(row = r, column = c, sticky = 'NESW')
- self.b[r][c].bind('<Button-3>', lambda a = '', r = r , c = c: on_right_click(r,c))
- create_board()
- def you_lose():
- def playagain():
- g = Game(self.size, gameboard)
- fuckyou.destroy()
- playagain.destroy()
- for r in range(len(self.grid)):
- for c in range(len(self.grid)):
- self.b[r][c].destroy()
- fuckyou = Label(mainframe, text = 'FUCK YOU', fg = 'white', bg = '#004982')
- fuckyou.pack(side = "top")
- playagain = Button(mainframe, text = 'PLAY AGAIN', command = playagain, fg = 'white', bg = '#004982')
- playagain.pack()
- def you_win():
- def playagain():
- g = Game(self.size, gameboard)
- fuckyou.destroy()
- fuckyouwin.destroy()
- playagain.destroy()
- unclicked_tiles=[]
- for r in range(len(self.grid)):
- for c in range(len(self.grid)):
- if self.grid[r][c].clicked == False:
- unclicked_tiles.append(self.grid[r][c])
- if len(unclicked_tiles)==0:
- for r in range(len(self.grid)):
- for c in range(len(self.grid)):
- self.b[r][c].destroy()
- fuckyouwin = Label(mainframe, text = 'YOU WIN', fg = 'white', bg = '#004982')
- fuckyouwin.pack()
- fuckyou = Label(mainframe, text = 'FUCK YOU ANYWAYS', fg = 'white', bg = '#004982')
- fuckyou.pack()
- playagain = Button(mainframe, text = 'PLAY AGAIN', fg = 'white', command = playagain, bg = '#004982')
- playagain.pack()
- def determine_count(r,c,tile,grid):
- count = 0
- for i in range(3):
- a = (r-1) + i
- if a >= 0 and a < len(grid):
- for i in range(3):
- b = (c-1) + i
- if b >=0 and b < len(grid):
- if self.grid[a][b] != tile:
- if self.grid[a][b].mine:
- count += 1
- return count
- def on_left_click(r,c,i):
- if i:
- you_lose()
- else:
- count = lambda x = self.grid[r][c], y = self.grid, r = r, c = c: determine_count(r,c,x,y)
- self.b[r][c].config(bg = self.bluecolors[count()], fg = 'white', text = str(count()))
- self.grid[r][c].clicked = True
- you_win()
- def on_right_click(r,c):
- if self.grid[r][c].flagged:
- if self.grid[r][c].clicked:
- count = lambda x = self.grid[r][c], y = self.grid, r = r, c = c: determine_count(r,c,x,y)
- self.b[r][c].config(bg = self.bluecolors[count()], fg = 'white', text = str(count()))
- self.grid[r][c].flagged = False
- else:
- self.grid[r][c].flagged = False
- self.b[r][c].config(text = '', bg = 'white')
- else:
- self.grid[r][c].flagged = True
- self.b[r][c].config(text = '#', fg = 'white', bg = '#00CC66')
- class Tile:
- def __init__(self, mine):
- self.mine = mine
- self.clicked = False
- self.flagged = False
- def welcome(root):
- welcomelabel = Label(root, text = 'Welcome to MINESWEEPER!', fg = 'white', bg = '#004982')
- instructionslabel = Label(root, text = 'Please type in the size of the board, then click PLAY!', fg = 'white', bg = '#004982', pady = 10)
- welcomelabel.pack()
- instructionslabel.pack()
- sizeentry = Entry(root, fg = '#004982', textvariable = StringVar())
- sizeentry.pack()
- sizeentry.focus_set()
- def play():
- try:
- size = int(sizeentry.get())
- g = Game(size, gameboard)
- welcomelabel.destroy()
- instructionslabel.destroy()
- sizeentry.destroy()
- playbutton.destroy()
- try:
- dumbass.destroy()
- except:
- pass
- except Exception:
- dumbass = Label(gameboard, text = "Enter a number, dumbass", bg = '#004982', fg = 'white')
- dumbass.pack()
- playbutton = Button(root, text = "PLAY!", fg = 'white', bg = '#004982', pady = 10, command = play)
- playbutton.pack()
- root.bind('<Return>', play)
- 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' )
- howtoplay.pack(side = 'bottom')
- root = Tk()
- title = Label(text = "MINESWEEPER by J(. )( .)$E", bg = '#004982', fg = 'white')
- title.pack(side = 'top', expand = True, fill = 'both')
- mainframe = Frame(root, height = 550, width = 450, pady = 25, bg = '#004982')
- mainframe.pack(expand = True, fill = 'both')
- mainframe.pack_propagate(0)
- gameboard = Frame(mainframe, height = 400, width = 400, bg = '#004982')
- gameboard.pack(side = 'bottom')
- gameboard.pack_propagate(0)
- welcome(mainframe)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement