用户界面 - Python Tkinter:循环计算机的轮流一定次数



我正在为骰子游戏(Pig)编写一个程序。在游戏中,玩家将掷一个d6,直到他们决定保持自己的分数(传给计算机),或者直到他们掷一个1,这将自动使其轮到计算机。

我遇到的问题是,我需要让计算机循环十次的函数。我想让电脑把骰子掷十次,它要么掷一次,然后传给玩家,要么在掷十次后保持不变。如何让计算机在不使用Tk内部循环的情况下滚动模具十次?

这是代码:

from Tkinter import *
from random import *
class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.headerFont = ("courier new", "16", "bold")
        self.title("Pig, The Dice Game")
        self.headers()
        self.rollDie()
    def headers(self):
        Label(self, text = "Instructions", font = self.headerFont).grid(columnspan = 4)
        Label(self, text = "Text", font = self.headerFont).grid(row = 1, columnspan = 4)
        Label(self).grid(row = 1, columnspan = 4)
        Label(self, text = "The Game of Pig", font = self.headerFont).grid(row = 2, columnspan = 4)
    def rollDie(self):
        self.btnRoll = Button(self, text = "Roll The Die")
        self.btnRoll["state"] = 'active'
        self.btnRoll.grid(row = 3, columnspan = 4)
        self.btnRoll["command"] = self.playerTurn
        self.btnHold = Button(self, text = "Hold")
        self.btnHold["state"]= 'active'
        self.btnHold.grid(row = 4, columnspan = 4)
        self.btnHold["command"] = self.compTurn
        self.btnPass = Button(self, text = "Pass")
        self.btnPass.grid(row = 5, columnspan = 4)
        self.btnPass["command"] = self.compTurn
        Label(self, text = "You Rolled:").grid(row = 6, column = 0)
        self.lblYouRolled = Label(self, bg = "#fff", anchor = "w", relief = "groove")
        self.lblYouRolled.grid(row = 6, column = 1, columnspan = 1, sticky = "we")
        Label(self, text = "Options:").grid(row = 7, column = 0)
        self.lblOptions = Label(self, bg = "#fff", anchor = "w", relief = "groove")
        self.lblOptions.grid(row = 7, column = 1, sticky = "we")
        Label(self, text = "Player One Turn Score:").grid(row = 8, column = 0)
        self.lblPlayerOneTurnScore = Label(self, bg = "#fff", anchor = "w", relief = "groove")
        self.lblPlayerOneTurnScore.grid(row = 8, column = 1, sticky = "we")

    def playerTurn(self):
        self.oneTurnTotal = [0]
        self.oneRoll = randint(1,6)
        self.btnHold["state"] = 'active'
        self.lblYouRolled["text"] = self.oneRoll
        if self.oneRoll != 1:
            self.oneTurnTotal.append(self.oneRoll)
            self.lblOptions["text"] = "Roll again, or hold and pass the dice to Player Two."
        else:
            self.lblOptions["text"] = "You rolled a 1! Click 'Pass' to pass your turn to the computer."
            self.oneTurnTotal = [0]
            self.btnRoll["state"] = 'disabled'
            self.btnHold["state"] = 'disabled'

    def calculatePlayerOneTurnScore(self):
        turnScore = sum(self.oneTurnTotal)
        self.lblPlayerOneTurnScore["text"] = turnScore

    def compTurn(self):
        self.compTurnTotal = [0]
        self.compRoll = randint(1,6)
        self.lblYouRolled["text"] = self.compRoll
        if self.compRoll != 1:
            self.compTurnTotal.append(self.compRoll)
            self.lblOptions["text"] = "The computer will roll again."
        else:
            self.lblOptions["text"] = "The computer rolled a 1! Its turn has ended."
            self.compTurnTotal = [0]
            self.btnRoll["state"] = 'active'
    def calculatePlayerTwoTurnScore(self):
        turnScore = sum(self.twoTurnTotal)
        self.lblPlayerTwoTurnScore["text"] = turnScore

def main():
  app = App()
  app.mainloop()
if __name__ == "__main__":
  main()

您可以将骰子滚动作为顶级小部件,然后让您的根"wait_window"来完成滚动小部件。在生成滚动的顶层小部件中创建一个函数,并将其调用十次

示例

from Tkinter import *
yourscorevalue=12
cpuscorevalue=10
class dice:
    def __init__(self,parent):
        rollcount=0
        top=self.top=Toplevel(parent)
        while 1:
            roll=self.diceroll()
            rollcount+=1
            if rollcount==10 or roll==1:
                break
    def diceroll(self):
        #Random Dice Roll code
class dicegame:
    def __init__(self):
        self.root=Tk()
        self.root.title('Dicerolling Game')
        Label(self.root, text='Your Score').grid(column=0,row=0,sticky='ew')
        Label(self.root, text=yourscorevalue, background='black',foreground='red').grid(column=1,row=0,sticky='ew')
        Label(self.root, text='Computer Score').grid(column=0,row=1,sticky='ew')
        Label(self.root, text=cpuscorevalue, background='black',foreground='red').grid(column=1,row=1,sticky='ew')
        b=Button(self.root, text='Roll The Dice', command=self.roll).grid(column=0,row=2,sticky='ew')
        c=Button(self.root, text="Done", command=self.root.destroy).grid(column=1,row=2,sticky='ew')
        mainloop()
    def roll(self):
        d=dice(self.root)
        self.root.wait_window(d.top)

相关内容

  • 没有找到相关文章

最新更新