Advertisement
Guest User

Untitled

a guest
Jun 11th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. from random import randint
  2.  
  3. class World_Map:
  4. def __init__(self, width, height):
  5. self.width = width
  6. self.height = height
  7. self.world_map = [['.' for i in range(width)]for i in range(height)]
  8. self.town_symbol = '#' # Used to represent a 'Town' when world_map is displayed
  9.  
  10. def display(self): # Displays map on screen
  11. for i in self.world_map:
  12. print(' '.join(i))
  13.  
  14. def place_towns(self, number_of_towns, padding): # Represented by '#' in world_map
  15.  
  16. for i in range(number_of_towns):
  17.  
  18. while 1:
  19. # Generate Random Integers (potential 'Town' location)
  20. x = randint(0,self.width - 1)
  21. y = randint(0,self.height - 1)
  22.  
  23. # Set min-max values of X and Y based on desired padding (free space around 'Towns')
  24. x_floor = x - padding
  25. x_ceiling = x + padding + 1
  26. y_floor = y - padding
  27. y_ceiling = y + padding + 1
  28.  
  29. # Keeps integers above zero to prevent pulling values from 'end of lists'
  30. if x_floor < 0: x_floor = 0
  31. if y_floor < 0: y_floor = 0
  32.  
  33. # Generates 2D array of 'rows' from world_map
  34. y_vals = self.world_map[y_floor:y_ceiling]
  35.  
  36. # Generates 2D array from y_vals of all values within specified 'padding' range of potential 'town location'
  37. x_vals = [row[x_floor:x_ceiling] for row in y_vals]
  38.  
  39. # Checks x_vals to determine if Town(#) already exists within 'range' of potential location
  40. if self.town_symbol in [i for i in x_vals]:
  41. pass
  42. else:
  43. self.world_map[y][x] = self.town_symbol
  44. break
  45.  
  46. M = World_Map(16,8) # Make 'World Map'
  47. M.place_towns(5, 1) # Make 'Towns'
  48.  
  49. M.display() # Show map
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement