Advertisement
Luc_Vest

8 Direction Maze

Sep 28th, 2023
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. import secrets
  2.  
  3. import pygame
  4. from pygame.locals import *
  5.  
  6. # Initialize pygame
  7. pygame.init()
  8.  
  9. # Set up display
  10. WIDTH, HEIGHT = 1024, 1024  # Set the dimensions of the window
  11. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  12.  
  13. # Title of the window
  14. pygame.display.set_caption('Red Line on Black Background')
  15.  
  16. # Colors
  17. BLACK = (0, 0, 0)
  18. RED = (120, 30, 30)
  19.  
  20. wi, he = 25, 25
  21.  
  22. cells = [[0 for i in range(he)] for j in range(wi)]
  23.  
  24. cnt = 0
  25.  
  26. cell_group = [[(j, i) for i in range(he)] for j in range(wi)]
  27.  
  28. def seek(i, j):
  29.     if cell_group[i][j] != (i, j):
  30.         p1, p2 = cell_group[i][j]
  31.         return seek(p1, p2)
  32.     else:
  33.         return (i, j)
  34.  
  35. def join(i1, j1, i2, j2):
  36.     i1r, j1r = seek(i1, j1)
  37.     i2r, j2r = seek(i2, j2)
  38.  
  39.     if (i1r, j1r) == (i2r, j2r):
  40.         return
  41.  
  42.     if (i1r, j1r) < (i2r, j2r):
  43.         mi1, mj1 = (i1r, j1r)
  44.         xmi1, xmj1 = (i2r, j2r)
  45.     else:
  46.         mi1, mj1 = (i2r, j2r)
  47.         xmi1, xmj1 = (i1r, j1r)
  48.  
  49.     cell_group[xmi1][xmj1] = (mi1, mj1)
  50.  
  51. connections = []
  52. used_connections = []
  53.  
  54. sister_pairs_third_group = {}
  55.  
  56. for i in range(wi):
  57.     for j in range(he):
  58.         for id in range(-1, 2):
  59.             for jd in range(-1, 2):
  60.                 if id == jd and id == 0:
  61.                     continue
  62.                 di, dj = i + id, j + jd
  63.                 if di < 0 or dj < 0 or di >= wi or dj >= he:
  64.                     continue
  65.                 connections.append((i, j, di, dj))
  66.  
  67. while connections:
  68.     r = secrets.randbelow(len(connections))
  69.     i, j, di, dj = connections.pop(r)
  70.     spx, spy = -1, -1
  71.  
  72.     if (i, j, di, dj) in sister_pairs_third_group:
  73.         x8, y8 = sister_pairs_third_group[(i, j, di, dj)]
  74.         spx, spy = seek(x8, y8)
  75.     if seek(i, j) == seek(di, dj) or seek(i, j) == (spx, spy) or seek(di, dj) == (spx, spy):
  76.         continue
  77.     else:
  78.         used_connections.append((i, j, di, dj))
  79.         join(i, j, di, dj)
  80.         if spx != -1:
  81.             join(i, j, spx, spy)
  82.  
  83.         if not (i == di or j == dj):
  84.             nx, ny = seek(i, j)
  85.             sister_pairs_third_group[(i, dj, di, j)] = (nx, ny)
  86.             sister_pairs_third_group[(di, j, i, dj)] = (nx, ny)
  87.             sister_pairs_third_group[(di, j, i, dj)] = (nx, ny)
  88.             sister_pairs_third_group[(i, dj, di, j, )] = (nx, ny)
  89.  
  90.  
  91.  
  92. sx, sy = WIDTH // (wi + 2), HEIGHT // (he + 2)
  93.  
  94. # Main loop
  95. running = True
  96. while running:
  97.     for event in pygame.event.get():
  98.         if event.type == QUIT:
  99.             running = False
  100.  
  101.     # Clear the screen
  102.     screen.fill(BLACK)
  103.  
  104.     # Draw a thick red line (start_x, start_y) to (end_x, end_y) with a thickness of 10 pixels
  105.     for i, j, di, dj in used_connections:
  106.         pygame.draw.line(screen, RED, (sx + sx * i, sy + sy * j), (sx + sx * di, sy + sy * dj), 11)
  107.  
  108.     for i in range(he):
  109.         for j in range(wi):
  110.             pygame.draw.circle(screen, RED, (sx + sx * j, sy + sy * i), 10, 10)
  111.  
  112.  
  113.     # Update the screen
  114.     pygame.display.flip()
  115.  
  116. # Quit pygame
  117. pygame.quit()
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement