接着昨天的工作,今天来看《Python游戏编程快速上手》的第五章----龙穴探险。
这一章我将不会给出书中的代码,我会直接给我我修改后的代码。我增加了一些游戏的规则,如下:
- 管理员(编程人员)可以调节龙穴的数量,也可以调节安全龙穴的数量,当然安全龙穴的数量必须不大于龙穴的数量,由于这个是由管理员设置的,所以无需加判断
代码还是非常简单的,因为还没有涉及UI,加上UI后会更有可玩性。
代码如下;
import random
import time
num = 3
safenum = 1
def storyBegin():
print("Now you are in a island full of dragons.")
print("In front of you,there are ", num, " caves.")
print("In ", safenum, " cave, the dragon is friendly and will share his treasure with you.")
print("The others dragon is greedy and hungry,and will eat you on sight.")
print()
def chooseCave():
print("Which one do you choose?(1 to ", num,")")
cave = int(input())
if cave > 0 and cave <= num:
return cave
else:
print("You can only choose in 1 to ", num)
chooseCave()
def checkCave(chooseC):
print("You approach the cave...")
time.sleep(2)
print("It is dark and spooky...")
time.sleep(2)
print("A large dragon jumps out in front of you!He opens his jaws and ...")
time.sleep(2)
print()
friendCave = random.sample(range(1, num), safenum)
if chooseC in friendCave:
print("Gives you his treasure!")
else:
print("Gobbles you down in one bite!")
if __name__ == "__main__":
while True:
storyBegin()
chooseC = chooseCave()
checkCave(chooseC)
print("Do you want to play again?(yes or no)")
againP = input()
if againP not in ["y", "yes"]:
break
加油吧,少年!