簡體   English   中英

Python Connect四游戲

[英]Python Connect Four game

我目前正在制作游戲“連接四”的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()

是否有人看到任何明顯的錯誤導致游戲板按鈕不顯示? 還是有任何一般性錯誤? 謝謝。

不要忘記將按鈕打包到框架中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM