mazelike.py 1004 B

12345678910111213141516171819202122232425262728293031323334
  1. import random
  2. # we rely on these being odd later on
  3. WIDTH, HEIGHT = 51, 31
  4. # set up all the pixels as black for the time being
  5. PIXELS = {(x,y): 1 for x in range(WIDTH) for y in range(HEIGHT)}
  6. # for every pixel in the image
  7. for (x, y) in PIXELS:
  8. # this effectively makes a grid of white pixels separated by black
  9. # lines
  10. if x % 2 == 1 and y % 2 == 1:
  11. PIXELS[(x, y)] = 0
  12. # but at each grid line, choose to make a random line going
  13. # either right or down
  14. if random.random() > 0.5:
  15. PIXELS[(x+1, y)] = 0
  16. else:
  17. PIXELS[(x, y+1)] = 0
  18. # also just make the edges black so we don't have white trails
  19. # going off the side.
  20. if x == WIDTH - 1 or y == HEIGHT - 1:
  21. PIXELS[(x, y)] = 1
  22. # print the PBM file
  23. print('P1')
  24. print(f'{WIDTH} {HEIGHT}')
  25. for y in range(HEIGHT):
  26. # we could do this all on one line, but this is nice for debugging
  27. for x in range(WIDTH):
  28. print(f'{PIXELS[x,y]}', end=' ')
  29. print()