maze_gen.py

Created by squarepoint

Created on April 13, 2023

1.07 KB

This (unoptimised) recursive algorithm generates a whole different maze each time you load the script! Crazy right?! (it takes a lot of time to render tho) Ima try to raycast it now…


from math import floor
from kandinsky import fill_rect as rct
from random import choice
from ion import keydown as kdn

def check(M,x,y):
  D=[]
  if M[y*21+x-2]==0 and x!=1:D.append([-2,0])
  if M[y*21+x+2]==0 and x!=19:D.append([2,0])
  if M[(y-2)*21+x]==0 and y!=1:D.append([0,-2])
  if y!=19 and M[(y+2)*21+x]==0:D.append([0,2])
  return D

def gen():
  M=[]
  for i in range(22):
    M.append(0)
  M.append(1)
  for i in range(418):
    M.append(0)
  P=[]
  x=1
  y=1
  while True:
    D=check(M,x,y)
    if len(D)==0:
      while len(D)==0:
        x-=P[-1][0]
        y-=P[-1][1]
        P.pop()
        D=check(M,x,y)
        if len(D)==0 and len(P)==0:
          return M
    e=choice(D)
    P.append(e)
    M2=[]
    for i in range(441):
      if i==(y+e[1])*21+x+e[0]or i==(y+e[1]/2)*21+x+e[0]/2:
        M2.append(1)
      else:
        M2.append(M[i])
    M=M2
    x+=e[0]
    y+=e[1]

def render(M):
  for i in range(441):
    x=i%21
    y=floor(i/21)
    c=M[i]*255
    rct(55+x*10,5+y*10,10,10,(c,c,c))

while True:
  render(gen())
  while not kdn(4):
    pass

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.