Tkinterモジュールを使用してサッパーを作成する

良い一日。 ほぼすべての初心者プログラマーが彼の最初のゲームの作成に取り組んでいます。 year 骨の折れる訓練の半年後、私はサッパーを書くことにしました。 Pythonは、tkinterインターフェイスを追加するためのモジュールである記述言語として選択されました。これは、Pythonでの経験がすでにあったためです。 この投稿は初心者のコーダーにとっては便利ですが、すでにすべてを知っている場合は、コメントでコードを改善するためのヒントを書くことができます。



始めましょう。 最初のステップは、セルが何であるかを決定することでした。 最も有益なソリューションは、フィールドクラスを作成することです。



class pole(object): #  ,   Object def __init__(self,master,row, column): # . master -  Tk(). self.button = Button(master, text = ' ') #     'button' self.mine = False #     self.value = 0 #-   self.viewed = False #/  self.flag = 0 #0 -  , 1 -  , 2 -  "?" self.around = [] #,     self.clr = 'black' #  self.bg = None #  self.row = row # self.column = column #
      
      





次に、ゲームをカスタマイズするためのインターフェースを作成する必要があります。



 settings = Tk() #  settings.title('') #   settings.geometry('200x150') #  mineText = Text(settings, width = 5, height = 1) #       mineLabe = Label(settings, height = 1, text = ':') highText = Text(settings, width = 5, height = 1) highLabe = Label(settings, height = 1, text = ':') lenghtText = Text(settings, width = 5, height = 1) lenghtLabe = Label(settings, height = 1, text = ':') mineBut = Button(settings, text = ':', command = bombcounter) #  mineBut.place(x = 70, y = 90) #   mineText.place(x = 75, y = 5) mineLabe.place(x = 5, y = 5) highText.place(x = 75, y = 30) highLabe.place(x = 5, y = 30) lenghtText.place(x = 75, y = 55) lenghtLabe.place(x = 5, y = 55) settings.mainloop()
      
      





その結果、次のようなウィンドウが表示されます。



画像






今、あなたは爆弾カウンター機能を登録する必要があります



 def bombcounter(): global bombs if mineText.get('1.0', END) == '\n': #   bombs = 10 #  ,    -  - 10 else: bombs = int(mineText.get('1.0', END)) #  ,     -  if highText.get('1.0', END) == '\n': high = 9 else: high = int(highText.get('1.0', END)) if lenghtText.get('1.0', END) == '\n': lenght = 9 else: lenght = int(lenghtText.get('1.0', END)) game(high,lenght) # ,  - 
      
      





次に、ゲームの機能を記述して、主要部分に進みます。



 def game(high,lenght): #  root = Tk() root.title('') global buttons global mines global flags flags = [] #,    ,    mines = [] #,    ,    buttons = [[pole(root,j,i) for i in range(high)] for j in range(lenght)] # ,     for i in range(len(buttons)): #   for j in range(len(buttons[i])): #    buttons[i][j].button.grid(column = j, row = i, ipadx = 7, ipady = 1) #       grid buttons[i][j].button.bind('<Button-1>', buttons[i][j].view) #   buttons[i][j].button.bind('<Button-3>', buttons[i][j].setFlag) #  buttons[i][j].setAround() #   self.around buttons[0][0].button.bind('<Control-Button-1>', cheat) #      root.resizable(False,False) #   root.mainloop()
      
      





