繁体   English   中英

与tkinter的Python棋盘游戏

[英]Python board game with tkinter

我试图创建一个蟒蛇游戏,玩家可以点击棋盘填充它们的颜色,直到它完成,赢得任何有更多填充框的人。 如果您单击一个框并且任何相邻的框填充了其他玩家颜色,它将更改颜色为您的颜色,我发现此板代码但我不能让它填充相邻的框。

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()

问题:当玩家点击一个方框时,我怎么能这样做?已经充满敌人颜色的相邻方框也会变成红色/绿色? 另外,我如何计算具有特定颜色的填充盒数量来确定谁赢了? 谢谢你的帮助。

如果您将标签存储在board ,则可以使用它来访问其背景颜色

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

你可以改变背景颜色

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

甚至

board[i][j]['bg'] = ...

由于您想要访问board某个点的邻居,因此使用for-loop是很自然的,例如:

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 -statement来处理越界索引:

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

使用上面的两个想法,您可以使用集合计算红色,绿色和灰色方块的数量。计数器:

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()

这需要一段时间! 这是我的版本:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM