From 1736931554175a70bf0a37fd70dfef8513fcf796 Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Mon, 2 Jan 2023 20:40:11 +0100 Subject: [PATCH] Lighting --- raycaster.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/raycaster.py b/raycaster.py index 9aa4a66..ddd659a 100755 --- a/raycaster.py +++ b/raycaster.py @@ -12,8 +12,8 @@ from time import time MAP_WIN_WIDTH = 640 MAP_WIN_HEIGHT = 640 -RAYCAST_WIN_WIDTH = MAP_WIN_WIDTH -RAYCAST_WIN_HEIGHT = MAP_WIN_HEIGHT +RAYCAST_WIN_WIDTH = 900 +RAYCAST_WIN_HEIGHT = 600 DUNGEON_WIDTH = MAP_WIN_WIDTH DUNGEON_HEIGHT = MAP_WIN_HEIGHT PLAYER_SPEED = 10 @@ -26,10 +26,10 @@ DEGREE_IN_RADIANTS = 0.01745329 MAP = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 1, 1, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 1, - 1, 0, 1, 0, 0, 0, 0, 1, + 1, 0, 1, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, ] @@ -68,9 +68,9 @@ class Main: if event.type == sdl2.SDL_KEYDOWN: # Rotate player if event.key.keysym.sym == sdl2.SDLK_LEFT: - self.player_position["r"] = self.player_position["r"] - 5*DEGREE_IN_RADIANTS + self.player_position["r"] = self.player_position["r"] + 10*DEGREE_IN_RADIANTS elif event.key.keysym.sym == sdl2.SDLK_RIGHT: - self.player_position["r"] = self.player_position["r"] + 5*DEGREE_IN_RADIANTS + self.player_position["r"] = self.player_position["r"] - 10*DEGREE_IN_RADIANTS # Compute deltax and deltay based on player direction player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED @@ -235,9 +235,15 @@ class Main: lineHeight = RAYCAST_WIN_HEIGHT # Center line vertically in window lineOffset = RAYCAST_WIN_HEIGHT / 2 - lineHeight / 2 + + # Simulate lighting based on wall incidence + color = sdl2.ext.Color(255,255,255,255) + if vertDist > horizDist: + color = sdl2.ext.Color(200,200,200,255) + # Draw line - for x in range(i*10, i*10+10): - sdl2.ext.draw.line(self.raycastSurface, sdl2.ext.Color(255,255,255,255), (x, int(lineOffset), x, int(lineOffset + lineHeight))) + for x in range(i*15, i*15+15): + sdl2.ext.draw.line(self.raycastSurface, color, (x, int(lineOffset), x, int(lineOffset + lineHeight))) def dist(self, ax, ay, bx, by): return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))