Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import secrets
- import pygame
- from pygame.locals import *
- # Initialize pygame
- pygame.init()
- # Set up display
- WIDTH, HEIGHT = 1024, 1024 # Set the dimensions of the window
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- # Title of the window
- pygame.display.set_caption('Red Line on Black Background')
- # Colors
- BLACK = (0, 0, 0)
- RED = (120, 30, 30)
- wi, he = 25, 25
- cells = [[0 for i in range(he)] for j in range(wi)]
- cnt = 0
- cell_group = [[(j, i) for i in range(he)] for j in range(wi)]
- def seek(i, j):
- if cell_group[i][j] != (i, j):
- p1, p2 = cell_group[i][j]
- return seek(p1, p2)
- else:
- return (i, j)
- def join(i1, j1, i2, j2):
- i1r, j1r = seek(i1, j1)
- i2r, j2r = seek(i2, j2)
- if (i1r, j1r) == (i2r, j2r):
- return
- if (i1r, j1r) < (i2r, j2r):
- mi1, mj1 = (i1r, j1r)
- xmi1, xmj1 = (i2r, j2r)
- else:
- mi1, mj1 = (i2r, j2r)
- xmi1, xmj1 = (i1r, j1r)
- cell_group[xmi1][xmj1] = (mi1, mj1)
- connections = []
- used_connections = []
- sister_pairs_third_group = {}
- for i in range(wi):
- for j in range(he):
- for id in range(-1, 2):
- for jd in range(-1, 2):
- if id == jd and id == 0:
- continue
- di, dj = i + id, j + jd
- if di < 0 or dj < 0 or di >= wi or dj >= he:
- continue
- connections.append((i, j, di, dj))
- while connections:
- r = secrets.randbelow(len(connections))
- i, j, di, dj = connections.pop(r)
- spx, spy = -1, -1
- if (i, j, di, dj) in sister_pairs_third_group:
- x8, y8 = sister_pairs_third_group[(i, j, di, dj)]
- spx, spy = seek(x8, y8)
- if seek(i, j) == seek(di, dj) or seek(i, j) == (spx, spy) or seek(di, dj) == (spx, spy):
- continue
- else:
- used_connections.append((i, j, di, dj))
- join(i, j, di, dj)
- if spx != -1:
- join(i, j, spx, spy)
- if not (i == di or j == dj):
- nx, ny = seek(i, j)
- sister_pairs_third_group[(i, dj, di, j)] = (nx, ny)
- sister_pairs_third_group[(di, j, i, dj)] = (nx, ny)
- sister_pairs_third_group[(di, j, i, dj)] = (nx, ny)
- sister_pairs_third_group[(i, dj, di, j, )] = (nx, ny)
- sx, sy = WIDTH // (wi + 2), HEIGHT // (he + 2)
- # Main loop
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == QUIT:
- running = False
- # Clear the screen
- screen.fill(BLACK)
- # Draw a thick red line (start_x, start_y) to (end_x, end_y) with a thickness of 10 pixels
- for i, j, di, dj in used_connections:
- pygame.draw.line(screen, RED, (sx + sx * i, sy + sy * j), (sx + sx * di, sy + sy * dj), 11)
- for i in range(he):
- for j in range(wi):
- pygame.draw.circle(screen, RED, (sx + sx * j, sy + sy * i), 10, 10)
- # Update the screen
- pygame.display.flip()
- # Quit pygame
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement