Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于文本的游戏太慢了_Python - Fatal编程技术网

Python 基于文本的游戏太慢了

Python 基于文本的游戏太慢了,python,Python,从事python脚本入门课程的项目。该项目是建立一个游戏,从他们提供的一些基本代码开始,创建一个冒险,带你穿过几个房间,沿途收集物品。我已经能够满足大多数需求,但是在游戏中添加了库存系统之后,它的处理速度变得非常缓慢,并且也造成了游戏进展的问题。我毫不怀疑代码效率低下,可以重新组织代码,也可以使用不同的循环 在使库存系统按要求运行之前,它将接受用户输入的方向,告诉您房间是否正确,如果房间中有物品,您将键入移动,如果正确,则移动到下一个房间。现在,当游戏开始时,它会显示你在正确的房间里没有物品(如

从事python脚本入门课程的项目。该项目是建立一个游戏,从他们提供的一些基本代码开始,创建一个冒险,带你穿过几个房间,沿途收集物品。我已经能够满足大多数需求,但是在游戏中添加了库存系统之后,它的处理速度变得非常缓慢,并且也造成了游戏进展的问题。我毫不怀疑代码效率低下,可以重新组织代码,也可以使用不同的循环

在使库存系统按要求运行之前,它将接受用户输入的方向,告诉您房间是否正确,如果房间中有物品,您将键入移动,如果正确,则移动到下一个房间。现在,当游戏开始时,它会显示你在正确的房间里没有物品(如预期的那样),你键入一个移动,现在处理下一个移动的速度非常慢。然后,它会显示项目和提示,然后再向您展示房间中的物品清单、它应该在房间中向您展示的位置、有项目以及您的物品清单

如果有人能指出我做错了什么,我将不胜感激

import time
# A dictionary for the Server room text game
# The dictionary links a room to other rooms.
rooms = {
        'IT Office': {'South': 'Corporate Office'}, # First Room
        'Corporate Office': {'North': 'IT Office', 'South': 'Cafeteria', 'East': 'Supply Closet', 'item': 'fix it guide'},
        'Supply Closet': {'West': 'Corporate Office', 'item': 'plumbers tape'},
        'Cafeteria': {'North': 'Corporate Office', 'West': 'Break Room', 'South': 'Maintenance Facility', 'item': 'poncho'},
        'Break Room': {'East': 'Cafeteria', 'item': 'rain boots'},
        'Maintenance Facility': {'North': 'Cafeteria', 'East': 'Equipment Shed', 'item': 'pipe wrench'},
        'Equipment Shed': {'West': 'Maintenance Facility', 'North': 'Server Room', 'item': 'section of pipe'},
        'Server Room': {'South': 'Equipment Shed'} #Last Room
}

#defining the game instructions
def introduction():
    print('')
    print('********** Server Room Text Adventure Game **********\n')
    time.sleep(1.0)
    print('*****************************************************')
    print('\nThere are 8 rooms to move between and 6 items to pick up\n')
    print('that you will require to complete your adventure.\n')
    print('Directions: You can go North, South, East, or West\n')
    print('to navigate between rooms. If the room has an item\n')
    print('it will be picked up and added to your inventory.\n')
    print('You can also type exit to leave the game. Good Luck!\n')
    print('*****************************************************\n\n')
    time.sleep(2.0)

# Defining player status, prints
def player_status():
    print('=========================================')
    print('\nYou are currently in the {}'.format(currentRoom))
    print('\nYour current inventory: {}'.format(inventory))
    #print('You see a {}'.format(currentItem))
    print('\n=========================================')
    print('\n')

# Inventory list to hold inventory as it's added in each room
inventory = []

user_item = ''
# defining Inventory carried to add to inventory list
def game(item):
    #user_item = input('')

    if item in inventory:  # use in operator to check membership
        print("you already have got this")
        print(" ".join(inventory))

    elif item not in inventory:
        print('You see a', item)
        print('Would you like to pick it up? \n')
        user_item = input('')
        if user_item == 'Y':
            print('item has been added\n')
            inventory.append(item)
        else:
            print('You leave the item behind.')
    #else:
        #print("You see a", item)
        #print("and you pick it up, its been added to inventory")
        #inventory.append(item)
        #print(" ".join(inventory))


# Current Room, starts off in IT Office
currentRoom = 'IT Office'

# Players Move
player_move = ''

# Calling the Introduction function
introduction()

# While Loop for moving between rooms
while True:
    player_status()

    player_move = input('Enter a move: \n')

    if player_move == 'Exit':
        print('Thanks for playing.')
        # break statement for exiting
        break

    if player_move in rooms[currentRoom]:
        currentRoom = rooms[currentRoom][player_move]

        addItem = input(' \n')

        if 'item' in rooms[currentRoom]:
            game(rooms[currentRoom]['item'])

        #if/else statements with break statement for final room/finishing the game
        if currentRoom == 'Server Room':
            if len(inventory) == 6:
                print('\n***********************     Congratulations!     ***********************')
                time.sleep(1.0)
                print('\n************************************************************************')
                print('\nYou have made it to the Server Room with all of the items to ')
                print('fix the burst pipe. You used the items you collected to repair')
                print('the pipe, and Saved the Data! You are a Hero to your company!\n')
                print('*************************************************************************\n')
                time.sleep(2.0)
                print('^^^^^^^^^^^^^^^^^^^^^^^^^^ Thanks for playing! ^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n')
                break

            else:
                print('\n################## You Failed! #####################')
                time.sleep(1.0)
                print('\n####################################################')
                print('\nYou are missing items needed to fix the pipe')
                print('& the Server Room has flooded. All of your')
                print('companies data is gone. Time to polish your resume.')
                print('\n####################################################\n')
                print('@@@@@@@@@@@@@@@@@@@ Please play again @@@@@@@@@@@@@@@@')
                break

    # else statement for input validation/invalid move
    else:
        print('\nYou walk in to a wall, try again.\n')
前两项建议:

我强烈建议您在遇到类似问题时,在调试器中逐步完成代码。我怀疑如果您这样做了,您会很快看到这个问题,因为您会进入问题代码行并意识到发生了什么。或者至少,你可以把问题留给其他人,并按照建议产生一个较小的MRE来产生问题

同样,在这样的情况下,如果您有代码可以减慢速度,并且不会改变结果,那么可以方便地将其“剔除”。例如,如果将对
time.sleep
的所有调用替换为对以下函数的调用:

REALLY_SLEEP = False
def sleep(value):
    if REALLY_SLEEP:
        time.sleep(value)
    else:
        import inspect
        print(f"**** Sleep called for {value}s at {inspect.stack()[1].lineno} ****")
然后,您可以在测试和开发程序时快速运行程序,并在您准备好让其他人玩时将
REALLY\u SLEEP
标志设置为True

所有这些都表明:当你搬进另一个房间时,你不会花很长时间,你会花很长时间:

    if player_move in rooms[currentRoom]:
        currentRoom = rooms[currentRoom][player_move]

        addItem = input(' \n')

这段代码检查用户是否输入了有效的移动,如果输入了,则移动到新房间,并等待用户输入,而用户无法检测到任何内容作为输入提示
addItem
没有使用,所以我假设它是早期迭代中被遗忘的随机代码。摆脱那条线,事情就会好起来


这又回到了我的开场白:无论是否在IDE中学习使用调试器,单步执行代码都会立即显示它在做什么。

你经常使用
time.sleep
,可能它在不应该输入的地方输入了条件。这里有一个额外的输入:
addItem=input('\n'))
您有时似乎已经进入了
游戏
功能(您可能想调用该功能,例如
拾取项目
来描述它的实际功能)。由于额外的
输入
,您的游戏将出现延迟,直到您按额外的enter键。请提供预期的时间。显示中间结果与预期结果的偏差。我们应该能够将单个代码块粘贴到文件中,运行它,并重现您的问题。这也让我们可以在您的上下文中测试任何建议。正如@MatsLindh所说,您的
addItem
输入没有提示字符串,因此当您玩游戏时,您不知道要输入什么。您应该添加一个提示字符串,说“您想拾取什么?”或类似的内容。您现在不能使用该
addItem
值执行任何操作。也许你应该把这句话注释掉,看看你的游戏在没有暂停输入的情况下表现如何。-除了调用
sleep()
,您在游戏中没有做任何需要花费时间的事情。如果你认为你的游戏进展缓慢,它要么在等待输入,要么在休眠。这篇文章似乎是一个完整的代码转储,而不是你的MRE。我们还希望您能够跟踪问题并打印有用的值。你对他们是如何获得这些价值观感到困惑吗?您发布的程序挂起等待输入。不要期望我们提供测试数据:只需用引发问题的测试用例替换您的
输入
。“处理缓慢”和“产生问题”是含糊不清的陈述,不是问题说明。谢谢,你也是对的,删除那个附加陈述似乎解决了我的问题。谢谢你的帮助。