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, 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,
@ -43,15 +43,27 @@ MAP = [
MAP_SIZE = 16
DOF = 2*MAP_SIZE # Depth Of Field
TEXTURE = [
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 1,
1, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
TEXTURES = [
[
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 0,
0, 1, 0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 1, 1, 1, 0,
0, 1, 1, 1, 0, 1, 1, 0,
0, 1, 1, 1, 1, 0, 1, 0,
0, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
[
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 0, 1, 1, 0, 1, 1,
1, 0, 1, 1, 1, 1, 0, 1,
0, 1, 1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 1, 1, 0,
1, 0, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1, 1, 0, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
],
]
TEXTURE_SIZE = 8
@ -178,7 +190,7 @@ class Main:
posX = i % MAP_SIZE * MAP_SCALE
posY = math.floor(i / MAP_SIZE) * MAP_SCALE
color = 0
if MAP[i] == 1:
if MAP[i] > 0:
color = 255
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1))
@ -194,6 +206,10 @@ class Main:
if rayAngle > math.pi * 2:
rayAngle = rayAngle - math.pi * 2
# Which map wall tiles have been hit by rayX and rayY
mapBlockHitX = 0
mapBlockHitY = 0
# Check horizontal lines
dof = 0 # Depth of field
if rayAngle == 0 or rayAngle == math.pi:
@ -223,6 +239,7 @@ class Main:
mapArrayPosition = mapY * MAP_SIZE + mapX
if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0:
dof = DOF # Hit the wall: we are done, no need to do other checks
mapBlockHitY = MAP[mapArrayPosition] # Save which map wall tile we reached
else:
# Didn't hit the wall: check successive horizontal line
rayX = rayX + xOffset
@ -264,6 +281,7 @@ class Main:
mapArrayPosition = mapY * MAP_SIZE + mapX
if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] != 0:
dof = DOF # Hit the wall: we are done, no need to do other checks
mapBlockHitX = MAP[mapArrayPosition] # Save which map wall tile we reached
else:
# Didn't hit the wall: check successive horizontal line
rayX = rayX + xOffset
@ -297,13 +315,15 @@ class Main:
textureSegmentEnd = textureSegmentStart + textureSegmentLength
# Obtain texture value in the pixel representing the current segment and calculate shading
if vertDist > horizDist:
texIndex = mapBlockHitY - 1 # The texture covering the selected map tile (0 is no texture, 1 is texture at TEXTURES[0] etc)
texColumn = int(rayX / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
shading = 127
else:
texIndex = mapBlockHitX - 1 # The texture covering the selected map tile
texColumn = int(rayY / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
shading = 255
texel = TEXTURE[texColumn + textureColumnPixel * TEXTURE_SIZE]
texel = TEXTURES[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE]
# Calculate color resulting from texture pixel value + shading
color = sdl2.ext.Color(texel*shading,texel*shading,texel*shading,255)
# Clipping