hey guys
i have a very basic program but unfortunatly it does not recognise how many bombs are near it it just displays ok if there is no bomb. and if there is a bomb a message pops up saying bomb would you like to retry, If i press no it quits.. which is fine. how ever i want it to start a new game if i pressed yes. If this makes sense:)
i would appreciate any help :)
import Tkinter as tk
import tkMessageBox
from Tkinter import *
import sys
class BoardGame:
under_vals = [
'0', '1', '1',
'0', '1', '0',
'1', '0', '0',
'0', '1', '1', ]
disp_vals = [
'', '', '',
'', '', '',
'', '', '',
'', '', '', ]
buttons={} #empty dictionary
def __init__(self):
self.win = tk.Tk()
self.win.title("Minesweeper");
#initialisation happens here
self.choices = tk.Menu(self.win)
self.choices.add_command(label="Start new game", command=self.newGame)
self.win.config(menu=self.choices)
return
def displayBoard(self):
# create all buttons with a loop
r = 0
c = 0
count = 0
for b in self.under_vals:
rel = 'ridge'
cmd = lambda x=(r,c): self.click(x)
button=tk.Button(self.win,text='',width=5,relief=rel,command=cmd)
button.grid(row=r,column=c)
self.buttons[(r,c)]=button
count += 1
c += 1
if c > 2:
c = 0
r += 1
def newGame():
self.win.newGame
def enterMainLoop(self):
self.win.mainloop()
def click(self, i):
r,c=i
bomb=self.under_vals[r*3+c]=='1' #Evaluates whether there is a bomb or not
if not bomb:
self.buttons[i].config(bg="#00FF00", text="OK")#config is used to edit
else:
if tkMessageBox.askyesno("error", "BOMB- Try Again?"):
return newGame()
else:
quit()
tc = BoardGame()
tc.displayBoard()
tc.enterMainLoop()