【问题标题】:Adding scoring system to a number guessing game including play again将计分系统添加到数字猜谜游戏中,包括再次玩
【发布时间】:2015-01-20 11:27:07
【问题描述】:

我的游戏现在运行良好,但我仍想更进一步。我想包含一个积分系统。我希望玩家在获胜时如果还剩 1 分,则获得 5 分,如果他们有 2 条,则获得 10 分,如果他们拥有全部 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()

【问题讨论】:

  • 我建议将点存储在一个文件中,并在您进入和退出程序时将分数读写到文件中。

标签: python scoring


【解决方案1】:

您应该使用全局变量来存储得分的数量。

所以我添加了用于添加点和打印消息的代码:

#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】:

使用这种模式:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2020-09-23
    相关资源
    最近更新 更多