2017-05-09 64 views
1

我是新來這個網站,所以請原諒我,如果我做錯了。我正在研究一個項目,我們正在試圖製作一個版本的掃雷器,它將具有特定的地雷和星座形狀。如何銷燬網格並創建一個新網格

我們試圖讓它看起來像關卡,如果玩家獲勝,他們會進入下一個關卡。唯一的問題是,在我們的勝利函數中,我們無法弄清楚如何用新礦創建新的網格,並刪除舊的網格,而不用堆疊或摧毀整個框架。

您可以從我們的github賬戶獲得的圖像文件:https://github.com/Riddler6897/Star-Minesweeper

它還擁有最新的代碼被稱爲TestRun.py文件中,所有的圖像都在images文件夾中。

這裏每個人的方便,這裏有四個.gif圖片:

plaintile.gif:plaintile.gif
clickedtile.gif:clickedtile.gif
minetile.gif:minetile.gif
flagtile.gif:flagtile.gif

from Tkinter import * #We could also use pygame# 
from time import time #To set a timer# 
from time import sleep #To make the RGBs turn on and off# 
#import RPi.GPIO as GPIO 
class Minesweeper(Frame): 

    def __init__(self, master): 

     self.images = [PhotoImage(file = "images/plaintile.gif"), 
         PhotoImage(file = "images/clickedtile.gif"), 
         PhotoImage(file = "images/minetile.gif"), 
         PhotoImage(file = "images/flagtile.gif")] 
     self.NoTile = [] 
     Frame.__init__(self, master) 
     self.master = master 
#populates NoTile with images corresponding to the number of mines adjacent to each tile 
     #for x in range (1,9): 
      #self.NoTile.append(PhotoImage(file = "images/tile_"+str(x)+".gif")) 

     self.button = [] 
#arrays containing the position of each mine that will make the shape of the constellations 
     self.Libra = [23, 63, 102, 124, 154, 161, 197, 209] 
     self.Orion = [0, 2, 16, 30, 62, 66, 73, 89, 93, 103, 113, 118, 125, 132, 139, 146, 153, 211, 217] 
     self.Phoenix = [22, 44, 45, 78, 85, 121, 148, 169, 203] 
     self.Taurus = [3, 30, 35, 64, 67, 83, 95, 99, 113, 129, 146, 163, 178, 204, 223] 
     self.CanisMajor = [21, 49, 69, 73, 81, 101, 140, 144, 186, 197, 203, 212] 
     self.Draco = [32, 51, 63, 67, 96, 127, 157, 161, 167, 173, 193, 195, 198, 212, 224] 
     self.CanesVenatici = [42, 182] 
     self.Chameleon = [66, 104, 106, 133, 140] 
     self.Crater = [4, 22, 98, 120, 142, 153, 164, 220] 
     self.Vulpecula = [2, 51, 61, 83, 114, 145, 178, 209] 
     self.levels = [self.CanesVenatici, self.Chameleon, self.Libra, self.Crater, self.Vulpecula, self.Phoenix, self.CanisMajor, self.Taurus, self.Draco, self.Orion] 
#an array containing all the constellations in order of "difficulty" 
     self.level = 0 
     self.score = 1 
     self.pressed = 0 
     self.tiles = 225-len(self.levels[self.level]) 
