2013-07-02 59 views
1

我试图创建一个python游戏,玩家将点击棋盘填充它的颜色直到它完成,赢得谁拥有更多的填充框。 如果你点击一个盒子,任何相邻的盒子都装满了其他玩家的颜色,它会改变你的颜色,我发现这个板子代码,但我不能让它填充相邻的盒子。与tkinter Python的棋盘游戏

import Tkinter as tk 

board = [ [None]*10 for _ in range(10) ] 

counter = 0 

root = tk.Tk() 

def on_click(i,j,event): 
    global counter 
    color = "green" if counter%2 else "red" 
    event.widget.config(bg=color) 
    board[i][j] = color 
    counter += 1 


for i,row in enumerate(board): 
    for j,column in enumerate(row): 
     L = tk.Label(root,text=' ',bg='grey') 
     L.grid(row=i,column=j,padx='3',pady='3') 
     L.bind('<Button-1>',lambda e i=i,j=j: on_click(i,j,e)) 

root.mainloop() 

问题:我怎样才能使它所以当玩家点击一个箱子相邻的箱子,都已经充满了敌人的颜色也变为红色/绿色的吗?另外我怎样才能计算具有某种颜色的填充框的数量,以确定谁赢了?谢谢你的帮助。

回答

1

这花了一段时间!这里是我的版本:

import Tkinter as tk 
import TkMessageBox as messagebox 

board = [ [None]*10 for _ in range(10) ] 

counter = 0 
root = tk.Tk() 

def check_board(): 
    freespaces = 0 
    redspaces = 0 
    greenspaces = 0 
    for i,row in enumerate(board): 
     for j,column in enumerate(row): 
      if board[i][j] == "red": 
       redspaces += 1 
      elif board[i][j] == "green": 
       greenspaces += 1 
      elif board[i][j] == None: 
       freespaces += 1 

    if freespaces == 0: 
     if greenspaces > redspaces: 
      winner = "green" 
     elif greenspaces < redspaces: 
      winner = "red" 
     else: 
      winner = "draw" 

     if winner != "draw": 
      messagebox.showinfo("Game Over!",winner+" wins!") 
     else: 
      messagebox.showinfo("Game Over!","The game was a draw!") 




def on_click(i,j,event): 
    global counter 
    if counter < 100: 
     if board[i][j] == None: 
      color = "green" if counter%2 else "red" 
      enemycolor = "red" if counter%2 else "green" 
      event.widget.config(bg=color) 
      board[i][j] = color 
      for k in range(-1,2): 
       for l in range(-1,2): 
        try: 
         if board[i+k][j+l] == enemycolor: 
          board[i+k][j+l] = color 
        except IndexError: 
         pass 
      counter += 1 
      global gameframe 
      gameframe.destroy() 
      redraw() 
      root.wm_title(enemycolor+"'s turn") 
     else: 
      messagebox.showinfo("Alert","This square is already occupied!") 
     check_board() 


def redraw(): 
    global gameframe 
    gameframe = tk.Frame(root) 
    gameframe.pack() 

    for i,row in enumerate(board): 

     for j,column in enumerate(row): 
      name = str(i)+str(j) 
      L = tk.Label(gameframe,text=' ',bg= "grey" if board[i][j] == None else board[i][j]) 
      L.grid(row=i,column=j,padx='3',pady='3') 
      L.bind('<Button-1>',lambda e,i=i,j=j:on_click(i,j,e)) 


redraw() 
root.mainloop() 

我重绘每次整板,因为没有存储的参考部件。我无法在创建完成后访问每个窗口小部件,因为它们都被称为“L”,所以我检查了电路板上的颜色值,并根据是否着色创建了窗口小部件。通过查看广场周围的3x3网格中的颜色进行检查。

我添加了一个函数来检查正方形,然后检测它们是否全部满了,如果有任何问题让我知道,您应该可以通过研究代码来计算出发生了什么。我添加的一个很好的触摸是根据轮到谁来改变标题栏!

编辑:要添加通知当前播放器颜色的标签,请将以下内容添加到重绘功能的末尾!

global counter 
whosturn = "Green" if counter%2 else "Red" 
turnLbl = tk.Label(gameframe,text=color+"'s Turn") 
turnLbl.grid(row=11,column = 0,columnspan = 10) 
+0

感谢您的帮助!我有个问题。将可能发出的警告是什么玩家把它作为标签而不是在窗口中作为标签在板上,或者它需要对板重新编码并创建一个类?我混淆董事会的创建方式。 –

+0

是!更新的答案,将最后一部分添加到重绘功能。理想情况下,您应该从头开始重新创建整个程序,并在课程中正确编码以了解更多信息。使用self来传递变量会更好。和Tk变量而不是使用凌乱的全局变量。 – mbdavis

2

如果您存储在board的标签,那么你就可以

board[i][j]['bg'] 

访问它的背景颜色,你可以用

board[i][j].config(bg=...) 

甚至

board[i][j]['bg'] = ... 
改变背景颜色

既然你要访问的board一个点的邻居,很自然地使用for-loop S,从而为:

for ii in range(i - 1, i + 2): 
    for jj in range(j - 1, j + 2): 

或等价但较少的嵌套使用itertools.product

import itertools as IT 
for ii, jj in IT.product(range(i - 1, i + 2), range(j - 1, j + 2)): 

现在您可以通过board[ii][jj]访问邻居,但请注意,iijj可能是超出界限的索引。

if ii<0 or ii>=rows or jj<0 or jj>=cols: continue 

把使用上述两种思路, 您使用可以指望的红色,绿色和灰色方块的数量:我们可以通过使用if语句来处理出界外指标collections.Counter

import collections 
collections.Counter(
     board[i][j]['bg'] for i, j in IT.product(range(rows), range(cols))) 

import Tkinter as tk 
import itertools as IT 
import collections 

cols, rows = 3, 3 
board = [[None] * cols for _ in range(rows)]  
other = {'green': 'red', 'red': 'green'} 

player = 'red' 

def on_click(event, i, j): 
    global player 
    board[i][j]['bg'] = player 
    for ii, jj in IT.product(range(i - 1, i + 2), range(j - 1, j + 2)): 
     if ii<0 or ii>=rows or jj<0 or jj>=cols: continue 
     neighbor = board[ii][jj] 
     if neighbor['bg'] != 'grey' and (ii, jj) != (i, j): 
      neighbor['bg'] = other[neighbor['bg']] 
    check_for_winner() 
    player = other[player] 

def check_for_winner(): 
    s = score() 
    if s['red'] + s['green'] == cols*rows: 
     # every box filled 
     winner = max(s, key=s.get) 
     print('Winner is: {}'.format(winner)) 
     root.after(1, flash_winner, winner, 'blue') 

def score(): 
    return collections.Counter(
     board[i][j]['bg'] for i, j in IT.product(range(rows), range(cols))) 

def flash_winner(winner, altcolor): 
    for i, j in IT.product(range(rows), range(cols)): 
     if board[i][j]['bg'] == winner: 
      board[i][j]['bg'] = altcolor 
    root.after(250, flash_winner, altcolor, winner) 

root = tk.Tk() 
for i, j in IT.product(range(rows), range(cols)): 
    board[i][j] = L = tk.Label(root, text=' ', bg='grey') 
    L.grid(row=i, column=j, padx=3, pady=3) 
    L.bind('<Button-1>', lambda e, i=i, j=j: on_click(e, i, j)) 

root.mainloop() 
+0

非常感谢!现在我必须研究一些你以前了解的东西。 –