Advertisement
cookertron

Maze Game

Dec 27th, 2020
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import math, random
  2. import pygame
  3. from pygame import gfxdraw
  4.  
  5. def mazeCells(width, height):
  6.     """ Create a grid with empty cells on odd row/column combinations. """
  7.     grid = []
  8.     for row in range(height):
  9.         grid.append([])
  10.         for column in range(width):
  11.             if column % 2 == 1 and row % 2 == 1:
  12.                 grid[row].append(0)
  13.             elif column == 0 or row == 0 or column == width - 1 or row == height - 1:
  14.                 grid[row].append(1)
  15.             else:
  16.                 grid[row].append(1)
  17.     return grid
  18.  
  19.  
  20. def makeMaze(width, height):
  21.     maze = mazeCells(width, height)
  22.  
  23.     w = (len(maze[0]) - 1) // 2
  24.     h = (len(maze) - 1) // 2
  25.     vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
  26.  
  27.     def walk(x: int, y: int):
  28.         vis[y][x] = 1
  29.  
  30.         d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
  31.         random.shuffle(d)
  32.         for (xx, yy) in d:
  33.             if vis[yy][xx]:
  34.                 continue
  35.             if xx == x:
  36.                 maze[max(y, yy) * 2][x * 2 + 1] = 0
  37.             if yy == y:
  38.                 maze[y * 2 + 1][max(x, xx) * 2] = 0
  39.  
  40.             walk(xx, yy)
  41.  
  42.     walk(random.randrange(w), random.randrange(h))
  43.  
  44.     return maze
  45.  
  46.  
  47. pygame.init()
  48. PDR = pygame.Rect(0, 0, 1280, 960)
  49. PD = pygame.display.set_mode(PDR.size)
  50. FPS = 60
  51.  
  52. BLACK = (0, 0, 0)
  53. WHITE = (255, 255, 255)
  54. RED = (190, 0, 31)
  55. BLUE = (10, 181, 206)
  56.  
  57. MAX_CIRCLE_DIAMETER = 64
  58. MAX_CIRCLE_RADIUS = MAX_CIRCLE_DIAMETER // 2
  59.  
  60. REVEAL_RADIUS = 300
  61.  
  62. quitDemo = False
  63.  
  64. events = []
  65.  
  66. mx, my = 21, 16
  67.  
  68. m = makeMaze(mx, my)
  69.  
  70. while True:
  71.     events.extend(pygame.event.get())
  72.     if events:
  73.         e = events.pop()
  74.         if (e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE) or e.type == pygame.QUIT: break
  75.  
  76.     PD.fill(BLUE)
  77.  
  78.     mx, my = pygame.mouse.get_pos()
  79.     print((mx + MAX_CIRCLE_RADIUS) // MAX_CIRCLE_DIAMETER, my)
  80.     for x in range(0, PDR.w + 1, MAX_CIRCLE_DIAMETER):
  81.         for y in range(0, PDR.h + 1, MAX_CIRCLE_DIAMETER):
  82.             d = math.hypot(mx - x, my - y)
  83.             if d <= REVEAL_RADIUS:
  84.                 radius = MAX_CIRCLE_RADIUS - (MAX_CIRCLE_RADIUS / REVEAL_RADIUS * d)
  85.             else:
  86.                 radius = 0
  87.            
  88.             if radius:
  89.                 pygame.gfxdraw.filled_circle(PD, x, y, int(radius), RED)
  90.                 if radius >= 4:
  91.                     if m[(y + MAX_CIRCLE_RADIUS) // MAX_CIRCLE_DIAMETER][(x + MAX_CIRCLE_RADIUS) // MAX_CIRCLE_DIAMETER]:
  92.                         pygame.gfxdraw.filled_circle(PD, x, y, int(radius // 2), WHITE)
  93.                     else:
  94.                         pygame.gfxdraw.filled_circle(PD, x, y, int(radius // 2), BLUE)                        
  95.    
  96.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement