Add additional securities when building map
This commit is contained in:
parent
fdca88ae20
commit
fcd20b3cdc
1 changed files with 29 additions and 7 deletions
36
utils.py
36
utils.py
|
@ -36,7 +36,16 @@ def fetch_rooms(path):
|
||||||
|
|
||||||
return assembly,background
|
return assembly,background
|
||||||
|
|
||||||
|
def check_map(carte):
|
||||||
|
"""Function to check if a map is valid i.e. if no two rooms are at the same location
|
||||||
|
"""
|
||||||
|
positions = []
|
||||||
|
for room in carte:
|
||||||
|
if room[2] in positions:
|
||||||
|
return(False)
|
||||||
|
else:
|
||||||
|
positions.append(room[2])
|
||||||
|
return(True)
|
||||||
|
|
||||||
def map_generator(n):
|
def map_generator(n):
|
||||||
"""Map generator generate a map with a main path of n rooms
|
"""Map generator generate a map with a main path of n rooms
|
||||||
|
@ -53,6 +62,7 @@ def map_generator(n):
|
||||||
start_label,start_room = random.choice(assembly[start_key])
|
start_label,start_room = random.choice(assembly[start_key])
|
||||||
|
|
||||||
# Place the first room in the list
|
# Place the first room in the list
|
||||||
|
positions = []
|
||||||
position = [0,0]
|
position = [0,0]
|
||||||
asset = pygame.image.load(start_room).convert_alpha()
|
asset = pygame.image.load(start_room).convert_alpha()
|
||||||
mask = pygame.mask.from_surface(asset)
|
mask = pygame.mask.from_surface(asset)
|
||||||
|
@ -63,7 +73,9 @@ def map_generator(n):
|
||||||
start_pos = start_poss[dir_from]
|
start_pos = start_poss[dir_from]
|
||||||
current_label = start_label
|
current_label = start_label
|
||||||
|
|
||||||
for i in range(n):
|
counter = 0
|
||||||
|
while counter < n:
|
||||||
|
|
||||||
# select the next direction, can't be the direction of arrival
|
# select the next direction, can't be the direction of arrival
|
||||||
dir_next = random.choice(current_label.replace(dir_from,''))
|
dir_next = random.choice(current_label.replace(dir_from,''))
|
||||||
|
|
||||||
|
@ -89,15 +101,25 @@ def map_generator(n):
|
||||||
next_position = [position[0]-room_side,position[1]]
|
next_position = [position[0]-room_side,position[1]]
|
||||||
dir_from = 'R'
|
dir_from = 'R'
|
||||||
|
|
||||||
# Build the map
|
# Check if we are not overwriting an existing room and store the current one.
|
||||||
carte.append((asset,mask,next_position))
|
if next_position not in positions:
|
||||||
|
counter += 1
|
||||||
|
# Build the map
|
||||||
|
carte.append((asset,mask,next_position))
|
||||||
|
positions.append(position)
|
||||||
|
|
||||||
# Update the label for next turn
|
# Update the variables for next turn
|
||||||
current_label = next_label
|
current_label = next_label
|
||||||
position = next_position
|
position = next_position
|
||||||
|
positions.append(position)
|
||||||
|
|
||||||
background = pygame.image.load(back_path).convert()
|
background = pygame.image.load(back_path).convert()
|
||||||
|
|
||||||
|
if not check_map(carte):
|
||||||
|
print("Invalid Map.")
|
||||||
|
else:
|
||||||
|
print("Map checked and valid!")
|
||||||
|
|
||||||
return(carte,start_pos,background)
|
return(carte,start_pos,background)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue