ESC/POS Thermal Printer Server
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

146 řádky
4.6 KiB

  1. #!/usr/bin/python
  2. # import modules used here -- sys is a very standard one
  3. import sys
  4. from PIL import Image, ImageDraw
  5. import math
  6. import random
  7. class Cell:
  8. walls = int('1111', 2)
  9. visited = False
  10. def __init__(self, x, y):
  11. self.x = x
  12. self.y = y
  13. def draw(self, draw):
  14. global start, end, margin
  15. if self.walls & 1 == 1:
  16. draw.line([(margin + cellSize * self.x, margin + cellSize * self.y + cellSize), (margin + cellSize * self.x + cellSize, margin + cellSize * self.y + cellSize)], fill="black", width=3)
  17. if (self.walls >> 1 & 1) == 1:
  18. draw.line([(margin + cellSize * self.x + cellSize, margin + cellSize * self.y), (margin + cellSize * self.x + cellSize, margin + cellSize * self.y + cellSize)], fill="black", width=3)
  19. if (self.walls >> 2 & 1) == 1:
  20. draw.line([(margin + cellSize * self.x, margin + cellSize * self.y), (margin + cellSize * self.x + cellSize, margin + cellSize * self.y)], fill="black", width=3)
  21. if (self.walls >> 3 & 1) == 1:
  22. draw.line([(margin + cellSize * self.x, margin + cellSize * self.y), (margin + cellSize * self.x, margin + cellSize * self.y + cellSize)], fill="black", width=3)
  23. if self.x == start[0] and self.y == start[1]:
  24. draw.ellipse([margin + cellSize * self.x + cellSize / 4, margin + cellSize * self.y + cellSize / 4, margin + cellSize * self.x + cellSize / 4 + cellSize / 2, margin + cellSize * self.y + cellSize / 4 + cellSize / 2], fill="blue")
  25. elif self.x == end[0] and self.y == end[1]:
  26. draw.ellipse([margin + cellSize * self.x + cellSize / 4, margin + cellSize * self.y + cellSize / 4, margin + cellSize * self.x + cellSize / 4 + cellSize / 2, margin + cellSize * self.y + cellSize / 4 + cellSize / 2], fill="green")
  27. rows = 0
  28. cols = 0
  29. #image = Image.new('RGB', (380, 400 * 2), (0, 0, 0))
  30. image = Image.new('RGB', (500, 400 * 4), (0, 0, 0))
  31. draw = ImageDraw.Draw(image)
  32. cellSize = 20
  33. margin = 10
  34. matrix = []
  35. stack = []
  36. currentCell = None
  37. biggestStack = 0
  38. start = (0, 0)
  39. end = (0, 0)
  40. # Gather our code in a main() function
  41. def main():
  42. global matrix, stack, currentCell, start, rows, cols, cellSize, margin
  43. # Setup
  44. if len(sys.argv) > 1:
  45. cellSize = int(sys.argv[1])
  46. random.seed()
  47. drawingWidth = image.width - margin * 2
  48. drawingHeight = image.height - margin * 2
  49. draw.rectangle([margin, margin, margin + drawingWidth, margin + drawingHeight], fill="white")
  50. rows = int(math.floor(drawingHeight / cellSize))
  51. cols = int(math.floor(drawingWidth / cellSize))
  52. print(cellSize)
  53. for y in range(rows):
  54. for x in range(cols):
  55. matrix.append(Cell(x, y))
  56. currentCell = matrix[0]
  57. start = (currentCell.x, currentCell.y)
  58. while nrOfUnvisitedCells() > 0:
  59. runMazeStep()
  60. # Draw
  61. for cell in matrix:
  62. cell.draw(draw)
  63. image.save("maze.png")
  64. def runMazeStep():
  65. global currentCell, stack, end, biggestStack
  66. currentCell.visited = True
  67. nei = []
  68. if currentCell.x > 0:
  69. cell = matrix[index(currentCell.x - 1, currentCell.y)]
  70. if not cell.visited:
  71. nei.append(cell)
  72. if currentCell.y > 0:
  73. cell = matrix[index(currentCell.x, currentCell.y - 1)]
  74. if not cell.visited:
  75. nei.append(cell)
  76. if currentCell.x < cols - 1:
  77. cell = matrix[index(currentCell.x + 1, currentCell.y)]
  78. if not cell.visited:
  79. nei.append(cell)
  80. if currentCell.y < rows - 1:
  81. cell = matrix[index(currentCell.x, currentCell.y + 1)]
  82. if not cell.visited:
  83. nei.append(cell)
  84. if len(nei) > 0:
  85. chosen = random.choice(nei)
  86. stack.append(currentCell)
  87. if len(stack) > biggestStack:
  88. biggestStack = len(stack)
  89. end = (chosen.x, chosen.y)
  90. if chosen.x < currentCell.x:
  91. chosen.walls &= ~(1 << 1)
  92. currentCell.walls &= ~(1 << 3)
  93. elif chosen.x > currentCell.x:
  94. chosen.walls &= ~(1 << 3)
  95. currentCell.walls &= ~(1 << 1)
  96. elif chosen.y > currentCell.y:
  97. chosen.walls &= ~(1 << 2)
  98. currentCell.walls &= ~(1 << 0)
  99. elif chosen.y < currentCell.y:
  100. chosen.walls &= ~(1 << 0)
  101. currentCell.walls &= ~(1 << 2)
  102. currentCell = chosen
  103. else:
  104. currentCell = stack.pop()
  105. def nrOfUnvisitedCells():
  106. global matrix
  107. res = len([cell for cell in matrix if not cell.visited])
  108. return res
  109. def index(x, y):
  110. global cols
  111. return y * cols + x
  112. # Standard boilerplate to call the main() function to begin
  113. # the program.
  114. if __name__ == '__main__':
  115. main()