Python script to generate simple "dungeon maps"
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import random as r
  2. import sys
  3. def createDungeon(x=None, y=None, seed=None):
  4. """ Initializes an x by y grid.
  5. x is width, y is height
  6. seed is the chance that a given cell will be "live" and should be an integer between 1-99.
  7. If True is equivalent to "walkable", then lower seeds make more walls.
  8. """
  9. x = 10 if x == None else int(x)
  10. y = 10 if y == None else int(y)
  11. seed = 45 if seed == None else int(seed)
  12. new_map = []
  13. for j in range(y):
  14. new_row = []
  15. for i in range(x):
  16. new_row.append(False if r.randint(1,99) > seed else True)
  17. new_map.append(new_row)
  18. return new_map
  19. def refineDungeon(d_map, d_lmt=None, a_lmt=None):
  20. """ Refines the grid.
  21. """
  22. d_lmt = 3 if d_lmt == None else int(d_lmt)
  23. a_lmt = 4 if a_lmt == None else int(a_lmt)
  24. new_map = []
  25. for j in range(len(d_map)):
  26. new_line = []
  27. for i in range(len(d_map[j])):
  28. x, y = i, j
  29. n_count = countAliveNeighbors(d_map, x, y)
  30. if d_map[y][x]:
  31. if n_count <= d_lmt:
  32. new_line.append(False)
  33. else:
  34. new_line.append(True)
  35. else:
  36. if n_count >= a_lmt:
  37. new_line.append(True)
  38. else:
  39. new_line.append(False)
  40. new_map.append(new_line)
  41. return new_map
  42. def countAliveNeighbors(d_map, x, y):
  43. count = 0
  44. for j in range(-1,2):
  45. for i in range(-1,2):
  46. n_x, n_y = x+i, y+j
  47. if i == 0 and j == 0:
  48. continue
  49. if n_x < 0 or n_x >= len(d_map[j]) or n_y == 0 or n_y >= len(d_map):
  50. count += 1
  51. elif d_map[n_y][n_x]:
  52. count += 1
  53. return count
  54. def printDungeon(d_map, wall=None, path=None):
  55. wall = "II" if wall == None else wall
  56. path = " " if path == None else path
  57. for line in d_map:
  58. print("".join([wall if x == True else path for x in line]))
  59. print()
  60. def main(x=None, y=None, seed=None, d_lmt=None, a_lmt=None):
  61. # Initialize
  62. x = 20 if x == None else int(x)
  63. y = 20 if y == None else int(y)
  64. seed = 45 if seed == None else int(seed)
  65. d_lmt = 4 if d_lmt == None else int(d_lmt)
  66. a_lmt = 4 if a_lmt == None else int(a_lmt)
  67. my_map = createDungeon(x,y,seed)
  68. printDungeon(my_map)
  69. my_map = refineDungeon(my_map, d_lmt, d_lmt)
  70. printDungeon(my_map)
  71. my_map = refineDungeon(my_map, d_lmt, d_lmt)
  72. printDungeon(my_map)
  73. if __name__ == "__main__":
  74. args = sys.argv
  75. try:
  76. x = args[1]
  77. except:
  78. x = None
  79. try:
  80. y = args[2]
  81. except:
  82. y = None
  83. try:
  84. seed = args[3]
  85. except:
  86. seed = None
  87. try:
  88. d_lmt = args[4]
  89. except:
  90. d_lmt = None
  91. try:
  92. a_lmt = args[5]
  93. except:
  94. a_lmt = None
  95. main(x, y, seed, d_lmt, a_lmt)