如何让游戏按钮显示在基于Python的游戏中?

发布于 2024-12-10 12:56:57 字数 2590 浏览 0 评论 0原文

我目前正在制作《四子棋》游戏的 Python 版本。这将是非常基本的,使用 Tkinter 作为界面。我认为我已经布置了大部分代码,并且没有收到任何错误,但它目前没有创建实际的游戏板。现在,我只将其编码为使第一行为 7(游戏板为 7x6),但它没有显示。

这是代码:

from Tkinter import *

class cell:
    def __init__(self,cellNum,frame,game):
        self.Empty=PhotoImage(file='empty.gif')
        self.Black=PhotoImage(file='black.gif')
        self.Red=PhotoImage(file='red.gif')
        self.b = Button(frame,image=self.Empty,command=self.makeMove)
        self.frame = frame
        self.game = game
        self.num = cellNum
        
    def pack(self):
        self.gameSquare.pack()

    def makeMove(self):
        Player = self.game.Player
        if self.Player == 'Black':
            self.b.config(image=self.Black)
            num = self.num
            self.game.moves[Player].append(num)
            self.game.free.remove(num)
            self.Player = 'Red'
        else:
            self.b.config(image=self.Red)
            self.Player = 'Black'
        self.turninfo.config(text=Player+"'s Turn")

    def restart(self):
        self.b.config(image=self.Empty)

class game:
    def __init__(self):
        self.win = Tk()
        self.win.title('Connect Four')
        self.win.config(bg="blue")
        self.cells=[]
        self.free = range(42)
        self.moves = { 'X' : [ ], 'O' : [ ] }
        self.Row1 = Frame(self.win)
        for i in range(7):
            self.cells.append(cell(i,self.Row1,self))
        self.Player = 'Black'

        self.titleFrame = Frame(self.win)
        self.title = Label(self.win,text="Connect Four",font=(200),fg='white', bg='blue')


        self.middleRow = Frame(self.win)
        self.turninfo = Label(self.middleRow,text=self.Player+"'s Turn", font=(200),fg='white',bg='blue')

        self.bottomRow = Frame(self.win)
        self.quitbutton = Button(self.bottomRow, text="Quit", command=self.win.destroy, font=(200))
        self.playbutton = Button(self.bottomRow, text="Play Again", command=self.restart, font=(200))

        self.titleFrame.pack()
        self.title.pack()
        self.Row1.pack()
        self.turninfo.pack()
        self.middleRow.pack()
        self.bottomRow.pack()
        self.quitbutton.pack(side="left")
        self.playbutton.pack(side="right")
        self.win.mainloop()

    def restart(self):
        self.Player = 'Black'
        self.turninfo.config(text=self.Player+"'s Turn")
        self.free = range(42)
        self.moves = {'Black' : [ ], 'Red' : [ ]}
        for c in self.cells:
            c.restart()

game = game()
        

有人看到任何导致游戏板按钮不显示的明显错误吗?或者一般有什么错误吗?

I am currently working on making a Python version of the game Connect Four. It is going to be pretty basic, using Tkinter for the interface. I think I have most of the code laid out and I'm not getting any errors, but it currently isn't creating the actual game board. Right now, I only have it coded to make the first row of 7 (the gameboard is 7x6), but it's not showing up.

Here is the code:

from Tkinter import *

class cell:
    def __init__(self,cellNum,frame,game):
        self.Empty=PhotoImage(file='empty.gif')
        self.Black=PhotoImage(file='black.gif')
        self.Red=PhotoImage(file='red.gif')
        self.b = Button(frame,image=self.Empty,command=self.makeMove)
        self.frame = frame
        self.game = game
        self.num = cellNum
        
    def pack(self):
        self.gameSquare.pack()

    def makeMove(self):
        Player = self.game.Player
        if self.Player == 'Black':
            self.b.config(image=self.Black)
            num = self.num
            self.game.moves[Player].append(num)
            self.game.free.remove(num)
            self.Player = 'Red'
        else:
            self.b.config(image=self.Red)
            self.Player = 'Black'
        self.turninfo.config(text=Player+"'s Turn")

    def restart(self):
        self.b.config(image=self.Empty)

class game:
    def __init__(self):
        self.win = Tk()
        self.win.title('Connect Four')
        self.win.config(bg="blue")
        self.cells=[]
        self.free = range(42)
        self.moves = { 'X' : [ ], 'O' : [ ] }
        self.Row1 = Frame(self.win)
        for i in range(7):
            self.cells.append(cell(i,self.Row1,self))
        self.Player = 'Black'

        self.titleFrame = Frame(self.win)
        self.title = Label(self.win,text="Connect Four",font=(200),fg='white', bg='blue')


        self.middleRow = Frame(self.win)
        self.turninfo = Label(self.middleRow,text=self.Player+"'s Turn", font=(200),fg='white',bg='blue')

        self.bottomRow = Frame(self.win)
        self.quitbutton = Button(self.bottomRow, text="Quit", command=self.win.destroy, font=(200))
        self.playbutton = Button(self.bottomRow, text="Play Again", command=self.restart, font=(200))

        self.titleFrame.pack()
        self.title.pack()
        self.Row1.pack()
        self.turninfo.pack()
        self.middleRow.pack()
        self.bottomRow.pack()
        self.quitbutton.pack(side="left")
        self.playbutton.pack(side="right")
        self.win.mainloop()

    def restart(self):
        self.Player = 'Black'
        self.turninfo.config(text=self.Player+"'s Turn")
        self.free = range(42)
        self.moves = {'Black' : [ ], 'Red' : [ ]}
        for c in self.cells:
            c.restart()

game = game()
        

Does anyone see any obvious errors causing the gameboard buttons not to show up? Or any errors in general?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-12-17 12:56:57

不要忘记将按钮装入框架中:

    self.b = Button(frame,image=self.Empty,command=self.makeMove)
    self.b.pack(side=LEFT)

Don't forget to pack the buttons into the frame:

    self.b = Button(frame,image=self.Empty,command=self.makeMove)
    self.b.pack(side=LEFT)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文