为了账号安全,请及时绑定邮箱和手机立即绑定

python学习-游戏

标签:
Python

# coding:utf-8

#!/usr/bin/python

'''

Created on Jan 4, 2018

@author: wayne

'''

import random #导入模块

import time

def displayIntro():         #定义一个函数,不执行代码,在调用的时候才执行

        print('you are in a land full of dragons.In front of you')

        print('you see two caves.In one cave,the dragon is friendly')

        print('and will share his treasure with you the other dragon')

        print('is greedy and hungry,and will eat you on sight.')

        print()

def chooseCave():       #定义一个玩家选择要进入的1或者2的函数

        cave = ''           #创建一个新的空字符串

        while cave != '1' and cave != '2':

                '''

                                                布尔操作符,and,or,not

                and 表示:表达式全为true为true,其中一个值为False或者全为False 表达式结果为False

                >>> True and True

                True

                >>>True and False

                False

                >>> False and False

                False

                or:只要其中一个值为True 那么结果就为True ,只有两个值都为False的时候才是False

                >>>True or False

                True

                >>> True or True

                True

                >>>False or False

                False

                not :只能作用一个值,不能两个值组合在一起,not是取值的反值

                >>> not True

                False

                >>> not False

                True

                >>> not (1 ==2)

                True

                '''        

                print('which cave will you go into?(1 or 2)')

                cave = input() #接收输入值,

                return cave

def checkCave(chosenCave):  #继承chosenCave的函数值

        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 .....')

        print()

        time.sleep(2)

        friendlyCave = random.randint(1,2)  #随机产生一个数字,在1,2中间

        if chosenCave == str(friendlyCave): #判断输入值和随机产生的值是否一致,一致则玩家获胜,否则失败

                print('Gives you his treasure!')

        else:

                print('Gobbles you down in one bite!')

playAgain = 'yes'       #存储一个初始值,"yes"

while playAgain == 'yes' or playAgain == 'y': #判断条件是否为True

        displayIntro() #调用函数

        caveNumber = chooseCave()   #调用输入值的函数

        checkCave(caveNumber) #调用checkCave的函数

        print('Do you want to play again!(yes or no)') #判断玩家是否还要玩

        playAgain = input() #接收键盘输入的值,如果是True则循环开始

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消