Here’s a self‐contained “Doom‑like” ray‑casting demo in Pygame. It includes a start menu, basic WASD+mouse look, a simple map, wall rendering via a DDA ray‑casting algorithm, a running score (seconds survived), and a clean exit.
Save this as, e.g., raycast_doom.py and run with python raycast_doom.py.
import pygame, sys, math, time
from pygame.locals import *
# ─────────────────────────────────────────────────────────────
# CONFIG
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
HALF_HEIGHT = SCREEN_HEIGHT // 2
FOV = math.pi / 3 # 60 degrees
HALF_FOV = FOV / 2
NUM_RAYS = 120
MAX_DEPTH = 20
DELTA_ANGLE = FOV / NUM_RAYS
DIST = NUM_RAYS / (2 * math.tan(HALF_FOV))
PROJ_COEFF = 3 * DIST * 50
SCALE = SCREEN_WIDTH // NUM_RAYS
# Simple world map
WORLD_MAP = [
'111111111111',
'1..........1',
'1..11......1',
'1..........1',
'1......11..1',
'1..........1',
'1...1......1',
'1..........1',
'111111111111',
]
# ─────────────────────────────────────────────────────────────
class Player:
def __init__(self, x, y, angle=0):
self.x, self.y = x, y
self.angle = angle
self.speed = 0.04
def movement(self):
sin_a = math.sin(self.angle)
cos_a = math.cos(self.angle)
keys = pygame.key.get_pressed()
dx = dy = 0
speed = self.speed * delta_time
# Forward/back
if keys[K_w]:
dx += cos_a * speed
dy += sin_a * speed
if keys[K_s]:
dx -= cos_a * speed
dy -= sin_a * speed
# Strafe
if keys[K_a]:
dx += sin_a * speed
dy -= cos_a * speed
if keys[K_d]:
dx -= sin_a * speed
dy += cos_a * speed
# Collision detection
if not self.check_wall(self.x + dx, self.y):
self.x += dx
if not self.check_wall(self.x, self.y + dy):
self.y += dy
# Mouse look
mx, my = pygame.mouse.get_rel()
self.angle += mx * 0.002
def check_wall(self, x, y):
i, j = int(y), int(x)
return WORLD_MAP[i][j] == '1'
class Raycaster:
def __init__(self, screen, player):
self.screen = screen
self.player = player
def cast_rays(self):
start_angle = self.player.angle - HALF_FOV
cur_angle = start_angle
for ray in range(NUM_RAYS):
sin_a = math.sin(cur_angle)
cos_a = math.cos(cur_angle)
# DDA initialization
for depth in range(MAX_DEPTH):
target_x = self.player.x + depth * cos_a
target_y = self.player.y + depth * sin_a
i, j = int(target_y), int(target_x)
if 0 <= i < len(WORLD_MAP) and 0 <= j < len(WORLD_MAP[0]):
if WORLD_MAP[i][j] == '1':
# Simple shading by depth
depth *= math.cos(self.player.angle - cur_angle)
proj_height = PROJ_COEFF / (depth + 0.0001)
color_intensity = 255 / (1 + depth * depth * 0.0001)
color = (color_intensity, color_intensity, color_intensity)
pygame.draw.rect(self.screen, color,
(ray * SCALE,
HALF_HEIGHT - proj_height // 2,
SCALE,
proj_height))
break
cur_angle += DELTA_ANGLE
# ─────────────────────────────────────────────────────────────
def draw_menu(screen):
screen.fill((0, 0, 0))
font = pygame.font.SysFont('Arial', 48, bold=True)
text = font.render('PY-RAYCASTER DEMO', True, (100, 200, 200))
screen.blit(text, (SCREEN_WIDTH//2 - text.get_width()//2, 200))
font2 = pygame.font.SysFont('Arial', 32)
text2 = font2.render('Press ENTER to Start or ESC to Quit', True, (200, 200, 200))
screen.blit(text2, (SCREEN_WIDTH//2 - text2.get_width()//2, 300))
pygame.display.flip()
def draw_hud(screen, start_time):
elapsed = int(time.time() - start_time)
font = pygame.font.SysFont('Arial', 24)
txt = font.render(f'Score (seconds): {elapsed}', True, (255, 255, 255))
screen.blit(txt, (10, 10))
def main():
global delta_time
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Doom-like Raycaster")
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
# Start menu loop
while True:
draw_menu(screen)
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit(); sys.exit()
if e.type == KEYDOWN:
if e.key == K_RETURN:
break
if e.key == K_ESCAPE:
pygame.quit(); sys.exit()
else:
continue
break
# Game init
player = Player(3.0, 3.0, angle=1.57)
raycaster = Raycaster(screen, player)
start_time = time.time()
# Game loop
while True:
delta_time = clock.tick(60)
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit(); sys.exit()
if e.type == KEYDOWN and e.key == K_ESCAPE:
pygame.quit(); sys.exit()
player.movement()
# Draw sky and floor
screen.fill((30, 30, 30), (0, 0, SCREEN_WIDTH, HALF_HEIGHT))
screen.fill((80, 80, 80), (0, HALF_HEIGHT, SCREEN_WIDTH, HALF_HEIGHT))
raycaster.cast_rays()
draw_hud(screen, start_time)
pygame.display.flip()
if __name__ == '__main__':
main()
How it works:
- Start Menu: A simple press‑Enter or ESC screen.
- Player: Moves with W/A/S/D (collision with walls), looks around with mouse.
- Ray‑Casting (DDA): Shoots 120 rays per frame, finds wall intersection, projects vertical stripes with depth shading.
- HUD: Displays a “score” that’s simply the number of seconds you’ve survived.
You can extend this by adding textures, enemies (sprites), weapons, sound, floor/ceiling textures, more complex maps, etc. Enjoy exploring your tiny “Doom‐like” world! 🚀