2015-12-14 76 views
-1

我在制作文字冒险游戏。 我很困惑如何实现我的游戏的下一部分。 这里是我的旁边有加: “添加到捡起一个物体的能力,如果用户键入获取密钥,则:如何在Python中使用Split命令?

  1. 斯普利特用户输入,所以你拆出来,只是有一个变量等于以“钥匙”

    1,采用的是内置到Python字符串分割方法,例如:。 command_words = user_command.split(”“)

    这将什么类型的用户分成列表中的每个。项目根据空格分隔。

    1. 更新您的指示,以改为检查command_words [0]。
    2. 添加对get命令的检查。

2.Search列表,直到找到适合什么用户试图捡起一个物体。

这是我的代码至今:

done = False 
object_list = [] 
room_list = [] 

class Object(): 
    def __init__(self, name, description, room): 
     self.name = name 
     self.description = description 
     self.current_room = room 

wand = Object("Holly Wand", "This wand is an 11\" supple wand made of holly and a single phoenix tail feather core.", 1) 

object_list.append(wand) 

dh_cloak = Object("Cloak of Invisibility", "This is part one part of the most powerful trifecta. \nThis cloak shields you from all wandering eyes. \nFind all three parts and you may just be able to escape from this castle.", 3) 

object_list.append(dh_cloak) 

class Room(): 
    def __init__(self, describe, nw, n, ne, e, se, s, sw, w): 
     self.description = describe 
     self.northwest = nw 
     self.north = n 
     self.northeast = ne 
     self.east = e 
     self.southeast = se 
     self.south = s 
     self.southwest = sw 
     self.west = w 

kitchen = Room("You are in the Kitchen. Look at the scrumptious roast chicken and kidney pudding! \nThere are doors leading to North, East, and West.", None, 4, None, 2, None, None, None, 0) 

room_list.append(kitchen) 

east_cooridor = Room("You apparated into the East Corridor. \nYou can apparate to the Northwest or the Southwest.", 8, None, None, None, None, None, 2, None) 

room_list.append(east_cooridor) 

great_hall = Room("You are in the Great Hall. What is that great smell? \nThere appears to be doors leading to the north and the south.", None, 7, None, None, None, 1, None, None) 

room_list.append(great_hall) 

owlery = Room("You are in the Owlery. WHOOs got mail? There is a glass door overlooking the Forbidden Forest. \nThere are doors in every direction.", None, 9, None, 8, None, 4, None, 6) 

room_list.append(owlery) 

room_list.append(forbidden_forest) 

current_room = 4 
while not done: 
    print(room_list[current_room].description) 

key = current_room 
i = 0 
while i < len(object_list) and object_list[i].current_room != key: 
    i += 1 

if i < len(object_list): 
    print("There is an object you can pick up in this room.") 
    print() 

direction = input("Which direction would you like to travel? ") 
print() 

if direction.lower() == "n" and current_room == 9 or direction.lower() == "north" and current_room == 9: 
    print("You wandered too far into the Forbidden Forest with out all of the Deathly Hallows to protect you. \nYou have died.") 
    done = True 

elif direction.lower() == "nw" or direction.lower() == "northwest": 
    next_room = room_list[current_room].northwest 
    if next_room == None: 
     print("TROLL! Troll in the dungeon!!") 
    else: 
     current_room = next_room 
    print() 

elif direction.lower() == "n" or direction.lower() == "north": 
    next_room = room_list[current_room].north 
    if next_room == None: 
     print("Run away!!!!") 
    else: 
     current_room = next_room 
    print() 

elif direction.lower() == "ne" or direction.lower() == "northeast": 
    next_room = room_list[current_room].northeast 
    if next_room == None: 
     print("Oh cool! Pixies! Wait...yikes! Bad idea!") 
    else: 
     current_room = next_room 
    print()  

elif direction.lower() == "e" or direction.lower() == "east": 
    next_room = room_list[current_room].east 
    if next_room == None: 
     print("Don't go over there! The Whomping Willow is over there!") 
    else: 
     current_room = next_room 
    print() 

elif direction.lower() == "se" or direction.lower() == "southeast": 
    next_room = room_list[current_room].southeast 
    if next_room == None: 
     print("Don't go in there...") 
    else: 
     current_room = next_room 
    print() 

elif direction.lower() == "s" or direction.lower() == "south": 
    next_room = room_list[current_room].south 
    if next_room == None: 
     print("AHHH! It's Fluffy, the three-headed dog!") 
    else: 
     current_room = next_room 
    print() 

elif direction.lower() == "sw" or direction.lower() == "southwest": 
    next_room = room_list[current_room].southwest 
    if next_room == None: 
     print("The third floor corridor is forbidden.") 
    else: 
     current_room = next_room 
    print() 

elif direction.lower() == "w" or direction.lower() == "west": 
    next_room = room_list[current_room].west 
    if next_room == None: 
     print("I wouldn't go that way if I were you. You may run into something dangerous!") 
    else: 
     current_room = next_room 
    print() 

elif direction.lower() == "q" or direction.lower() == quit: 
    done = True 

else: 
    print("What kind of sorcery is this!? Try going an actual direction.") 
    print() 

我一直奉行给我的方向,但我们没有教有关拆分命令,我无法找到任何解释它很好的在线。
我希望有人能够向我解释我如何使用split命令将用户键入的内容“拆分”为列表。我不太明白如何或为什么我会这样做。 任何建议从哪里去这里将不胜感激。谢谢!

+1

如果您发布一个简洁的问题,您更有可能获得帮助。你可以很容易地删除一些法术或房间,它会让你更容易理解你的代码。 – Jaco

+0

添加引起问题的简明代码部分也会有所帮助。 – Learner

+0

请优化您的问题。 –

回答

0

split方法经常用于解析用户的命令,其中包含用空格或标点符号分隔的单词。例如...

brief_directions = ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw'] 
full_directions = ['north', 'south', 'east', 'west', 'northeast', 
        'northwest', 'southeast', 'southwest'] 
user_command = input('What would you like to do? ') 
if user_command in brief_directions + full_directions: 
    print('You want to go {}'.format(user_command)) 
else: 
    command_words = user_command.split() 
    print('You want to {verb} the {noun}'.format(verb=command_words[0], 
               noun=command_words[1])) 

你想怎么办?拿到钥匙

你想拿到钥匙

然后你可以写这就是所谓的每个用户输入“得到”的第一个字,例如时间单一功能

if command_words[0] == "get": 
    get_object(command_words[1]) 

这使您无需为用户可能想要得到的每个对象编写单独的一段代码。

+0

谢谢!这绝对有帮助。对不起,以前的所有困惑! – pibbles97

0

在努力解释split给你,在这里我去...

在Python中,split函数作为一个字符串操作器,允许程序员或用户定义其中一个分隔符字符串被切碎,分裂。当函数在字符串上执行时,字符串将转换为原始的后续字符串列表,这些字符串在每次出现的定义的分隔符处进行分隔和索引。

例如,假设我们有一个字符串"The cow jumped over the moon.",并且我们希望切断有空格的字符串。为了做到这一点,我们使用split函数。

s = "The cow jumped over the moon" 
print(s.split(' ')) 

在这个部分中,我将分隔符定义为space。代码段的结果将打印:['The', 'cow', 'jumped', 'over', 'the', 'moon']。正如你所看到的,它返回了原始的所有子串的列表,分割了哪里有空间。

该原则也适用于给split函数的所有参数。如果我们决定在信e的不断发生分裂,我们这样做:

s.split('e') 

这将返回:['Th', ' cow jump', 'd ov', 'r th', ' moon']

我希望这有助于!