-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.py
More file actions
52 lines (46 loc) · 1.79 KB
/
world.py
File metadata and controls
52 lines (46 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import random
from monsters import Enemy
from items import Treasure
class Dungeon:
def __init__(self, width, height):
self.width = width
self.height = height
self.map = [[0 for _ in range(width)] for _ in range(height)]
self.generate_dungeon()
self.enemies = []
self.treasures = []
self.generate_entities()
def generate_dungeon(self):
# Create a simple grid with walls around the edges
for y in range(self.height):
for x in range(self.width):
if x == 0 or y == 0 or x == self.width - 1 or y == self.height - 1:
self.map[y][x] = 1 # Wall
else:
# Randomly generate some inner walls
if random.random() < 0.15:
self.map[y][x] = 1 # Wall
# Ensure player start position is clear
self.map[1][1] = 0
self.map[1][2] = 0
self.map[2][1] = 0
def generate_entities(self):
# Generate enemies
for _ in range(15):
while True:
x = random.randint(1, self.width - 2)
y = random.randint(1, self.height - 2)
if self.map[y][x] == 0:
enemy_type = random.choice(["goblin", "orc", "dragon"])
self.enemies.append(Enemy(x, y, enemy_type))
break
# Generate treasures
for _ in range(10):
while True:
x = random.randint(1, self.width - 2)
y = random.randint(1, self.height - 2)
if self.map[y][x] == 0:
treasure_type = random.choice(["gold", "health", "sword"])
self.treasures.append(Treasure(x, y, treasure_type))
break