maze2.py

Created by vef03715

Created on February 01, 2021

2.31 KB


# maze2 
from kandinsky import *
from random import *

def vline(x,y,w,wl):
  if w:
    x1=x*wl+9
    y1=y*wl+5
    fill_rect(x1,y1,1,wl,color(0,0,0))

def hline(x,y,w,wl):
  if w:
    x1=x*wl+9
    y1=y*wl+5
    fill_rect(x1,y1,wl,1,color(0,0,0))

def cell(x,y,a,w,wl):
  z=a[x+y*w]
  hline(x,y,z&1,wl)
  vline(x+1,y,z&2,wl)
  hline(x,y+1,z&4,wl)
  vline(x,y,z&8,wl)

def disp(w,h,a,wl):
  for x in range(w):
    for y in range(h):
      cell(x,y,a,w,wl)

def nor(u,w,a,n):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
  return u

def eas(u,w,a,n):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&=13
      a[v]&=7
      return v
  return u

def sou(u,w,a,n):
  v=u+w 
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
  return u

def wes(u,w,a,n):
  v=u-1 
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
  return u

def move(u,c,w,a,n):
  for i in range(c):
    d=randrange(4)
    if d==0:
      u=nor(u,w,a,n)
    if d==1:
      u=eas(u,w,a,n)
    if d==2:
      u=sou(u,w,a,n)
    if d==3:
      u=wes(u,w,a,n)

def maze():
  print("*** maze2 ***")
  print("making now")
  # Initialize
  w=25
  h=17
  n=w*h
  c=n/4
  wl=12
  a=[15 for x in range(n)]
  t=[x for x in range(n)]
  a[0]=7
  
  while 1:
    
    # Shuffle the indexes
    for i in range(n):
      j=randrange(n)
      t[i],t[j]=t[j],t[i]
    
    # Check each cell in index order
    done=True
    for i in range(n):
      u=t[i]
      if a[u]==15:
        done=False
      else:
        move(u,c,w,a,n)
    
    # Done if all cells connected
    if done:
      break
  
  # Done
  a[n-1]&=13
  disp(w,h,a,wl)

def ccheck(cxy):
  if cxy[1]>315:
    cxy[0]=1
  if cxy[2]>212:
    cxy[0]=1
  return cxy

def cright(cxy):
  cc=get_pixel(cxy[1]+5,cxy[2])
  if cc != color(0,0,0):
    set_pixel(cxy[1],cxy[2],'green')
    cxy[1]=cxy[1]+1
    cxy[3]=1
  else:
    cxy[3]=4    
  cxy=ccheck(cxy)
  return cxy

def cdown(cxy):
  cc=get_pixel(cxy[1],cxy[2]+5)
  if cc != color(0,0,0):
    set_pixel(cxy[1],cxy[2],'green')
    cxy[2]=cxy[2]+1
    cxy[3]=4
  else:
    cxy[3]=2    
  cxy=ccheck(cxy)
  return cxy

def maze2():
  maze()
  cxy=[0,5,10,1]
  while True:
    if cxy[3]==1:
      cxy=cright(cxy)
    if cxy[3]==4:
      cxy=cdown(cxy)
    if cxy[0]==1:
      break
  draw_string("END",100,150,'green')

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.