Python script to generate simple "dungeon maps"
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

procgen.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import random as r
  2. def createDungeon(x=None, y=None, seed=None):
  3. """ Initializes an x by y grid.
  4. x is width, y is height
  5. seed is the chance that a given cell will be "live" and should be an integer between 1-99.
  6. If True is equivalent to "walkable", then lower seeds make more walls.
  7. """
  8. x = 10 if x == None else int(x)
  9. y = 10 if y == None else int(y)
  10. seed = 45 if seed == None else int(seed)
  11. new_map = []
  12. for j in range(y):
  13. new_row = []
  14. for i in range(x):
  15. new_row.append(False if r.randint(1,99) > seed else True)
  16. new_map.append(new_row)
  17. return new_map
  18. def refineDungeon(d_map, d_lmt=None, a_lmt=None):
  19. """ Refines the grid.
  20. """
  21. d_lmt = 3 if d_lmt == None else int(d_lmt)
  22. a_lmt = 4 if a_lmt == None else int(a_lmt)
  23. new_map = []
  24. for j in range(len(d_map)):
  25. new_line = []
  26. for i in range(len(d_map[j])):
  27. x, y = i, j
  28. n_count = countAliveNeighbors(d_map, x, y)
  29. if d_map[y][x]:
  30. if n_count <= d_lmt:
  31. new_line.append(False)
  32. else:
  33. new_line.append(True)
  34. else:
  35. if n_count >= a_lmt:
  36. new_line.append(True)
  37. else:
  38. new_line.append(False)
  39. new_map.append(new_line)
  40. return new_map
  41. def countAliveNeighbors(d_map, x, y):
  42. count = 0
  43. for j in range(-1,2):
  44. for i in range(-1,2):
  45. n_x, n_y = x+i, y+j
  46. if i == 0 and j == 0:
  47. continue
  48. if n_x < 0 or n_x >= len(d_map[j]) or n_y == 0 or n_y >= len(d_map):
  49. count += 1
  50. elif d_map[n_y][n_x]:
  51. count += 1
  52. return count
  53. def printDungeon(d_map, wall=None, path=None):
  54. wall = "II" if wall == None else wall
  55. path = " " if path == None else path
  56. for line in d_map:
  57. print("".join([wall if x == True else path for x in line]))
  58. print()
  59. def main():
  60. my_map = createDungeon(20,20)
  61. printDungeon(my_map)
  62. my_map = refineDungeon(my_map, 4, 4)
  63. printDungeon(my_map)
  64. my_map = refineDungeon(my_map, 4, 4)
  65. printDungeon(my_map)
  66. if __name__ == "__main__":
  67. main()