##################################### ## Python Library to Create a Maze ## ## ------ Called in Game.py ------ ## ##################################### # Make_Maze is a function we found online. # We did NOT write this part. It was lightly edited only. # Source: http://rosettacode.org/wiki/Maze_generation#Python def make_maze(w, h): # The symbols used in this were changed to be more machine-readable. import random vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)] ver = [["* "] * w + ['*'] for _ in range(h)] + [[]] hor = [["**"] * w + ['*'] for _ in range(h + 1)] def walk(x, y): 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: hor[max(y, yy)][x] = "* " if yy == y: ver[y][max(x, xx)] = " " walk(xx, yy) walk(random.randrange(w), random.randrange(h)) s = "" for (a, b) in zip(hor, ver): s += ''.join(a + ['\n'] + b + ['\n']) return s ######################################## ## Everything below is written by us: ## ######################################## def maze_create(t): # Takes string output from make_maze, and makes it in turtle import turtle #These first 10 or so lines just set up the function import random t.up() t.goto(-515, 515) t.shape("square") t.shapesize(2.45, 2.45, 1) #Making the size 2.45 fixed odd spacing issues in the maze t.color("#4682B4") t.speed(0) maze = make_maze(10, 10) listCoord = [] #Records the coordinates of maze walls. Used to keep turtle inside maze. xGoal = 0 yGoal = 0 keyGoal = random.randint(1, 440) # Creates random point on maze to put black win box. counter = 0 paint_color = [] #Holds current paint color, so black win box doesn't screw it up for n, c in enumerate(maze): if c == "*": # Asterisk represents a wall in the maze. Places a stamp and records coordinates. t.stamp() listCoord.append([t.xcor(),t.ycor()]) if c != "\n": #For non-newlines, moves to the next square if counter > keyGoal and c != "*": # Places the black win box. counter = -100000 t.color("black") t.stamp() t.color(paint_color) xGoal = t.xcor() yGoal = t.ycor() t.goto(t.xcor() + 50, t.ycor()) counter += 1 else: # Starts a new line of the maze. paint_color = (random.random(), random.random(), random.random()) t.color(paint_color) t.goto(-515, t.ycor() - 50) counter += 1 return listCoord, xGoal, yGoal