2017-03-09 33 views
-2

只是想要一个专业人士对此视图,提前感谢如果你编辑这个。 (如果你这样做)我想这段代码专注于同一主题(作为地下城游戏)。感谢您甚至花时间看这个!有人可以请我改进我的代码吗? (我是一个新手)

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!") 
+1

您遇到问题的代码是否存在特定问题?如果有什么特定的东西你不能弄清楚,你可以要求使用一个简短的代码来重现问题。正如目前所问,这太宽了 – Kewl

+4

尝试代码审查,如果没有具体问题 – depperm

+0

一些推荐:a)你不必将'输入'转换为字符串;它已经是和b)'backpack_stick = 0'这种跟踪项目的方法效率非常低,因为它创建了太多的变量。用dict代替'backpack = {'stick':0}'。 c)'randomint'返回一个整数。再次不需要转换。 d)'如果enmychn_rat == 1或2或3或4:'不会做你认为它的做法 –

回答

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

1.可以用下面的替代(注:如果你撒尿写的东西像“拒绝”说不,因为它有一个“Y”在这我的建议会失败)。编辑:有人使用input.lower()建议startsWith(“Y”),这是比我提出什么更好的,如果你键入“否”

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 
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物体,一个怪物物体,给他们添加生命,攻击点等等,制作关卡。

相关问题