python入门小游戏——龙之境

本文提供了一个使用Python编写的入门级小游戏——龙之境。玩家需选择一个洞穴,程序会随机决定哪个洞穴有友好的龙。玩家需输入1或2选择洞穴,如果选择错误,龙会吃掉玩家。游戏结束后询问玩家是否愿意再玩一次。
摘要由CSDN通过智能技术生成

python入门小游戏——龙之境

  1. 本文使用python实现龙之境游戏小游戏,判断输入与生成的随机数是否一致来确定有没有通过游戏。熟悉python的一些函数的使用。
# Dragon Realm
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():
    cave = ''
    if cave != '1' and cave != '2':
        print('Which cave will you go into?(1 or 2)')
    
    # 接收输入的选项
    cave = input()
    return cave 

def checkCave(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)
    if chosenCave == str(friendlyCave):
        print('Gives you his treasure!')
    else:
        print('Gobbles you down in one bite!')

playAgain = 'yes'
if playAgain == 'yes' or playAgain == 'y':
    displayIntro()
    caveNumber = chooseCave()
    checkCave(caveNumber)
    print('Do you want to play again?(yes or no)')
    playAgain = input()
  1. 运行脚本
python dragon.py

You are in a land full of dragons. In front of you,
you see two caves. In one cave, the dragon is friendly
and will share his treasure with you. The other dragon
is greedy and hungry, and will eat you on sight.
Which cave will you go into?(1 or 2)
1
You approach the cave…
It is dark and spooky…
A large dragon jumps out in front of you! He opens his jaws and…
Gobbles you down in one bite!
Do you want to play again?(yes or no)
no

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐江小鱼

知识创造财富,余额还是小数

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值