需要帮助完成飞镖评分系统

时间:2014-06-04 00:17:33

标签: python

我创建了一个飞镖评分系统,可以接受多个玩家并显示在tkinter GUI上。就像真正的飞镖游戏一样,每个玩家都以501分开始,我有从该分数中减去的按钮来模拟飞镖投掷。

我差不多完成了,除非我想要,所以球员得分不会超过0,因为当得分没有停在0时它会变成负数。例如,如果一名球员得到5分并且他们获得了20分,则得分将变为-15而不是0。 一些帮助将不胜感激!

这是我的代码(对不起,相当长):

import tkinter
from tkinter import *

class Application(object):

    def __init__(self):
        self.playerNames = []
        self.playerScores = []
        self.root = tkinter.Tk()

        self.gameCounter=0

        self.numberPlayers=0
        self.inputNumber=0
        self.root.title("Darts!")
        self.root.configure(background="light blue")

        self.welcomeLabel = tkinter.Label(text="Welcome to Darts!", bg="light blue", fg="red")
        self.welcomeLabel.grid(column=0, row=0)

        self.dartsLabel = tkinter.Label(text = "Enter Player names in one by one", bg="light blue", fg="Dark blue")
        self.dartsLabel.grid(column=0, row=1)

        self.nameEntry = tkinter.Entry(text="OK", fg="purple")
        self.nameEntry.grid(column=0, row=2)

        self.btnAddListItem = tkinter.Button(text = "Add Player", command = self.addNewName, bg="light grey")
        self.btnAddListItem.grid(column=0, row=3)

        self.finishedAdding = tkinter.Button(text = "Play Darts!", command = self.displayNames, bg="light grey")
        self.finishedAdding.grid(column=0, row=4)

        self.QuitBtn = tkinter.Button(text="Quit", command=self.comfirmQuit, bg="light grey")
        self.QuitBtn.grid(column=0, row=20)

    def comfirmQuit(self):
        self.comfirmQuitLabel = tkinter.Label(text="Are you sure?", bg="light blue", fg="red")
        self.comfirmQuitLabel.grid(column=0, row=21)

        self.comfirmQuitButton = tkinter.Button(text="Yes", command= self.quitGame, bg="light grey")
        self.comfirmQuitButton.grid(column=0, row=22)

        self.cancelQuitButton = tkinter.Button(text="No", command=self.cancelQuit, bg="light grey")
        self.cancelQuitButton.grid(column=0, row=23)

    def quitGame(self):
        global root
        self.root.destroy()

    def cancelQuit(self):
        self.comfirmQuitButton.destroy()
        self.cancelQuitButton.destroy()
        self.comfirmQuitLabel.destroy()

    def addNewName(self):
        Inputname = self.nameEntry.get()
        self.playerNames.append(Inputname)
        self.nameEntry.delete(0, tkinter.END)

    def displayNames(self):
        self.welcomeLabel.destroy()
        self.dartsLabel.destroy()
        self.nameEntry.destroy()
        self.btnAddListItem.destroy()
        self.finishedAdding.destroy()
        self.labelsForPlayerNames = []
        self.numberPlayers=len(self.playerNames)
        while self.numberPlayers>self.gameCounter:
            self.playerScores.append(501)
            self.gameCounter+=1
        self.turnCounter=0
        self.playerCounter=0
        self.roundNumber=1
        self.theScore()

    def theScore(self):

        self.dartsLabel = tkinter.Label(text = "Turn: " + self.playerNames[0], bg="light blue", fg="dark blue")
        self.dartsLabel.grid(column=0, row=0)
        self.playerScoreLabel = tkinter.Label(text =self.playerScores[0], bg="light blue", fg="red")
        self.playerScoreLabel.grid(column=0, row=3)
        self.scoreSpin=Spinbox(from_ =1, to=20, bg="light grey", fg="purple")
        self.scoreSpin.grid(column=0, row=2)

        self.btnAddListItem = tkinter.Button(text = "No Multiplier", command = self.singleScore, bg="light grey")
        self.btnAddListItem.grid(column=0, row=4)
        self.btnAddListItem = tkinter.Button(text = "2x Multiplier", command = self.doubleScore, bg="light grey")
        self.btnAddListItem.grid(column=0, row=5)
        self.btnAddListItem = tkinter.Button(text = "3x Multiplier", command = self.tripleScore, bg="light grey")
        self.btnAddListItem.grid(column=0, row=6)
        self.btnAddListItem = tkinter.Button(text = "Half Bullseye", command = self.scoreHalfBullseye, bg="light grey")
        self.btnAddListItem.grid(column=0, row=7)
        self.btnAddListItem = tkinter.Button(text = "Bullseye", command = self.scoreBullseye, bg="light grey")
        self.btnAddListItem.grid(column=0, row=8)

    def singleScore(self):
        self.inputNumber = int(self.scoreSpin.get())
        self.scoringSystem()
    def doubleScore(self):
        self.inputNumber = int(self.scoreSpin.get())
        self.inputNumber=self.inputNumber*2
        self.scoringSystem()

    def tripleScore(self):
        self.inputNumber = int(self.scoreSpin.get())
        self.inputNumber=self.inputNumber*3
        self.scoringSystem()
    def scoreHalfBullseye(self):
        self.inputNumber = 25
        self.scoringSystem()
    def scoreBullseye(self):
        self.inputNumber = 50
        self.scoringSystem()

    def scoringSystem(self):
        self.turnCounter+=1
        self.playerScores[self.playerCounter]=self.playerScores[self.playerCounter]-self.inputNumber
        self.playerScoreLabel.config(text= self.playerScores[self.playerCounter])
        self.dartsLabel.grid(column=0, row=3)

        if self.turnCounter>2:                
            self.playerCounter+=1
            self.turnCounter=0

        if self.playerCounter>=self.numberPlayers:
            self.playerCounter=0
            self.roundNumber+=1
            self.displayScores()
        self.dartsLabel.config(text= "Turn: " + self.playerNames[self.playerCounter])
        self.dartsLabel.grid(column=0, row=0)
        self.root.wm_title("Round: %d" % self.roundNumber)

    def displayScores(self):
        self.dartsLabel.destroy()
        self.scoreSpin.destroy()
        self.btnAddListItem.destroy()
        self.root.wm_title("Round Scores")
        self.theScore()   

myApp = Application()
myApp.root.mainloop()

1 个答案:

答案 0 :(得分:0)

假设当潜在分数大于实际分数时你不想发生任何事情:

if potential_score > actual_score:
    pass
else:
    compute_score()