Hideable map screen, raycast upscaling, bettere controls

This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-07 11:01:59 +01:00
parent 3c927778e2
commit 5b8b45e793

View File

@ -10,62 +10,81 @@ import sdl2.ext
import math import math
import time import time
MAP_WIN_WIDTH = 640 # Map cfg
MAP_WIN_HEIGHT = 640 MAP_HIDDEN = True
RAYCAST_WIN_WIDTH = 400 MAP_SCALE = 24
RAYCAST_WIN_HEIGHT = 255 MAP_SIZE = 17
DUNGEON_WIDTH = MAP_WIN_WIDTH MAP_WIN_WIDTH = MAP_SIZE * MAP_SCALE
DUNGEON_HEIGHT = MAP_WIN_HEIGHT MAP_WIN_HEIGHT = MAP_SIZE * MAP_SCALE
PLAYER_SPEED = 10
PLAYER_ROTATION_SPEED = 0.17
RAY_LENGTH = 100
MAP_SCALE = 40
COLLISION = True
MAP = [ # Textures cfg
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, TEXTURE_SIZE = 8
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, # Raycast cfg
1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 1, RAYCAST_WIN_WIDTH = 1000
1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 1, RAYCAST_WIN_HEIGHT = 600
1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 1, RAYCAST_RESOLUTION_SCALING = 4
1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 0, 0, 1, RAYCAST_RENDER_WIDTH = int(RAYCAST_WIN_WIDTH / RAYCAST_RESOLUTION_SCALING)
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, RAYCAST_RENDER_HEIGHT = int(RAYCAST_WIN_HEIGHT / RAYCAST_RESOLUTION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, 1,
0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1,
1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
], ],
[
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 0, 1, 1, 1, 1,
1, 0, 0, 0, 1, 1, 1, 1,
1, 0, 0, 0, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
]
] ]
TEXTURE_SIZE = 8
class Main: class Main:
@ -76,6 +95,7 @@ class Main:
# Graphics # Graphics
sdl2.ext.init() sdl2.ext.init()
if not MAP_HIDDEN:
self.mapWindow = sdl2.ext.Window("2D Map", size=(MAP_WIN_WIDTH, MAP_WIN_HEIGHT)) self.mapWindow = sdl2.ext.Window("2D Map", size=(MAP_WIN_WIDTH, MAP_WIN_HEIGHT))
self.mapWindow.show() self.mapWindow.show()
self.mapSurface = self.mapWindow.get_surface() self.mapSurface = self.mapWindow.get_surface()
@ -85,7 +105,7 @@ class Main:
self.raycastSurface = self.raycastWindow.get_surface() self.raycastSurface = self.raycastWindow.get_surface()
# Player # Player
self.player_position = {"x": int(DUNGEON_WIDTH/2), "y": int(DUNGEON_HEIGHT/2), "r": 0} # r is rotation in radiants self.player_position = PLAYER_SPAWN_POSITION
return return
@ -97,14 +117,15 @@ class Main:
while running: while running:
events = sdl2.ext.get_events() events = sdl2.ext.get_events()
for event in events: for event in events:
if event.type == sdl2.SDL_QUIT: if event.type == sdl2.SDL_QUIT or (event.type == sdl2.SDL_KEYDOWN and event.key.keysym.sym == sdl2.SDLK_ESCAPE):
running = False running = False
break break
if event.type == sdl2.SDL_KEYDOWN:
keystate = sdl2.SDL_GetKeyboardState(None)
# Rotate player # Rotate player
if event.key.keysym.sym == sdl2.SDLK_LEFT: if keystate[sdl2.SDL_SCANCODE_LEFT]:
self.player_position["r"] = self.player_position["r"] - PLAYER_ROTATION_SPEED self.player_position["r"] = self.player_position["r"] - PLAYER_ROTATION_SPEED
elif event.key.keysym.sym == sdl2.SDLK_RIGHT: elif keystate[sdl2.SDL_SCANCODE_RIGHT]:
self.player_position["r"] = self.player_position["r"] + PLAYER_ROTATION_SPEED self.player_position["r"] = self.player_position["r"] + PLAYER_ROTATION_SPEED
# Compute deltax and deltay based on player direction # Compute deltax and deltay based on player direction
@ -112,26 +133,27 @@ class Main:
player_delta_y = math.sin(self.player_position["r"]) * PLAYER_SPEED player_delta_y = math.sin(self.player_position["r"]) * PLAYER_SPEED
# Move player based on its direction # Move player based on its direction
if event.key.keysym.sym == sdl2.SDLK_UP: if keystate[sdl2.SDL_SCANCODE_UP]:
self.movePlayerRelative(player_delta_x, player_delta_y) self.movePlayerRelative(player_delta_x, player_delta_y)
elif event.key.keysym.sym == sdl2.SDLK_DOWN: elif keystate[sdl2.SDL_SCANCODE_DOWN]:
self.movePlayerRelative(-player_delta_x, -player_delta_y) self.movePlayerRelative(-player_delta_x, -player_delta_y)
# Limit position into dungeon bounds # Limit position into dungeon bounds
if self.player_position["x"] < 0: if self.player_position["x"] < 0:
self.player_position["x"] = 0 self.player_position["x"] = 0
if self.player_position["x"] > DUNGEON_WIDTH: if self.player_position["x"] > MAP_WIN_WIDTH:
self.player_position["x"] = DUNGEON_WIDTH self.player_position["x"] = MAP_WIN_WIDTH
if self.player_position["y"] < 0: if self.player_position["y"] < 0:
self.player_position["y"] = 0 self.player_position["y"] = 0
if self.player_position["y"] > DUNGEON_HEIGHT: if self.player_position["y"] > MAP_WIN_HEIGHT:
self.player_position["y"] = DUNGEON_HEIGHT self.player_position["y"] = MAP_WIN_HEIGHT
if self.player_position["r"] > 2*math.pi: if self.player_position["r"] > 2*math.pi:
self.player_position["r"] = 0 self.player_position["r"] = 0
if self.player_position["r"] < 0: if self.player_position["r"] < 0:
self.player_position["r"] = 2*math.pi self.player_position["r"] = 2*math.pi
self.draw() self.draw()
if not MAP_HIDDEN:
self.mapWindow.refresh() self.mapWindow.refresh()
self.raycastWindow.refresh() self.raycastWindow.refresh()
@ -165,10 +187,7 @@ class Main:
self.player_position["y"] = newPlayerY self.player_position["y"] = newPlayerY
def draw(self): def draw(self):
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(0,0,0,255)) # Clears map screen if not MAP_HIDDEN:
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.draw2Dmap()
self.drawPlayer() self.drawPlayer()
self.drawRays() self.drawRays()
@ -186,6 +205,7 @@ class Main:
def draw2Dmap(self): def draw2Dmap(self):
# 2D map # 2D map
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(0,0,0,255)) # Clears map screen
for i in range(len(MAP)): for i in range(len(MAP)):
posX = i % MAP_SIZE * MAP_SCALE posX = i % MAP_SIZE * MAP_SCALE
posY = math.floor(i / MAP_SIZE) * MAP_SCALE posY = math.floor(i / MAP_SIZE) * MAP_SCALE
@ -195,12 +215,15 @@ class Main:
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1)) sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1))
def drawRays(self): def drawRays(self):
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)
# Casts rays for raycasting # Casts rays for raycasting
playerAngle = self.player_position["r"] playerAngle = self.player_position["r"]
# Cast one ray for every window pixel, from -0,5 rads to +0,5 rads (about 60° viewing angle) # Cast one ray for every window pixel, from -0,5 rads to +0,5 rads (about 60° viewing angle)
for i in range(RAYCAST_WIN_WIDTH): for i in range(RAYCAST_RENDER_WIDTH):
rayAngle = playerAngle + (i/RAYCAST_WIN_WIDTH) - 0.5 rayAngle = playerAngle + (i/RAYCAST_RENDER_WIDTH) - 0.5
if rayAngle < 0: if rayAngle < 0:
rayAngle = math.pi * 2 + rayAngle rayAngle = math.pi * 2 + rayAngle
if rayAngle > math.pi * 2: if rayAngle > math.pi * 2:
@ -296,6 +319,7 @@ class Main:
rayY = horizRayY rayY = horizRayY
shortestDist = horizDist shortestDist = horizDist
if not MAP_HIDDEN:
# Draw rays in 2D view # Draw rays in 2D view
sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(0,0,255,255), (self.player_position["x"], self.player_position["y"], rayX, rayY)) sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(0,0,255,255), (self.player_position["x"], self.player_position["y"], rayX, rayY))
@ -303,9 +327,9 @@ class Main:
# ------ Draw 3D view ------ # ------ Draw 3D view ------
# Calculate line height based on distance # Calculate line height based on distance
lineHeight = MAP_SCALE * RAYCAST_WIN_HEIGHT / shortestDist lineHeight = MAP_SCALE * RAYCAST_RENDER_HEIGHT / shortestDist
# Center line vertically in window # Center line vertically in window
lineOffset = RAYCAST_WIN_HEIGHT / 2 - lineHeight / 2 lineOffset = RAYCAST_RENDER_HEIGHT / 2 - lineHeight / 2
# Draw pixels vertically from top to bottom to obtain a line # Draw pixels vertically from top to bottom to obtain a line
textureSegmentEnd = 0 textureSegmentEnd = 0
@ -331,13 +355,21 @@ class Main:
texel = TEXTURES[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE] texel = TEXTURES[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE]
# Calculate color resulting from texture pixel value + shading # Calculate color resulting from texture pixel value + shading
color = sdl2.ext.Color(texel*shading,texel*shading,texel*shading,255) color = sdl2.ext.Color(texel*shading,texel*(shading/5),texel*(shading/5),255)
# Clipping # Clipping
lineEnd = textureSegmentEnd lineEnd = textureSegmentEnd
if textureSegmentEnd > lineOffset + lineHeight: if lineEnd > lineOffset + lineHeight:
textureSegmentEnd = lineOffset + lineHeight lineEnd = lineOffset + lineHeight
lineStart = textureSegmentStart
if lineStart < 0:
lineStart = 0
# Upscaling
lineStart = lineStart * RAYCAST_RESOLUTION_SCALING
lineEnd = lineEnd * RAYCAST_RESOLUTION_SCALING
# Draw segment # Draw segment
sdl2.ext.draw.line(self.raycastSurface, color, (i, int(textureSegmentStart), i, int(textureSegmentEnd))) for repeat in range(1, RAYCAST_RESOLUTION_SCALING + 1):
x = i * RAYCAST_RESOLUTION_SCALING + repeat
sdl2.ext.draw.line(self.raycastSurface, color, (x, int(lineStart), x, int(lineEnd)))
def dist(self, ax, ay, bx, by): def dist(self, ax, ay, bx, by):