2014-11-22 74 views
1

现在我的游戏工作正常,但我仍想继续。我想要一个积分评分系统。我希望玩家如果他们赢了1次还剩1次,如果他们有2次则剩10次,如果他们拥有全部3次,那么他们将得到5分。但是我不希望如果他们再次玩,分数就会被重置。我只是想要它说当你退出游戏时你已经取得了分数。我试图以许多不同的方式做到这一点,但我似乎可以得到它的工作,它重新设置每次再次播放乐谱的分数。我在下面列出了我的基本游戏代码,请尝试和帮助。任何其他本游戏的推荐都非常受欢迎。将评分系统添加到猜谜游戏中,包括再次玩游戏

**我的歉意我不知道我之前是否清楚这一点。我不想被存储在得分后的节目就关上,直到当被问及再次播放**

#imports required modules 
import random 

#correct number variable created 
num = 0 

#generates number at random 
comp_num = random.randint(1,10) 

print('I\'m thinking of a number guess what it is...\n') 

#main game code 
def main(): 
    #generates number at random 
    comp_num = random.randint(1,10) 
    #set num as a global variable 
    global num 
    #lives created 
    lives = 3 
    while lives >= 1: 
     #player guesses 
     guess = int(input('Guess: ')) 
     if comp_num == guess: 
      #if correct says well done 
      print('\nWell Done! You guessed Correctly!\n') 
      break 
     elif comp_num >= guess: 
      #if guess is too low tells player 
      #one live taken for incorrect guess 
      lives = lives -1 
      print('\nToo low!\n') 
      #player is told how many lives they have left 
      print('You guessed incorrectly. You have',lives,'live(s) remaining.\n') 
      if lives == 0: 
        #if player guesses incorrectly they get told the correct awnser 
        print('The number I was thinking of was...',comp_num,'!\n') 
     elif comp_num <= guess: 
      #if guess is too high tells player 
      #one live taken for incorrect guess 
      lives = lives -1 
      print('\nToo high!\n') 
      #player is told how many lives they have left 
      print('You guessed incorrectly. You have',lives,'live(s) remaining.\n') 
      if lives == 0: 
        #if player guesses incorrectly they get told the correct awnser 
        print('The number I was thinking of was...',comp_num,'!\n') 

def end(): 
    #asks player if they want to play again 
    play_again = input('Would you like to play again?[Y/N] ') 
    while play_again.lower() == 'y': 
     #if they do game resets and plays again 
     if play_again.lower() == 'y': 
      comp_num = random.randint(1,10) 
      print('\nI\'m thinking of a number guess what it is...\n') 
      main() 
      play_again = input('Would you like to play again?[Y/N] ') 
      if play_again.lower() == 'n': 
       break 
    if play_again.lower() == 'n': 
     #if they don't game ends 
     input('Ok, Press enter to exit') 
     exit() 

#calls main section of game 
main() 


#calls end of game to give option of playing again and reseting game 
end() 
+0

我建议点存储在一个文件,读取和写入比分到文件,每当你进入和退出程序。 – Ehsan 2014-11-22 10:28:28

回答

2

使用此模式:

def game(): # Play a single game. 
    lives = 3 
    ... 
    return 5 * lives # Return the score of the game. 

def ask_again(): 
    print 'Play again?' 
    answer = ... 
    return answer == 'y' # Return True iff the user wants to play again. 

def main(): 
    score = game() 
    while ask_again(): 
    score += game() 
    print score 

main()   
1

您应该使用全局变量,用于存储得分点的数量的游戏者N按下。

所以我添加的代码添加点和打印信息:

#imports required modules 
import random 

#correct number variable created 
num = 0 

score = 0 

#generates number at random 
comp_num = random.randint(1,10) 

print('I\'m thinking of a number guess what it is...\n') 

#main game code 
def main(): 
    #generates number at random 
    comp_num = random.randint(1,10) 

    #set num as a global variable 
    global num 

    global score 

    #lives created 
    lives = 3 
    while lives >= 1: 
     #player guesses 
     guess = int(input('Guess: ')) 
     if comp_num == guess: 
      #if correct says well done 
      print('\nWell Done! You guessed Correctly!\n') 

      # add score 
      if lives == 3: 
       score += 15 
      elif lives == 2: 
       score += 10 
      elif lives == 1: 
       score += 5 

      break 
     elif comp_num >= guess: 
      #if guess is too low tells player 
      #one live taken for incorrect guess 
      lives -= 1 
      print('\nToo low!\n') 
      #player is told how many lives they have left 
      print('You guessed incorrectly. You have',lives,'live(s) remaining.\n') 
      if lives == 0: 
        #if player guesses incorrectly they get told the correct awnser 
        print('The number I was thinking of was...',comp_num,'!\n') 
     elif comp_num <= guess: 
      #if guess is too high tells player 
      #one live taken for incorrect guess 
      lives -= 1 
      print('\nToo high!\n') 
      #player is told how many lives they have left 
      print('You guessed incorrectly. You have',lives,'live(s) remaining.\n') 
      if lives == 0: 
        #if player guesses incorrectly they get told the correct awnser 
        print('The number I was thinking of was...',comp_num,'!\n') 

def end(): 

    global score 

    #asks player if they want to play again 
    play_again = input('Would you like to play again?[Y/N] ') 
    while play_again.lower() == 'y': 
     #if they do game resets and plays again 
     if play_again.lower() == 'y': 
      comp_num = random.randint(1,10) 
      print('\nI\'m thinking of a number guess what it is...\n') 
      main() 
      play_again = input('Would you like to play again?[Y/N] ') 
      if play_again.lower() == 'n': 
       break 
    if play_again.lower() == 'n': 
     #if they don't game ends 

     print("You scored " + str(score) + " amazing points!") 

     input('Ok, Press enter to exit') 
     exit() 

#calls main section of game 
main() 


#calls end of game to give option of playing again and reseting game 
end() 
+2

这仍然有问题,持续不断的时间会导致递归过载。 – 2014-11-22 10:29:00