【问题标题】:Noughts and Crosses Tkinter projectNoughts and Crosses Tkinter 项目
【发布时间】:2017-01-25 04:23:03
【问题描述】:

好吧,基本上我的导师已经为我设置了一项任务,即创建一个基于 GUI 的 Noughts and Crosses 不使用类的 Python 游戏,到目前为止,这就是我想出的。我的一个问题是我怎样才能做到这一点,所以每回合它都会将玩家切换到“X”,然后切换到下一个“O”,这样他们就可以轮流了,我尝试了很多方法,但我需要帮助哈哈。 谢谢!

代码如下:

player = "O"
from tkinter import *

game = Tk()
game.title("Noughts and Crosses")

game.geometry("")

app = Frame(game)
app.grid()

def tl():
    topLeft.configure(text = player)

def tm():
    topMid.configure(text = player)

def tr():
    topRight.configure(text = player)

def ml():
    midLeft.configure(text = player)

def mm():
    midMid.configure(text = player)

def mr():
    midRight.configure(text = player)

def bl():
    botLeft.configure(text = player)

def bm():
    botMid.configure(text = player)

def br():
    botRight.configure(text = player)

#Top Row
topLeft = Button(app, text = "-", activebackground="red", command=tl)
topLeft.grid(row = 0, column = 0, ipadx=20, ipady=18, padx=10, pady=10)

topMid = Button(app, text = "-", activebackground="red", command=tm)
topMid.grid(row = 0, column = 1, ipadx=20, ipady=18, padx=10, pady=10)

topRight = Button(app, text = "-", activebackground="red", command=tr)
topRight.grid(row = 0, column = 2, ipadx=20, ipady=18, padx=10, pady=10)

#Middle Row
midLeft = Button(app, text = "-", activebackground="red", command=ml)
midLeft.grid(row = 1, column = 0, ipadx=20, ipady=18, padx=10, pady=10)

midMid = Button(app, text = "-", activebackground="red", command=mm)
midMid.grid(row = 1, column = 1, ipadx=20, ipady=18, padx=10, pady=10)

midRight = Button(app, text = "-", activebackground="red", command=mr)
midRight.grid(row = 1, column = 2, ipadx=20, ipady=18, padx=10, pady=10)


#Bottom Row
botLeft = Button(app, text = "-", activebackground="red", command=bl)
botLeft.grid(row = 2, column = 0, ipadx=20, ipady=18, padx=10, pady=10)

botMid = Button(app, text = "-", activebackground="red", command=bm)
botMid.grid(row = 2, column = 1, ipadx=20, ipady=18, padx=10, pady=10)

botRight = Button(app, text = "-", activebackground="red", command=br)
botRight.grid(row = 2, column = 2, ipadx=20, ipady=18, padx=10, pady=10)

game.mainloop()

【问题讨论】:

  • 现在我正试图弄清楚如何做到这一点,所以当点击一次时,盒子会变成零,然后当玩家 2 的玩家点击它时会变成十字架

标签: python python-3.x tkinter


【解决方案1】:

创建函数来更改player。当您使用= 时,您必须使用关键字global 来通知函数您使用外部/全局变量。如果没有global,它将创建局部变量,并且不会更改外部player

def change_player():
    global player

    if player == 'O':
        player = 'X'
    else:
        player = 'O'

然后在你的所有函数中使用这个函数 - 像这样:

def tl():
    topLeft.configure(text = player)
    change_player()

顺便说一句:为了使代码更具可读性,请将import 放在开头,并将所有函数放在game = Tk() 之前。

您可以使用for 循环来创建所有按钮并将它们保留在linst button[0]button[1] 等上。然后您可以只创建一个可以分配给所有Button 的函数。 (但它需要lambda 来分配带有参数的函数)

【讨论】:

  • 顺便说一句:您不检查按钮是否为-,因此您可以将按钮从X更改为O
【解决方案2】:

x_turn = 真

虽然不是游戏结束: turn = 'X' if x_turn else 'O'

# Your one-move code goes here

# At the bottom of the loop:
x_turn = not x_turn

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    相关资源
    最近更新 更多