Page 1 of 1

creepy 3d maze made in python!

Posted: Thu Sep 18, 2025 3:53 pm
by sk8erboi1oh1 (Radiation-s)
import tkinter as tk

import math



# Maze (1 = wall, 0 = empty)

maze = [

[1,1,1,1,1,1,1,1],

[1,0,0,0,0,0,0,1],

[1,0,1,1,0,1,0,1],

[1,0,0,1,0,0,0,1],

[1,1,0,0,0,1,0,1],

[1,0,0,1,0,0,0,1],

[1,0,0,0,0,1,0,1],

[1,1,1,1,1,1,1,1]

]



TILE = 64

player_x, player_y = 100, 100

player_angle = 0



WIDTH, HEIGHT = 800, 600



root = tk.Tk()

root.title("Pseudo-3D Maze (Tkinter)")

canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="black")

canvas.pack()



def cast_rays():

canvas.delete("all")

num_rays = 120

fov = math.pi / 3

max_depth = 800

delta_angle = fov / num_rays

cur_angle = player_angle - fov / 2



for ray in range(num_rays):

sin_a = math.sin(cur_angle)

cos_a = math.cos(cur_angle)



for depth in range(max_depth):

x = player_x + depth * cos_a

y = player_y + depth * sin_a

i, j = int(x / TILE), int(y / TILE)



if maze[j] == 1:

depth *= math.cos(player_angle - cur_angle) # fix fisheye

proj_height = min(HEIGHT, 50000 / (depth + 0.0001))

shade = int(255 / (1 + depth * depth * 0.0001))

color = f'#{shade:02x}{shade:02x}{shade:02x}'

canvas.create_rectangle(

ray * (WIDTH // num_rays),

HEIGHT//2 - proj_height//2,

(ray+1) * (WIDTH // num_rays),

HEIGHT//2 + proj_height//2,

fill=color, outline=color

)

break

cur_angle += delta_angle



def move_player(dx, dy):

global player_x, player_y

new_x = player_x + dx

new_y = player_y + dy

if maze[int(new_y//TILE)][int(new_x//TILE)] == 0:

player_x, player_y = new_x, new_y



def update():

cast_rays()

root.after(30, update)



def key_press(event):

global player_angle

if event.keysym == "Left":

player_angle -= 0.1

elif event.keysym == "Right":

player_angle += 0.1

elif event.keysym == "Up":

move_player(5*math.cos(player_angle), 5*math.sin(player_angle))

elif event.keysym == "Down":

move_player(-5*math.cos(player_angle), -5*math.sin(player_angle))



root.bind("<KeyPress>", key_press)

update()

root.mainloop()