Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, random
- import pygame
- from pygame import gfxdraw
- def mazeCells(width, height):
- """ Create a grid with empty cells on odd row/column combinations. """
- grid = []
- for row in range(height):
- grid.append([])
- for column in range(width):
- if column % 2 == 1 and row % 2 == 1:
- grid[row].append(0)
- elif column == 0 or row == 0 or column == width - 1 or row == height - 1:
- grid[row].append(1)
- else:
- grid[row].append(1)
- return grid
- def makeMaze(width, height):
- maze = mazeCells(width, height)
- w = (len(maze[0]) - 1) // 2
- h = (len(maze) - 1) // 2
- vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
- def walk(x: int, y: int):
- vis[y][x] = 1
- d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
- random.shuffle(d)
- for (xx, yy) in d:
- if vis[yy][xx]:
- continue
- if xx == x:
- maze[max(y, yy) * 2][x * 2 + 1] = 0
- if yy == y:
- maze[y * 2 + 1][max(x, xx) * 2] = 0
- walk(xx, yy)
- walk(random.randrange(w), random.randrange(h))
- return maze
- pygame.init()
- PDR = pygame.Rect(0, 0, 1280, 960)
- PD = pygame.display.set_mode(PDR.size)
- FPS = 60
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (190, 0, 31)
- BLUE = (10, 181, 206)
- MAX_CIRCLE_DIAMETER = 64
- MAX_CIRCLE_RADIUS = MAX_CIRCLE_DIAMETER // 2
- REVEAL_RADIUS = 300
- quitDemo = False
- events = []
- mx, my = 21, 16
- m = makeMaze(mx, my)
- while True:
- events.extend(pygame.event.get())
- if events:
- e = events.pop()
- if (e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE) or e.type == pygame.QUIT: break
- PD.fill(BLUE)
- mx, my = pygame.mouse.get_pos()
- print((mx + MAX_CIRCLE_RADIUS) // MAX_CIRCLE_DIAMETER, my)
- for x in range(0, PDR.w + 1, MAX_CIRCLE_DIAMETER):
- for y in range(0, PDR.h + 1, MAX_CIRCLE_DIAMETER):
- d = math.hypot(mx - x, my - y)
- if d <= REVEAL_RADIUS:
- radius = MAX_CIRCLE_RADIUS - (MAX_CIRCLE_RADIUS / REVEAL_RADIUS * d)
- else:
- radius = 0
- if radius:
- pygame.gfxdraw.filled_circle(PD, x, y, int(radius), RED)
- if radius >= 4:
- if m[(y + MAX_CIRCLE_RADIUS) // MAX_CIRCLE_DIAMETER][(x + MAX_CIRCLE_RADIUS) // MAX_CIRCLE_DIAMETER]:
- pygame.gfxdraw.filled_circle(PD, x, y, int(radius // 2), WHITE)
- else:
- pygame.gfxdraw.filled_circle(PD, x, y, int(radius // 2), BLUE)
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement