《Python游戏编程快速上手》第五章--龙穴探险

接着昨天的工作,今天来看《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

加油吧,少年!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值