有人可以帮我改进我的代码吗? (我是菜鸟)

时间:2017-03-09 16:21:39

标签: python

只想要专业人士查看此内容,如果您编辑此内容,请提前感谢。 (如果你这样做)我希望这段代码专注于同一主题(作为一个地下城游戏。)感谢您花时间去看看这个!

import time 
    import random

    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print("Text dungeon explorer!")
    print("COMMANDS:")
    print("inventory -- acceses your current inventory")
    print("hold 'item' -- hold selected item")
    print("attack -- attacks if enemy is approched")
    print("eat -- eats current item held")
    print("use -- uses item held")
    print("open -- opens any available chests in said room")
    print("pickup -- picks up any current items in said room")
    print("drop -- drops held item")
    print("throw -- throws held item")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

    time.sleep(3)

    print ("It's a dark in the dungeon, no food, no wepons. You don't even know where you are... There is an object on the floor, all you can make out of it is that it looks quite like a stick.")

    time.sleep(11)

    ch1 = str(input("Do you take it? [y/n]: "))

    if ch1 in ['y', 'Y', 'Yes', 'YES', 'yes']:
        print("You have taken the stick! A very odd feel to it.")
        time.sleep(2)
        backpack_stick = 1

    else:
          print ("You did not take the stick.")
          backpack_stick = 0

    print ("As you proceed further into the cave, you see a wooden door.")
    time.sleep(6)
    ch2 = str(input("Do you open the door? [y/n]"))

    enmysleep_rat = int(random.randomint(1, 10))
    enmychn_rat = int(random.randomint(1, 10))
    chstchnc = int(random.randomint(1, 10))
    if enmychn_rat == 1 or 2 or 3 or 4:
      enmychance_rat = True
      if enmychance_rat is True:
         print("Oh no! A musipial rat! You take a closer look if it is sleeping.")

    time.sleep(3)
             if enmychance_rat is True and
        enmysleep_rat = 1 or 2:

             print("The rat is sleeping! I could take the availible loot without fighting!")
    elif enmysleep_rat = 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10:
             print("The rat is not sleeping! Prepare for battle!")

2 个答案:

答案 0 :(得分:1)

if ch1 in ['y', 'Y', 'Yes', 'YES', 'yes']:

1。可以用以下内容替换(注意:如果你想写'nay'之类的东西,因为它中有'y',我的建议会失败)。编辑:其他人建议使用input.lower()。startsWith(“y”)这比我建议的要好,如果输入'nay'则不会失败

if 'y' in ch1.lower():

2。此外,

if enmychn_rat == 1 or 2 or 3 or 4:

可以替换为

if enmychn_rat in [1, 2, 3, 4]:

我很确定你原来的行不会按照你的意愿行事。它评估为

if enmychn_rat == 1 or True or True or True:

永远是真的。

if enmychance_rat is True

可以改为简单

if enmychance_rat

答案 1 :(得分:0)

小心,你不要测试门的ch2变量。尝试做一些定义。 做一些全面的案例,就像将来你会有越来越多的生物"像老鼠一样。 对于一个真实的"游戏的感觉,尝试制作随机事件,比如,如果你有一个"动作",就像开门,你接受它等等,你可以为它们指定事件。

制作更多生物,就像这样。

导入随机

def creature():
    creatures = ["rat","pig","parrot","monster"]
    life = random.randint(1,10)
    is_asleep_var = random.randint(1,6)
    if is_asleep_var == 1:
        is_asleep = "sleeping"
    else:
        is_asleep = "awake"
    print ("Oh a " + creatures[random.randint(0,len(creatures)-1)]+" with "+str(life)+" life, who is " + str(is_asleep))

while(1):

    creature()
    raw_input()

制作越来越多的活动,生物。事件可能触发生物。你应该调查对象。制作一个charachter对象,一个怪物对象,为它们增加生命,攻击点等,制作关卡。

相关问题