首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有更好的方法来编码这个文字冒险游戏?

有没有更好的方法来编码这个文字冒险游戏?
EN

Code Review用户
提问于 2013-04-17 20:46:55
回答 2查看 4K关注 0票数 3

这是我第一次尝试一个基本的文字冒险游戏,我想知道是否有办法使它更有效率(或任何其他东西,我可以添加,以使游戏更好)。它还没有完全完成,但如果有办法使它更好,请张贴在评论。

代码语言:javascript
复制
#lets me use the exit function
import sys
#Lets me use the time function
import time
#Want to play loop

    #asks for name
name = raw_input('What is your name, adventurer? ')
print 'Nice to meet you, '+name+'. Are you ready for your adventure?'
while True:
    ready = raw_input('y/n ')
if ready == 'y':
    print 'Good, let us start our great adventure!'
    break
elif ready == 'n':
    print 'That is a real shame...' 
    time.sleep(1)
    print 'Exiting program in 5 seconds:'
    time.sleep(1)
    print '5'
    time.sleep(1)
    print '4'
    time.sleep(1)
    print '3'
    time.sleep(1)
    print '2'
    time.sleep(1)
    print '1'
    time.sleep(1)
    sys.exit('Exiting Game...')
    break
else:
    print 'That is not a valid answer... Try again'
    time.sleep(2)

#First level loop
while True:
print 'You awaken on the ground of a great forest. You can see two possible things to         do, enter the cabin to your left, or wander through the woods.'
print ' '

print 'Where do you want to go?'
FirstDecision = raw_input('cabin/woods ')
    #Cabin option
if FirstDecision == 'cabin':
    print 'You approach the door to the cabin, luckily it is unlocked.'
    Cnt = raw_input('Enter < to continue')
    print 'You open the door and walk through, only to hear a dry and shaky voice say:'
    print '\"Get out.\"'
    Cnt = raw_input('Enter < to continue')
    print 'What do you do?'
    FirstCabin = raw_input('leave/fight ')
    if FirstCabin == 'leave':
        print 'As you run out the door, the voice shoots and kills you.'
        Cnt = raw_input('Enter < to continue')
        print ' '
        FirstRetry = raw_input('Try again? (y/n)')
        if FirstRetry == 'y':
            print 'Restarting back at checkpoint...'
            time.sleep(2)
        elif FirstRetry == 'n':
            print 'That\'s a shame...'
            time.sleep(1)
            print 'Exiting program in 5 seconds:'
            time.sleep(1)
            print '5'
            time.sleep(1)
            print '4'
            time.sleep(1)
            print '3'
            time.sleep(1)
            print '2'
            time.sleep(1)
            print '1'
            time.sleep(1)
            sys.exit('Exiting Game...')
            break
    elif FirstCabin == 'fight':
        print 'You turn to where the voice came from and you bluntly say \"No.\"``
EN

回答 2

Code Review用户

发布于 2013-04-18 00:02:12

几件事。首先,您应该抽象出退出游戏的函数:

代码语言:javascript
复制
def exitGame():
    print 'That\'s a shame...'
    print 'Exiting program in 5 seconds:'
    for i in range(5):
        time.sleep(1)
        print(i+1)
    sys.exit('Exiting Game...')

其次,我可能会将小屋、重试等推到数组中。

代码语言:javascript
复制
DECISIONS = []
CABINS = []
CONTINUES = []

第三,我将第一级定义为一个函数,这样您就可以优雅地退出:

代码语言:javascript
复制
EXIT_MESSAGE = 'Exiting Game...'
#First level loop
while True:
    try: firstLevel()
    except SystemExit as e:
        print(str(e))
        break

最后一件事。使用打印作为函数而不是语句。也许这是个人偏好,但我发现字符串格式非常可取:

代码语言:javascript
复制
print('Nice to meet you, %s. Are you ready for your adventure?' % name)
票数 3
EN

Code Review用户

发布于 2013-08-30 00:32:33

在我看来,你代码的第38行有158个字符长:

代码语言:javascript
复制
print 'You awaken on the ground of a great forest. You can see two possible things to         do, enter the cabin to your left, or wander through the woods.'

以下是PEP 8中“最大线长”一节的前两句:

限制所有行最多79个字符。对于结构限制较少的长文本块(docstring或注释),行长度应限制为72个字符。

若要将第38行拆分为源中的较短行,可以将其存储在括号内的变量中:

代码语言:javascript
复制
s1 = ('You awaken on the ground of a great forest. '
      'You can see two possible things to do, enter '
      'the cabin to your left, or wander through the woods.')

请注意,有了上面的内容,输出仍然是一行连续的文本,没有换行。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/25194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档