作成する必要がある関数は3つまであります。 .setAround()から始めましょう:



  def setAround(self): if self.row == 0: self.around.append([self.row+1,self.column]) if self.column == 0: self.around.append([self.row,self.column+1]) self.around.append([self.row+1,self.column+1]) elif self.column == len(buttons[self.row])-1: self.around.append([self.row,self.column-1]) self.around.append([self.row+1,self.column-1]) else: self.around.append([self.row,self.column-1]) self.around.append([self.row,self.column+1]) self.around.append([self.row+1,self.column+1]) self.around.append([self.row+1,self.column-1]) elif self.row == len(buttons)-1: self.around.append([self.row-1,self.column]) if self.column == 0: self.around.append([self.row,self.column+1]) self.around.append([self.row-1,self.column+1]) elif self.column == len(buttons[self.row])-1: self.around.append([self.row,self.column-1]) self.around.append([self.row-1,self.column-1]) else: self.around.append([self.row,self.column-1]) self.around.append([self.row,self.column+1]) self.around.append([self.row-1,self.column+1]) self.around.append([self.row-1,self.column-1]) else: self.around.append([self.row-1,self.column]) self.around.append([self.row+1,self.column]) if self.column == 0: self.around.append([self.row,self.column+1]) self.around.append([self.row+1,self.column+1]) self.around.append([self.row-1,self.column+1]) elif self.column == len(buttons[self.row])-1: self.around.append([self.row,self.column-1]) self.around.append([self.row+1,self.column-1]) self.around.append([self.row-1,self.column-1]) else: self.around.append([self.row,self.column-1]) self.around.append([self.row,self.column+1]) self.around.append([self.row+1,self.column+1]) self.around.append([self.row+1,self.column-1]) self.around.append([self.row-1,self.column+1]) self.around.append([self.row-1,self.column-1])
      
      





ここで発生するのは、self.around配列にデータを入力することだけです。 さまざまなケースを検討し、出力で正しい答えを得ます。 これを簡単にするオプションがある場合、それらを考慮します。



ビューを書く()



  def view(self,event): if mines == []: #   seter(0,self.around,self.row,self.column) #  if self.value == 0: # .     6,7  8,      self.clr = 'yellow' self.value = None self.bg = 'lightgrey' elif self.value == 1: self.clr = 'green' elif self.value == 2: self.clr = 'blue' elif self.value == 3: self.clr = 'red' elif self.value == 4: self.clr = 'purple' if self.mine and not self.viewed and not self.flag: #    ,          self.button.configure(text = 'B', bg = 'red') # ,     self.viewed = True #,    for q in mines: buttons[q[0]][q[1]].view('<Button-1>') #      lose() #   elif not self.viewed and not self.flag: #  ,        self.button.configure(text = self.value, fg = self.clr, bg = self.bg) #     self.viewed = True if self.value == None: #    for k in self.around: buttons[k[0]][k[1]].view('<Button-1>') #   
      
      





だから。 これで、関数を記述しました。セルを開き、配列を埋め、ゲームを開始し、競技場のサイズと地雷の数に関する値を取得します。 ただし、地雷を設定する機能はまだありません。 修正されました:



 def seter(q, around,row,column): #        if q == bombs: # -   = -  for i in range(len(buttons)): #   for j in range(len(buttons[i])): #     i for k in buttons[i][j].viewAround(): #      j if buttons[k[0]][k[1]].viewMine(): #     k  buttons[i][j].setValue(buttons[i][j].viewValue()+1) #    j return a = choice(buttons) #   b = choice(a) #  if [buttons.index(a),a.index(b)] not in mines and [buttons.index(a),a.index(b)] not in around and [buttons.index(a),a.index(b)] != [row,column]: #,        ,         (   ) b.setMine() #  mines.append([buttons.index(a),a.index(b)]) #    seter(q+1,around,row,column) # , ,      else: seter(q,around,row,column) #   
      
      





そして、2番目に重要な関数:setValue()



  def setValue(self,value): self.value = value
      
      





これで主要部分は終了です。 ゲームはすぐに動作しますが、ボックスをチェックして勝ち/負けを決定することはできません。 ここではすべてが簡単です。 チェックボックス:



  def setFlag(self,event): if self.flag == 0 and not self.viewed: #       self.flag = 1 #  self.button.configure(text = 'F', bg = 'yellow') #  flags.append([self.row,self.column]) #    elif self.flag == 1: #   self.flag = 2 #  '?' self.button.configure(text = '?', bg = 'blue') #  flags.pop(flags.index([self.row,self.column])) #     elif self.flag == 2: #  self.flag = 0 #    self.button.configure(text = ' ', bg = 'white') #  if sorted(mines) == sorted(flags) and mines != []: #      winer() #  
      
      





lose()およびwiner()関数は単純であり、説明は不要です。 必要に応じて、コメントに書き込みます。



最終ビュー:



画像






コメントにあなたの質問、提案、批判を書いてください。私は答え、議論し、考えようとします。



All Articles