A Python Game in Terminal with All Deep Research log's and formulas
Screen.Recording.2025-05-27.150625.mp4
Inspired by classic games like Doom and Wolfenstein 3D, this project simulates 3D environments using ASCII characters and basic trigonometry.
This project uses fundamental raycasting techniques to simulate a 3D world from a 2D map.
- Position:
P = (px, py)
- View angle (facing direction):
θ
in radians
For each column i
on the terminal screen:
FOV = π / 3 # e.g. 60 degrees field of view ray_angle = θ - (FOV / 2) + (i / screen_width) * FOV
ray_angle = θ - (FOV / 2) + (i / screen_width) * FOV
https://github.com/user-attachments/assets/4cd98917-8033-4464-a7ce-5cff0fca05cc
ray_dir_x = cos(ray_angle)
ray_dir_y = sin(ray_angle)
- Iterate along the ray in small steps until a wall is hit.
Iteratively step through the map until a wall is hit:
(x, y) = (px, py)
loop:
x += ray_dir_x * step
y += ray_dir_y * step
if map[⌊y⌋][⌊x⌋] == wall:
break
distance = sqrt((x - px)² + (y - py)²)
wall_height = screen_height / distance
if distance < close: char = '█'
elif distance < medium: char = '▓'
elif distance < far: char = '░'
else: char = '.'
θ += Δθ # positive for right, negative for left
px += cos(θ) * move_speed # forward py += sin(θ) * move_speed px -= cos(θ) * move_speed # backward py -= sin(θ) * move_speed
Key | Action |
---|---|
← / → | Rotate view (left/right) |
↑ / ↓ | Move forward/backward |
W / S | Optional movement keys |
The world is a 2D grid:
map = [
[1,1,1,1,1,1,1],
[1,0,0,0,0,0,1],
[1,0,1,0,1,0,1],
[1,0,1,0,1,0,1],
[1,0,0,0,0,0,1],
[1,1,1,1,1,1,1]
]
Where:
1
= Wall0
= Walkable space
- Python 3
curses
(comes with Python for Unix/macOS)- Windows: use
windows-curses
viapip install windows-curses
- Windows: use
Built with ❤️ using:
- ASCII art rendering
- Basic trigonometry
- Classic game engine inspiration
- Add monsters or objects
- Map editor using text input
- Minimap overlay
- Save/load map feature