我的游戏现在可以正常运行,但我仍然想进一步。我想要一个积分系统。我希望玩家在获胜时仅剩1个生命,从而获得5分;如果他们拥有2个,则获得15分;如果他们拥有3个,则获得15分。但是我不希望再次比赛时重新获得分数,我只想得分说退出游戏时您已经获得了“”分。我试图以多种不同的方式进行操作,但似乎可以使它工作,每次在播放中再次按y都会重置比分。我在下面包含了我的基本游戏代码,请尝试并提供帮助。非常欢迎对此游戏的任何其他推荐。

**我的道歉我不知道我之前是否足够清楚。我不希望在程序关闭后才存储分数,直到要求再次播放时玩家按n **

#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()

最佳答案

使用此模式:

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()

关于python - 在数字猜谜游戏中添加计分系统,包括再次玩,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27076239/

10-16 12:01