Skip to content

Commit e574720

Browse files
Procedural Dungeon Generator Script Added
1 parent 0941df0 commit e574720

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Procedural Dungeon Generator
2+
3+
This script generates random dungeon layouts for a text-based adventure game. It creates diverse dungeon layouts with various room types, connections, obstacles, enemies, treasures, and traps to enhance gameplay.
4+
5+
## Features
6+
7+
- Generates multiple dungeon levels.
8+
- Creates random room layouts with different sizes and shapes.
9+
- Adds corridors to connect rooms in the dungeon.
10+
- Places doors, enemies, treasures, and traps to add gameplay elements.
11+
- Provides flexibility for adding more room types, interactive elements, and features.
12+
13+
## Usage
14+
15+
1. Make sure you have Python (3.x) installed on your system.
16+
17+
2. Clone this repository and run the script
18+
19+
```bash
20+
python dungeon_generator.py
21+
```
22+
23+
3. The script will generate and display the dungeons with various features and levels.
24+
25+
## Customization
26+
27+
You can customize the script by adjusting parameters and adding more room types, enemies, treasures, traps, and other features. Feel free to explore the code and modify it according to your game's requirements.
28+
29+
## Contributing
30+
31+
Contributions are welcome! If you have any improvements, bug fixes, or new features to add, please create a pull request. For major changes, it's recommended to open an issue first to discuss your ideas.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import random
2+
3+
class DungeonGenerator:
4+
def __init__(self, width, height, num_levels):
5+
self.width = width
6+
self.height = height
7+
self.num_levels = num_levels
8+
self.dungeons = []
9+
10+
def generate(self):
11+
for _ in range(self.num_levels):
12+
dungeon = [[' ' for _ in range(self.width)] for _ in range(self.height)]
13+
self.add_rooms(dungeon)
14+
self.add_corridors(dungeon)
15+
self.add_doors(dungeon)
16+
self.add_enemies(dungeon)
17+
self.add_treasures(dungeon)
18+
self.add_traps(dungeon)
19+
self.add_stairs(dungeon)
20+
self.dungeons.append(dungeon)
21+
22+
def add_rooms(self, dungeon):
23+
num_rooms = random.randint(15, 30)
24+
for _ in range(num_rooms):
25+
room_width = random.randint(4, 12)
26+
room_height = random.randint(4, 10)
27+
x = random.randint(1, self.width - room_width - 1)
28+
y = random.randint(1, self.height - room_height - 1)
29+
self.create_room(dungeon, x, y, room_width, room_height)
30+
31+
def create_room(self, dungeon, x, y, width, height):
32+
for i in range(x, x + width):
33+
for j in range(y, y + height):
34+
dungeon[j][i] = '.'
35+
36+
def add_corridors(self, dungeon):
37+
for i in range(len(dungeon) - 1):
38+
for j in range(len(dungeon[0]) - 1):
39+
if dungeon[i][j] == '.' and dungeon[i+1][j] == '.':
40+
self.create_vertical_corridor(dungeon, i, j)
41+
if dungeon[i][j] == '.' and dungeon[i][j+1] == '.':
42+
self.create_horizontal_corridor(dungeon, i, j)
43+
44+
def create_vertical_corridor(self, dungeon, x, y):
45+
while y < len(dungeon[0]) - 1 and dungeon[x][y] != '#':
46+
dungeon[x][y] = '#'
47+
y += 1
48+
49+
def create_horizontal_corridor(self, dungeon, x, y):
50+
while x < len(dungeon) - 1 and dungeon[x][y] != '#':
51+
dungeon[x][y] = '#'
52+
x += 1
53+
54+
def add_doors(self, dungeon):
55+
for i in range(1, len(dungeon) - 1):
56+
for j in range(1, len(dungeon[0]) - 1):
57+
if dungeon[i][j] == '.' and random.random() < 0.02:
58+
dungeon[i][j] = '+'
59+
60+
def add_enemies(self, dungeon):
61+
num_enemies = random.randint(5, 15)
62+
enemy_types = ['Goblin', 'Skeleton', 'Orc', 'Spider']
63+
for _ in range(num_enemies):
64+
x = random.randint(1, self.width - 2)
65+
y = random.randint(1, self.height - 2)
66+
if dungeon[y][x] == '.':
67+
enemy_type = random.choice(enemy_types)
68+
dungeon[y][x] = 'E(' + enemy_type[0] + ')'
69+
70+
def add_treasures(self, dungeon):
71+
num_treasures = random.randint(5, 10)
72+
treasure_types = ['Gold', 'Gem', 'Artifact']
73+
for _ in range(num_treasures):
74+
x = random.randint(1, self.width - 2)
75+
y = random.randint(1, self.height - 2)
76+
if dungeon[y][x] == '.':
77+
treasure_type = random.choice(treasure_types)
78+
dungeon[y][x] = '$(' + treasure_type[0] + ')'
79+
80+
def add_traps(self, dungeon):
81+
num_traps = random.randint(5, 10)
82+
trap_types = ['Spikes', 'Poison', 'Fire']
83+
for _ in range(num_traps):
84+
x = random.randint(1, self.width - 2)
85+
y = random.randint(1, self.height - 2)
86+
if dungeon[y][x] == '.':
87+
trap_type = random.choice(trap_types)
88+
dungeon[y][x] = '^(' + trap_type[0] + ')'
89+
90+
def add_stairs(self, dungeon):
91+
for _ in range(2):
92+
x = random.randint(1, self.width - 2)
93+
y = random.randint(1, self.height - 2)
94+
if dungeon[y][x] == '.':
95+
dungeon[y][x] = '<' if _ == 0 else '>'
96+
97+
def print_dungeons(self):
98+
for level, dungeon in enumerate(self.dungeons, start=1):
99+
print(f"Level {level} Dungeon:")
100+
for row in dungeon:
101+
print(''.join(row))
102+
print()
103+
104+
# Create a dungeon generator and generate dungeons
105+
dungeon_generator = DungeonGenerator(80, 40, num_levels=3)
106+
dungeon_generator.generate()
107+
108+
# Print the generated dungeons
109+
dungeon_generator.print_dungeons()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random

0 commit comments

Comments
 (0)