【发布时间】: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()
【问题讨论】:
-
我建议将点存储在一个文件中,并在您进入和退出程序时将分数读写到文件中。