将网格位置增加一个数字,随机分配在X&amp上; y那个数字

发布于 2025-01-21 23:25:32 字数 8 浏览 3 评论 0原文

continue

I have created an path finding algorithm visuliser using pygame, that shows A* Vs. Dijkstra's Algorith.

the start node,end node and obstacles are all randomly positioned as such

def generate_num(x, y):
    return randint(x, y)
....
....
 for x in range(0, density):
        row_pos = generate_num(0, rows - 1)
        col_pos = generate_num(0, rows - 1)
        node = grid[row_pos][col_pos]
        if not start and node != end:
            start = node
            start.makeStartNode()

        elif not end and node != start:
            end = node
            end.makeEndNode()

        elif node != end and node != start:
            node.makeObstacle()

in order to evaluate, my algorithms I am constraining the distance between the start and end node (for small, medium and large) and thus need to make the end node eg (10,30,50) grid blocks away.

however i do not want it to be evenly spread across grid[x][y] i would like it to be random.

so if distance is set to 50, grid[x][y] could be grid[17][33] or grid[10][40] or grid[25][25] but will always add up to being 50 nodes away

the makexxxNode()function simply changed the colour of the the node to identify its type visually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2025-01-28 23:25:32

it was as simple as what @hilberts_drinking_problem stated y = 50 - x as x is random number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文