#labels showing the current level and the number of mines in the level   
     self.label1 = Label(master, text = "Mines: "+str(len(self.levels[self.level]))) 
     self.label1.grid(row = 16, column = 0, columnspan = 5) 
     self.label2 = Label(master, text = "Level: "+ str(self.score)) 
     self.label2.grid(row = 16, column = 5, columnspan = 5) 
     self.label3 = Label(master, text = "Tiles Left: "+str(self.pressed)) 
     self.label3.grid(row = 16, column = 10, columnspan = 5) 



    def buttonPressed(self, i): 
     self.pressed += 1 
     self.tiles -= 1 
     self.label3 = Label(self.master, text = "Tiles Left: "+str(self.tiles)) 
     self.label3.grid(row = 16, column = 10, columnspan = 5) 
     self.button[i].config(image = self.images[1], state=DISABLED) 
     if self.tiles == 200: 
      self.win() 

    def minePressed(self, i): 
     self.button[i].config(image=self.images[2]) 
     #self.lose() 

    def constellation_plot(self): 
     for i in self.levels[self.level]: 
      self.button[i].config(command = lambda i=i: self.minePressed(i)) 

    #def update_mine(self): 
     #pass 

    def win(self): 
     self.master.grid_forget() 
     self.score += 1 
     self.level += 1 
     self.pressed = 0 
     self.tiles = 225-len(self.levels[self.level]) 
     self.grid() 



    def grid(self): #This will make the grid# 
     i = 0 
     for r in range(15): 
      for c in range(15): 
       self.mGrid = Button(self.master, image=self.images[0], command = lambda i=i :self.buttonPressed((i))) 
       self.button.append(self.mGrid) 
       self.mGrid.image = self.images[0] 
       self.mGrid.grid(row=r, column=c) 
       i += 1 

     self.constellation_plot() 


    def play(): #Will actually run the game# 
     pass 

    #def lose(self): #Will use GPIO for RGBs and allow the player to start the same level over# 
     #quit(1) 


    def time_out(): #timer. When the time goes out, the whole program will stop running while still showing score.# 
     pass 

    def sound(): #To play a sound if they win or lose. Can be put under the lose function# 
     pass 

window = Tk() 
minesweeper = Minesweeper(window) 
window.title("Minesweeper") 
minesweeper.grid() 
window.mainloop() 
+0

爲了讓其他人嘗試運行你的代碼,它會有幫助,他們有4個被引用的'gif'圖像的副本。由於您無法將它們嵌入到您的問題中,因此我建議將它們中的每一個上傳到免費的在線圖像共享網站之一,如http://imgur.com,並在您的問題中添加鏈接。 – martineau

+0

將發送鏈接到我們的github帳戶工作? –

+0

是的,我想是的,[編輯]你的問題,並添加鏈接。 – martineau

回答

0

可能還有一些其他問題,但要回答你的問題,你可以添加一個remove_button_grid()方法,並使用self.remove_button_grid(而不是您目前在代碼中使用的self.master.grid_forget())在win()函數中調用它。

這是新的方法應該是什麼樣子:

def remove_button_grid(self): 
     while self.button: 
      button = self.button.pop() 
      button.destroy() 

順便說一句,我認爲self.buttons將爲按鈕列表是一個更好的名字屬性。另外,我認爲你應該重新命名Minesweeper類別的grid()方法,因爲它是從Frame繼承的佈局管理器方法的名稱,並且做了與子類中定義的完全不同的東西,因爲不這樣做可能會使熟悉的人Tkinter - 它直到間接地發表評論爲止。

+0

非常感謝你幫助我們!它完全解決了我們的問題。我非常感謝所有非常快速和有益的迴應 –

+1

我不會將它命名爲'grid_forget',因爲它與內置函數名稱衝突。雖然它會起作用,但會導致代碼混淆,因爲大多數閱讀它的人不會期望'grid_forget'實際上銷燬一個小部件而不是忘記它。 –

+1

@Bryan:非常好的一點 - 一般來說很合理的建議。除此之外,對於一個相關的問題來說,這是一個了不起的階段,儘管這個問題有點問題主題。類'Minesweeper',一個'Tkinter.Frame'子類,定義了它自己的'grid()'方法,它與同名Tkinter佈局管理器無關。具有諷刺意味的是,這一事實讓我忘記了在我的答案中編寫代碼時「Tkinter」佈局管理器的方法。所以更好的一點是你說的也應用於現有的'Minesweeper.grid()'方法。 – martineau