简体   繁体   中英

random generation of levels using 2D arrays Python

I want to do random generation of levels using 2D arrays. 0 = emptiness, 1 = wall, how generate levels which have a passable route from a starting point to a finish(yellow circle, appears in a random free spot on the map) location?

pole = [[0] * 20 for i in range(20)]
for i in range(20):
    for j in range(20):
        pole[i][j] = random.randint(0, 1)

At the moment it just randomizes the ones and zeros and it comes out: 在此处输入图像描述

The best way to do this is to generate a random map just like you do now and them check with an algorithm if there is an open route between start and finish. If not, just generate a new map and check again.

A "flood fill" algorithm where you start filling from start and see if the fill reaches the finish spot could work. For algorithm help, see:

Need help implementing flood-fill algorithm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM