}
+def random_cardinal():
+ """Return a random cardinal direction for random walks."""
+ return random.choice([(0, 1), (0, -1), (1, 0), (-1, 0)])
+
+
class LevelGenerator:
width = 0
height = 0
for walk in range(random.randint(3, 6)):
x = width // 2 + random.randint(-8, 8)
y = height // 2 + random.randint(-8, 8)
- dir_x = random.randint(-1, 1)
- dir_y = random.randint(-1, 1)
+ dir_x, dir_y = random_cardinal()
max_steps = random.randint(40, width * height // 4)
for step in range(20, max_steps):
if 0 < x < width - 1:
if 0 < y < height - 1:
self.underlayer[y][x] = '-'
if random.random() > 0.7:
- dir_x = random.randint(-1, 1)
- dir_y = random.randint(-1, 1)
+ dir_x, dir_y = random_cardinal()
x += dir_x
y += dir_y