我试图创建一个蟒蛇游戏,玩家将单击棋盘以用其颜色填充它,直到完成为止,然后赢得有更多填充盒的人。
如果您单击一个框,并且任何相邻的框都填充有其他播放器颜色,它将颜色更改为您的颜色,我找到了此棋盘代码,但无法填充相邻的框。

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


问题:当玩家单击一个盒子时,已经充满敌人颜色的相邻盒子也会变成红色/绿色,我该怎么做?另外,我该如何计算某些颜色的已装满盒子的数量以确定谁赢了?谢谢你的帮助。

最佳答案

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

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)

关于python - Tkinter的Python棋盘游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17437634/

10-11 19:26