猫史档案馆


【Python作品分享】Digital Pet【作品秀】

用户:展示账号展示账号查看:0 回复:0 评论:0 创建时间:2020-11-23T18:19:23


【作品展示】

center_image

 

【作品介绍】

Digital Pet

It is the mini python program which allows you to adopt your digital pet.

You can named your pet and play with it.

hp is the health point of your pet, you can add the value of it by feeding your pet, or deduct the value of it by fighting with masters.

score is the game point of this game, it will increases if you choose to fight.

once the hp <= 0, game over.

so try your besr to get higher scores!

 

【作品源代码】

'''Digital Pet'''

#get random integer
from random import randint

#basic info of your digital pet
name = input("Can you give a name to your digital pet? ") #get the name
hp = 10 #initial number of health points
score = 0 #initial number of total score
print("\nYour digital pet '"+name+"' wants to play with you! \n") #printing the welcome sentence based on the pet name

print("---------------------GAME RULES------------------------")
print("Feed: hp + 1 to 9 randomly.")
print("Fight: hp - 1 to 9 randomly, score + 1 to 10 randomly")
print("If the hp <= 0, game over.")
print("------------------------------------------------------- \n")

print("It currently has 10 hp and 0 score, try your best to get higher scores! \n")

#while loop
while True:
    text = input("feed(hp+) or fight(score+, hp-): ")
    #checking hp
    if text == "feed":
        hp = hp + randint(1,9) #adding random number of hp
        if hp >= 10: #checking if hp is 10
            hp = 10 #keep hp value is 10
            print("The maximum hp is 10. \n")
        print("You feeded your pet, it has hp "+str(hp) + "\n")
    elif text == "fight":
        score = score + randint(1,10) #adding random number of scores
        hp = hp - randint(1,9) #deducting random number of hp
        if hp <= 0: #checking if hp is 0
            print("hp <= 0, game over. \n")
            break  # quit the loop
        print("you have "+str(score)+" scores and your pet has hp "+str(hp) + "\n")
    else:
        print("Sorry, your pet does not understand. Try again!\n")
print("Your final score is "+str(score)+", and your pet has hp "+str(hp))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页