I found this code on a reddit post. I cannot find anymore who did it. This code generates a random labirynth and I added the chance to mode the green square in the labirynth with the awsd keys. Enjoy.
The original code (not mine) to make the maze
from tkinter import * from random import randint cell_size = 9 #pixels ms = 100 # rows and columns visited_cells = [] walls = [] map = [['w' for _ in range(ms)]for _ in range(ms)] def create(): for row in range(ms): for col in range(ms): if map[row][col] == 'P': color = 'White' elif map[row][col] == 'w': color = 'black' draw(row, col, color) def draw(row, col, color): x1 = col*cell_size y1 = row*cell_size x2 = x1+cell_size y2 = y1+cell_size ffs.create_rectangle(x1, y1, x2, y2, fill=color) def check_neighbours(ccr, ccc): neighbours = [[ccr, ccc-1, ccr-1, ccc-2, ccr, ccc-2, ccr+1, ccc-2, ccr-1, ccc-1, ccr+1, ccc-1], #left [ccr, ccc+1, ccr-1, ccc+2, ccr, ccc+2, ccr+1, ccc+2, ccr-1, ccc+1, ccr+1, ccc+1], #right [ccr-1, ccc, ccr-2, ccc-1, ccr-2, ccc, ccr-2, ccc+1, ccr-1, ccc-1, ccr-1, ccc+1], #top [ccr+1, ccc, ccr+2, ccc-1, ccr+2, ccc, ccr+2, ccc+1, ccr+1, ccc-1, ccr+1, ccc+1]] #bottom visitable_neighbours = [] for i in neighbours: #find neighbours to visit if i[0] > 0 and i[0] < (ms-1) and i[1] > 0 and i[1] < (ms-1): if map[i[2]][i[3]] == 'P' or map[i[4]][i[5]] == 'P' or map[i[6]][i[7]] == 'P' or map[i[8]][i[9]] == 'P' or map[i[10]][i[11]] == 'P': walls.append(i[0:2]) else: visitable_neighbours.append(i[0:2]) return visitable_neighbours #StartingPoint scr = randint(1, ms) scc = randint(1, ms) start_color = 'Green' ccr, ccc = scr, scc map[ccr][ccc] = 'P' finished = False while not finished: visitable_neighbours = check_neighbours(ccr, ccc) if len(visitable_neighbours) != 0: d = randint(1, len(visitable_neighbours))-1 ncr, ncc = visitable_neighbours[d] map[ncr][ncc] = 'P' visited_cells.append([ncr, ncc]) ccr, ccc = ncr, ncc if len(visitable_neighbours) == 0: try: ccr, ccc = visited_cells.pop() except: finished = True window = Tk() window.title('Maze') canvas_side = ms*cell_size ffs = Canvas(window, width = canvas_side, height = canvas_side, bg = 'grey') ffs.pack() create() draw(scr, scc, start_color) window.mainloop()
The ‘new’ code to navigate through the maze
it would be nice if someone could make a 3d way to navigate it.
from tkinter import * from random import randint cell_size = 12 #pixels ms = 50 # rows and columns visited_cells = [] walls = [] revisited_cells = [] # creates a list with 50 x 50 "w" items map = [['w' for _ in range(ms)] for _ in range(ms)] def create(): "Create a rectangle with draw function (below) with random color" for row in range(ms): for col in range(ms): if map[row][col] == 'P': color = 'White' elif map[row][col] == 'w': color = 'black' draw(row, col, color) def draw(row, col, color): x1 = col * cell_size y1 = row * cell_size x2 = x1 + cell_size y2 = y1 + cell_size ffs.create_rectangle(x1, y1, x2, y2, fill=color) def check_neighbours(ccr, ccc): neighbours = [[ ccr, ccc - 1, ccr - 1, ccc - 2, ccr, ccc - 2, ccr + 1, ccc - 2, ccr - 1, ccc - 1, ccr + 1, ccc - 1 ], # left [ccr, ccc + 1, ccr - 1, ccc + 2, ccr, ccc + 2, ccr + 1, ccc + 2, ccr - 1, ccc + 1, ccr + 1, ccc + 1], #right [ccr - 1, ccc, ccr - 2, ccc - 1, ccr - 2, ccc, ccr - 2, ccc + 1, ccr - 1, ccc - 1, ccr - 1, ccc + 1], #top [ccr + 1, ccc, ccr + 2, ccc - 1, ccr + 2, ccc, ccr + 2, ccc + 1, ccr + 1, ccc-1, ccr + 1, ccc + 1]] #bottom visitable_neighbours = [] for i in neighbours: #find neighbours to visit if i[0] > 0 and i[0] < (ms-1) and i[1] > 0 and i[1] < (ms-1): if map[i[2]][i[3]] == 'P' or map[i[4]][i[5]] == 'P' or map[i[6]][i[7]] == 'P' or map[i[8]][i[9]] == 'P' or map[i[10]][i[11]] == 'P': walls.append(i[0:2]) else: visitable_neighbours.append(i[0:2]) return visitable_neighbours #StartingPoint # starting color of row scr = randint(1, ms) # starting random column scc = randint(1, ms) start_color = 'Green' # memorize row and column of the starting rectangle # current color row and current color column ccr, ccc = scr, scc x1 = ccr * 12 y1 = ccc * 12 print(scr, scc) print(ccr, ccc) map[ccr][ccc] = 'P' loop = 1 while loop: visitable_neighbours = check_neighbours(ccr, ccc) if len(visitable_neighbours) != 0: d = randint(1, len(visitable_neighbours))-1 ncr, ncc = visitable_neighbours[d] map[ncr][ncc] = 'P' visited_cells.append([ncr, ncc]) ccr, ccc = ncr, ncc if len(visitable_neighbours) == 0: try: ccr, ccc = visited_cells.pop() revisited_cells.append([ccr, ccc]) except: loop = 0 window = Tk() window.title('Maze') canvas_side = ms*cell_size ffs = Canvas(window, width = canvas_side, height = canvas_side, bg = 'grey') ffs.pack() create() y1 = scr * cell_size x1 = scc * cell_size draw(scr, scc, start_color) e = randint(1, len(revisited_cells))-1 ecr = revisited_cells[e][0] ecc = revisited_cells[e][1] end_color = 'red' draw(ecr, ecc, end_color) # print(revisited_cells) def draw_rect(): ffs.create_rectangle((x1, y1, x1 + 12, y1 + 12), fill="green") def del_rect(): ffs.create_rectangle((x1, y1, x1 + 12, y1 + 12), fill="white") def move(event): global x1, y1 # print(event.char) del_rect() col = w = x1//cell_size row = h = y1//cell_size print("before", map[row][col]) if event.char == "a": if map[row][col - 1] == "P": x1 -= cell_size elif event.char == "d": if map[row][col + 1] == "P": x1 += cell_size elif event.char == "w": if map[row - 1][col] == "P": y1 -= cell_size elif event.char == "s": if map[row + 1][col] == "P": y1 += cell_size draw_rect() col = w = x1//cell_size row = h = y1//cell_size print(w, h) print("after", map[row][col]) window.bind("<Key>", move) window.mainloop()
Adding sounds with pygame
from tkinter import * from random import randint import pygame as pg # The use of sounds from pygame === def music_init(): "Initialize sounds avoiding delay" pg.mixer.pre_init(44100, -16, 1, 512) pg.init() pg.mixer.quit() pg.mixer.init(22050, -16, 2, 512) pg.mixer.set_num_channels(32) def game_sounds(): "Loads the sounds for the game" sounds_list = ("fire", "hit") sound_dic = {} for sound in sounds_list: sound_path = f"audio/{sound}.wav" print(sound_path) sound_dic[sound] = pg.mixer.Sound(sound_path) return sound_dic music_init() sounds_dic = game_sounds() sound = pg.mixer.Sound.play # end of code for init sounds cell_size = 12 #pixels ms = 50 # rows and columns visited_cells = [] walls = [] revisited_cells = [] # creates a list with 50 x 50 "w" items map = [['w' for _ in range(ms)] for _ in range(ms)] def create(): "Create a rectangle with draw function (below) with random color" for row in range(ms): for col in range(ms): if map[row][col] == 'P': color = 'White' elif map[row][col] == 'w': color = 'black' draw(row, col, color) def draw(row, col, color): x1 = col * cell_size y1 = row * cell_size x2 = x1 + cell_size y2 = y1 + cell_size ffs.create_rectangle(x1, y1, x2, y2, fill=color) def check_neighbours(ccr, ccc): neighbours = [[ ccr, ccc - 1, ccr - 1, ccc - 2, ccr, ccc - 2, ccr + 1, ccc - 2, ccr - 1, ccc - 1, ccr + 1, ccc - 1 ], # left [ccr, ccc + 1, ccr - 1, ccc + 2, ccr, ccc + 2, ccr + 1, ccc + 2, ccr - 1, ccc + 1, ccr + 1, ccc + 1], #right [ccr - 1, ccc, ccr - 2, ccc - 1, ccr - 2, ccc, ccr - 2, ccc + 1, ccr - 1, ccc - 1, ccr - 1, ccc + 1], #top [ccr + 1, ccc, ccr + 2, ccc - 1, ccr + 2, ccc, ccr + 2, ccc + 1, ccr + 1, ccc-1, ccr + 1, ccc + 1]] #bottom visitable_neighbours = [] for i in neighbours: #find neighbours to visit if i[0] > 0 and i[0] < (ms-1) and i[1] > 0 and i[1] < (ms-1): if map[i[2]][i[3]] == 'P' or map[i[4]][i[5]] == 'P' or map[i[6]][i[7]] == 'P' or map[i[8]][i[9]] == 'P' or map[i[10]][i[11]] == 'P': walls.append(i[0:2]) else: visitable_neighbours.append(i[0:2]) return visitable_neighbours #StartingPoint # starting color of row scr = randint(1, ms) # starting random column scc = randint(1, ms) start_color = 'Green' # memorize row and column of the starting rectangle # current color row and current color column ccr, ccc = scr, scc x1 = ccr * 12 y1 = ccc * 12 print(scr, scc) print(ccr, ccc) map[ccr][ccc] = 'P' loop = 1 while loop: visitable_neighbours = check_neighbours(ccr, ccc) if len(visitable_neighbours) != 0: d = randint(1, len(visitable_neighbours))-1 ncr, ncc = visitable_neighbours[d] map[ncr][ncc] = 'P' visited_cells.append([ncr, ncc]) ccr, ccc = ncr, ncc if len(visitable_neighbours) == 0: try: ccr, ccc = visited_cells.pop() revisited_cells.append([ccr, ccc]) except: loop = 0 window = Tk() window.title('Game') canvas_side = ms*cell_size ffs = Canvas(window, width = canvas_side, height = canvas_side, bg = 'grey') ffs.pack() create() y1 = scr * cell_size x1 = scc * cell_size draw(scr, scc, start_color) e = randint(1, len(revisited_cells))-1 ecr = revisited_cells[e][0] ecc = revisited_cells[e][1] end_color = 'red' draw(ecr, ecc, end_color) # print(revisited_cells) def draw_rect(): ffs.create_rectangle((x1, y1, x1 + 12, y1 + 12), fill="green") def del_rect(): ffs.create_rectangle((x1, y1, x1 + 12, y1 + 12), fill="white") def move(event): global x1, y1 # print(event.char) del_rect() col = w = x1//cell_size row = h = y1//cell_size print("before", map[row][col]) sound(sounds_dic["fire"]) if event.char == "a": if map[row][col - 1] == "P": x1 -= cell_size else: sound(sounds_dic["fire"]) sound(sounds_dic["fire"]) elif event.char == "d": if map[row][col + 1] == "P": x1 += cell_size else: sound(sounds_dic["fire"]) sound(sounds_dic["fire"]) elif event.char == "w": if map[row - 1][col] == "P": y1 -= cell_size else: sound(sounds_dic["fire"]) sound(sounds_dic["fire"]) elif event.char == "s": if map[row + 1][col] == "P": y1 += cell_size else: sound(sounds_dic["fire"]) sound(sounds_dic["fire"]) draw_rect() col = w = x1//cell_size row = h = y1//cell_size print(w, h) print("after", map[row][col]) window.bind("<Key>", move) window.mainloop()