gpt4 book ai didi

python - 在猜数字游戏中添加计分系统,包括再次玩

转载 作者:行者123 更新时间:2023-11-30 23:17:56 25 4
gpt4 key购买 nike

我的游戏现在运行良好,但我仍然想更进一步。我想要一个积分系统。我希望玩家获胜时,如果还剩 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()

最佳答案

使用此模式:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com