roguepy/testing/jungle_generator.py

96 lines
3.1 KiB
Python
Raw Normal View History

2023-03-20 17:50:40 +00:00
import random
def generate(width, height, edgeRows, edgeCols, seed=None):
grid = []
for row in range(height):
grid.append([])
for tile in range(width):
grid[row].append('#')
#grid = [['#'] * width] * height
edges = set()
for i in range(edgeCols):
for j in range(edgeRows):
x = i * (width // edgeCols)
y = j * (height // edgeRows)
edges.add((x,y))
not_connected = set(edges)
connected = set()
connected.add(next(iter(not_connected)))
while len(not_connected) > 0:
(dx, dy) = not_connected.pop()
(ox, oy) = next(iter(connected))
connected.add((dx, dy))
directions = ['N', 'S', 'W', 'E']
direction = random.choice(directions)
valid_direction = False
while not valid_direction:
direction = random.choice(directions)
if direction == 'N':
valid_direction = oy >= dy
if direction == 'S':
valid_direction = oy <= dy
if direction == 'W':
valid_direction = ox >= dx
if direction == 'E':
valid_direction = ox <= dx
if direction == 'N':
for y in range(dy, oy+1):
grid[y][ox] = '.'
(minX, maxX) = (ox, dx) if ox < dx else (dx, ox)
for x in range(minX, maxX):
grid[dy][x] = '.'
if direction == 'S':
for y in range(oy, dy+1):
grid[y][ox] = '.'
(minX, maxX) = (ox, dx) if ox < dx else (dx, ox)
for x in range(minX, maxX):
grid[dy][x] = '.'
if direction == 'W':
for x in range(dx, ox+1):
grid[oy][x] = '.'
(minY, maxY) = (oy, dy) if oy < dy else (dy, oy)
for y in range(minY, maxY):
grid[y][dx] = '.'
if direction == 'E':
for x in range(ox, dx+1):
grid[oy][x] = '.'
(minY, maxY) = (oy, dy) if oy < dy else (dy, oy)
for y in range(minY, maxY):
grid[y][dx] = '.'
for (x, y) in edges:
grid[y][x] = '.'
for row in grid:
for tile in row:
print(tile, end='')
print()
generate(80, 20, 6, 8)
"""
Generate points at random positions
Pick point X and another random point without connections.
"""
"""
################################################################
################################################################
#############################......#############################
###########################...####.#############################
#################################....#####......################
##############################..#.##...##.......################
###############################...####....#.....################
################################################################
################################################################
################################################################
################################################################
"""