如何在python中运行main函数中的函数?

时间:2017-06-24 07:41:36

标签: python function class methods pygame

请帮助我完成编程任务的代码。我试图运行RollDice()函数,CheckForLaddders()函数&我的main()函数中的CheckForSnakes()函数将运行主游戏。我已经定义了所有全局变量,如currentPosition1,currentPosition2,ladderList,snakesList& nameList等。

当我运行我的代码时,它说没有定义RollDice() - 这是我在main中调用的初始函数。基本上,我如何在代码的上下文中调用这些函数?我认为将所有上述4个函数放在一个类(gameOn)中意味着它们可以相互访问,实际上变成了方法。但我想我只是没有正确执行此操作。

我知道我的逻辑是正确的,但我只是python /编码的新手..请帮忙!

class gameOn:

 def RollDice():
    if nameList.index(player) == 0:
        diceRollValue = randrange(1,7,1)
        currentPosition1 += diceRollValue
        print (player, " dice is: ", diceRollValue, ", New position is: ", currentPosition1)
        if nameList.index(player) == 1:
            diceRollValue = randrange(1,7,1)
            currentPosition2 += diceRollValue
            print (player, " dice is: ", diceRollValue, ", New position is: ", currentPosition2)

 def CheckForLadder():
    if nameList.index(player) == 0:
        if currentPosition1 in ladderList:
            currentPosition1 += 15
            print ("Great ", player, " ! It's a ladder, Climb up by 15 cells. Your new position is: ", currentPosition1)
    if nameList.index(player)== 1:
        if currentPosition2 in LadderList:
            currentPosition2 += 15
            print ("Great ", player, " ! It's a ladder, Climb up by 15 cells. Your new position is: ", currentPosition2)


 def CheckForSnake():
    if nameList.index(player)== 0:
        if currentPosition1 in snakesList:
            currentPosition1 = currentPosition1 - 10
            print ("Oops!  ", player, " ! You've been bitten, go down 10 cells. Your new position is: ", currentPosition1)
    if nameList.index(player)== 1:
        if currentPosition2 in snakesList:
            currentPosition2 = currentPosition2 - 10
            print ("Oops!  ", player, " ! You've been bitten, go down 10 cells. Your new position is: ", currentPosition2)


 def main():
      while (currentPosition1 == 0 and currentPosition2 == 0):
           for player in nameList:
                RollDice()# How do I run this function which I've created above?
           print("\n")
      while (currentPosition1 <= 105 or currentPosition2 <= 105):
           for player in nameList:
                RollDice()# How do I run this function which I've created above?
                CheckForLadder()# How do I run this function which I've created above?
                CheckForSnake()# How do I run this function which I've created above?
           print("\n")

           if currentPosition1 >= 100:
                print ("\n")
                print  ("Huuuuray! Winner is ", player1)
                print ("Press any key to exit")
                break

           if currentPosition2 >= 100:
                print ("\n")
                print  ("Huuuuray! Winner is ", player2)
                print ("Press any key to exit")
                break
 main()

1 个答案:

答案 0 :(得分:1)

如果您创建了一个类,则必须首先添加self参数,如下所示:

class GameOn():

  def RollDice(self):
    ...

  def CheckForLadder(self):
    ...

  def CheckForSnake(self):
    ...


  def main(self):
      while (currentPosition1 == 0 and currentPosition2 == 0):
           for player in nameList:
                self.RollDice()
           print("\n")

      while (currentPosition1 <= 105 or currentPosition2 <= 105):
           for player in nameList:
                self.RollDice()
                self.CheckForLadder()
                self.CheckForSnake()
           print("\n")

           if currentPosition1 >= 100:
                print ("\n")
                print  ("Huuuuray! Winner is ", player1)
                print ("Press any key to exit")#finish this
                break

           if currentPosition2 >= 100:
                print ("\n")
                print  ("Huuuuray! Winner is ", player2)
                print ("Press any key to exit")#finish this
                break

game = GameOn()
game.main()