diff --git a/raycaster.py b/raycaster.py index 04f1e2b..67f91b7 100755 --- a/raycaster.py +++ b/raycaster.py @@ -8,38 +8,46 @@ import sys import sdl2.ext import math -from time import time +import time MAP_WIN_WIDTH = 640 MAP_WIN_HEIGHT = 640 -RAYCAST_WIN_WIDTH = 900 -RAYCAST_WIN_HEIGHT = 600 +RAYCAST_WIN_WIDTH = 800 +RAYCAST_WIN_HEIGHT = 480 DUNGEON_WIDTH = MAP_WIN_WIDTH DUNGEON_HEIGHT = MAP_WIN_HEIGHT PLAYER_SPEED = 10 PLAYER_ROTATION_SPEED = 0.17 RAY_LENGTH = 100 -MAP_SCALE = 80 -DOF = 8 # Depth Of Field +MAP_SCALE = 40 MAP = [ - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 0, 0, 0, 1, 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, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ] -MAP_SIZE = 8 +MAP_SIZE = 16 +DOF = 2*MAP_SIZE # Depth Of Field class Main: def __init__(self): # Check valid map if len(MAP) != MAP_SIZE * MAP_SIZE: - raise ValueError("Map size is {}, but should be a power of {}", len(MAP), MAP_SIZE) + raise ValueError("Map size is {}, but should be a power of {}".format(len(MAP), MAP_SIZE)) # Graphics sdl2.ext.init() @@ -57,6 +65,9 @@ class Main: return def run(self): + lastFpsCalcTime = 0 + frames = 0 + running = True while running: events = sdl2.ext.get_events() @@ -97,14 +108,25 @@ class Main: if self.player_position["r"] < 0: self.player_position["r"] = 2*math.pi - sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(0,0,0,0)) # Clears screen - sdl2.ext.draw.fill(self.raycastSurface, sdl2.ext.Color(0,0,0,0)) # Clears screen self.draw() self.mapWindow.refresh() self.raycastWindow.refresh() + + # Calculate FPS + frames = frames + 1 + if time.time() - lastFpsCalcTime > 1: + fps = frames/(time.time() - lastFpsCalcTime) + print(int(fps)) + frames = 0 + lastFpsCalcTime = time.time() + return 0 def draw(self): + sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(0,0,0,255)) # Clears map screen + sdl2.ext.draw.fill(self.raycastSurface, sdl2.ext.Color(0,0,128,255), (0, 0, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws ceiling) + sdl2.ext.draw.fill(self.raycastSurface, sdl2.ext.Color(0,128,0,255), (0, RAYCAST_WIN_HEIGHT/2, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws floor) + self.draw2Dmap() self.drawPlayer() self.drawRays() @@ -165,12 +187,12 @@ class Main: xOffset = -yOffset * aTan # Check if we reached a wall - while dof < 8: + while dof < DOF: mapX = int(rayX / MAP_SCALE) mapY = int(rayY / MAP_SCALE) mapArrayPosition = mapY * MAP_SIZE + mapX if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0: - dof = 8 # Hit the wall: we are done, no need to do other checks + dof = DOF # Hit the wall: we are done, no need to do other checks else: # Didn't hit the wall: check successive horizontal line rayX = rayX + xOffset @@ -206,12 +228,12 @@ class Main: yOffset = -xOffset * nTan # Check if we reached a wall - while dof < 8: + while dof < DOF: mapX = int(rayX / MAP_SCALE) mapY = int(rayY / MAP_SCALE) mapArrayPosition = mapY * MAP_SIZE + mapX if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] != 0: - dof = 8 # Hit the wall: we are done, no need to do other checks + dof = DOF # Hit the wall: we are done, no need to do other checks else: # Didn't hit the wall: check successive horizontal line rayX = rayX + xOffset