简体   繁体   English

我如何获得所有连接?

[英]How am I to get all the connections?

I am creating a platformer in pygame in which the levels are connected to eachother. 我在pygame中创建了一个平台,其中的关卡相互连接。 (one level is one screen, you move to the next level by walking off the screen). (一个级别是一个屏幕,您可以通过离开屏幕进入下一级别)。

I currently have it that it loads the connected level from it's file after walking offscreen, but this is obviously slow and as such I would like to pre-load all of the levels. 我目前拥有它,它可以在走出屏幕后从文件中加载连接的关卡,但这显然很慢,因此我想预加载所有关卡。 I would like to do this by taking one root level, getting all the levels it is connected to, getting all the levels each of those levels are connected to and so on until I have all of the levels. 为此,我想获得一个根级别,获取它连接的所有级别,获取每个级别所连接的所有级别,依此类推,直到获得所有级别为止。

I wrote this code to do so, but it does not work. 我编写此代码是为了这样做,但是它不起作用。 I wrote it when I was very tired. 我很累的时候就写了。 Can anyone help me with this? 谁能帮我这个? I'll answer any further questions if necessary. 如有必要,我将回答其他问题。

def loadLinkedLevels(level, surface, ignoredIds = []):
    levels = {}

    for levelId in level.warps.values():
        if levelId and levelId not in ignoredIds:
            levels[levelId] = LevelBuilder.loadLevel(levelId, surface)

    return levels

def getBranchingLevels(levels, p):
    newLevels = True # Do-while

    while newLevels:
        for level in levels.values():
            newLevels = loadLinkedLevels(level, p.screen, levels.keys())

        levels.update(newLevels)

        return levels

def preloadLevels(rootLevel, p):
    levels = loadLinkedLevels(rootLevel, p.screen)
    newLevels = {}

    for level in levels.values():
        newLevels.update(loadLinkedLevels(level, p.screen, levels.keys()))

    levels.update(newLevels)

    levels.update(getBranchingLevels(levels, p))

    return levels

The bug that stands out is here: 突出的错误在这里:

for level in levels.values():
    newLevels = loadLinkedLevels(level, p.screen, levels.keys())

levels.update(newLevels)

levels only gets updated with newLevels on the last time round the loop. 在循环的最后一次,仅使用newLevels更新levels (You would have been able to easily spot this if you had stepped through this code in the Python debugger .) (如果您在Python调试器中逐步执行了此代码,则可以轻松发现这一点。)

But in general your code seems way too complex. 但总的来说,您的代码似乎太复杂了。 You're trying to search the graph of levels starting at the root. 您正在尝试搜索从根开始的级别图。 So why not use a straightforward breadth-first search algorithm? 那么,为什么不使用简单的广度优先搜索算法呢? Like this: 像这样:

from collections import deque

def load_all_levels(root, p):
    """Load all levels reachable from `root`.
    Return a dictionary mapping level id to level.
    """
    # Queue of levels that have been loaded but whose neighbours have not.
    q = deque([root])
    # Map from level id to level for all levels loaded so far.
    loaded = {root.id: root}
    while q:
        for level_id in q.popleft().warps.values():
            if level_id not in loaded:
                level = LevelBuilder.loadLevel(level_id, p.screen)
                loaded[level_id] = level
                q.append(level)
    return loaded

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得小部件的所有连接? - How to get all connections of a widget? 在哪里可以获取图形API中所有连接的名称 - where can I get all names of connections in the graph API 如果我以 UTC +0 保存所有日期,如何在不同时区获得一致的数据 - How do I get consistent data across different timezones if I am saving all the dates in UTC +0 如何修复此 python 代码以获取我在代码中引用的表中的所有人员姓名? - How can I fix this python code to get all of the names of the people in the table I am referencing in the code? 当我得到所有值的“ nan”时,如何使用&或and操作获取正确的数据 - How to use & or and operation to get correct data when I am getting 'nan' for all values 我正在对 influenster.com 执行 web 刮擦。 我应该如何从所有 11 页而不是单页中获取评论 - I am performing web scraping on influenster.com. How am I supposed to get the reviews from all 11 pages rather than one single page 如何使用python获取通过计算机(linux)的所有网络连接 - How to get all the Network connections going through the computer (linux) using python 如何获取/抓取 Play 商店或应用商店上的所有应用评论,我只收到前 40 条评论? - How to get/crawl all the reviews of apps on play store or app store, I am just getting first 40 reviews? 如何关闭端口并尽快终止所有活动连接? - How can I close a port and also kill all active connections ASAP? 如何将 Delaunay 三角剖分用于 output 一个矩阵,其中包含三角剖分中的所有连接及其距离? - How can I use the Delaunay triangulation to output a matrix with all the connections in the triangulation and their distances?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM