-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycasting.py
More file actions
255 lines (205 loc) · 9.25 KB
/
raycasting.py
File metadata and controls
255 lines (205 loc) · 9.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import pygame
import math
import PIL as pil
import numpy as np
import random
import sys
"""
This code is really old and needs to be rewritten for scalability and optimization.
"""
FLAGS = pygame.SCALED | pygame.RESIZABLE
screen = pygame.display.set_mode((640, 360), FLAGS, vsync=1)
# GLOBAL CONSTANTS #
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
MAGENTA = (255, 0, 255)
MAP = ("1111133333333333"
"1000130000300033"
"1000100000300003"
"1011100000000333"
"2000020000300003"
"2020002033333303"
"2020000000300003"
"2222022330333333"
"3000000300000004"
"3000000300040004"
"3000000300040004"
"3303333344044044"
"4000000400005005"
"4004400000050005"
"4004400400000005"
"4444444455555555"
)
MAP_SIZE = 16
TILE_SIZE = 64
MAX_DEPTH = int(MAP_SIZE * TILE_SIZE)
FOV = math.pi / 3
HALF_FOV = FOV / 2
RAYS_CASTED = 128
ANGLE_STEP = FOV / RAYS_CASTED
PLAYER_SCALE = 20
# textures
RED_BRICK = pygame.image.load("W3D_red_bricks.png").convert()
BLUE_BRICK = pygame.image.load("W3D_blue_bricks.png").convert()
GRAY_BRICK = pygame.image.load("W3D_gray_bricks.png").convert()
CELL = pygame.image.load("W3D_blue_bricks_cell.png").convert()
WOOD = pygame.image.load("W3D_wood.png").convert()
SKY = pygame.image.load("Sky2.png").convert()
SKY = pygame.transform.scale(SKY, (640 * 4, 360))
# GLOBAL VARIABLES #
player_x = 2 * TILE_SIZE
player_y = 2 * TILE_SIZE
player_angle = math.pi
"""
Cast rays and render textures.
This process is slow because the texture's scale on the y axis gets to be way too much when the player gets too close to the wall.
I remember cropping the texture to the screen height but that didn't work.
"""
def cast_rays():
start_angle = player_angle - HALF_FOV
for ray in range(RAYS_CASTED):
for depth in range(MAX_DEPTH):
target_x = player_x - math.sin(start_angle) * depth
target_y = player_y + math.cos(start_angle) * depth
col = int(target_x / TILE_SIZE)
row = int(target_y / TILE_SIZE)
square = row * MAP_SIZE + col
if MAP[square] != "0":
depth *= math.cos(player_angle - start_angle)
wall_height = (screen_height * TILE_SIZE) / (depth + 0.0001) * 1.6
"""
if wall_height >= screen_height * 3.5:
wall_height = screen_height * 3.5
"""
# get whether on x or y and where relative to wall.
# tex_col_modifier is the x of the column of texture that renders for each ray.
if int((target_x + 1) % 64) == 0 or int((target_x + 1) % 64) == 1:
tex_col_modifier = int(target_y % 64)
elif int((target_y + 1) % 64) == 0 or int((target_y + 1) % 64) == 1:
tex_col_modifier = int(target_x % 64)
else:
tex_col_modifier = int(target_x % 64)
# different textures
if MAP[square] == "1":
red_brick_rescaled = pygame.transform.scale(RED_BRICK, (int(scale * 64), int(wall_height)))
screen.blit(red_brick_rescaled, (0 + ray * scale, (screen_height / 2) - wall_height / 2), (tex_col_modifier * int(scale), 0, int(scale), int(wall_height)))
elif MAP[square] == "2":
blue_brick_rescaled = pygame.transform.scale(BLUE_BRICK, (int(scale * 64), int(wall_height)))
screen.blit(blue_brick_rescaled, (0 + ray * scale, (screen_height / 2) - wall_height / 2), (tex_col_modifier * int(scale), 0, int(scale), int(wall_height)))
elif MAP[square] == "3":
gray_brick_rescaled = pygame.transform.scale(GRAY_BRICK, (int(scale * 64), int(wall_height)))
screen.blit(gray_brick_rescaled, (0 + ray * scale, (screen_height / 2) - wall_height / 2), (tex_col_modifier * int(scale), 0, int(scale), int(wall_height)))
elif MAP[square] == "4":
cell_rescaled = pygame.transform.scale(CELL, (int(scale * 64), int(wall_height)))
screen.blit(cell_rescaled, (0 + ray * scale, (screen_height / 2) - wall_height / 2), (tex_col_modifier * int(scale), 0, int(scale), int(wall_height)))
elif MAP[square] == "5":
wood_rescaled = pygame.transform.scale(WOOD, (int(scale * 64), int(wall_height)))
screen.blit(wood_rescaled, (0 + ray * scale, (screen_height / 2) - wall_height / 2), (tex_col_modifier * int(scale), 0, int(scale), int(wall_height)))
break
start_angle += ANGLE_STEP
# Detect if there was a collision in a certain direction
def collision(direction):
pygame.draw.circle(screen, GREEN, (int((player_x + PLAYER_SCALE) / 4), int(player_y / 4)), 1)
pygame.draw.circle(screen, GREEN, (int((player_x - PLAYER_SCALE) / 4), int(player_y / 4)), 1)
pygame.draw.circle(screen, GREEN, (int(player_x / 4), int((player_y - PLAYER_SCALE) / 4)), 1)
pygame.draw.circle(screen, GREEN, (int(player_x / 4), int((player_y + PLAYER_SCALE) / 4)), 1)
east = False
west = False
north = False
south = False
for x in range(MAP_SIZE):
for y in range(MAP_SIZE):
square = x + y * MAP_SIZE
if MAP[square] != "0":
tile = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
#pygame.draw.rect(screen, MAGENTA, tile)
if not east:
east = tile.collidepoint(player_x + PLAYER_SCALE, player_y) # direction 0
if not west:
west = tile.collidepoint(player_x - PLAYER_SCALE, player_y) # direction 1
if not north:
north = tile.collidepoint(player_x, player_y - PLAYER_SCALE) # direction 2
if not south:
south = tile.collidepoint(player_x, player_y + PLAYER_SCALE) # direction 3
if direction == 0:
return east
elif direction == 1:
return west
elif direction == 2:
return north
elif direction == 3:
return south
else:
raise Exception("Must be an integer between 0 and 3")
pygame.font.init()
FONT = pygame.font.SysFont("arial", 20)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen_width, screen_height = screen.get_size()
scale = screen_width / RAYS_CASTED
# define player rect
player_rect = pygame.Rect(player_x - PLAYER_SCALE / 2, player_y - PLAYER_SCALE / 2, PLAYER_SCALE, PLAYER_SCALE)
# key inputs
if player_angle <= -math.pi:
player_angle = math.pi
if player_angle >= math.pi * 3:
player_angle = math.pi
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
player_angle -= 0.1
if keys[pygame.K_d]:
player_angle += 0.1
if keys[pygame.K_w]:
if player_x - math.sin(player_angle) * 4 > player_x:
if not collision(0):
player_x -= math.sin(player_angle) * 4
if player_x - math.sin(player_angle) * 4 < player_x:
if not collision(1):
player_x -= math.sin(player_angle) * 4
if player_y + math.cos(player_angle) * 4 > player_y:
if not collision(3):
player_y += math.cos(player_angle) * 4
if player_y + math.cos(player_angle) * 4 < player_y:
if not collision(2):
player_y += math.cos(player_angle) * 4
if keys[pygame.K_s]:
if player_x + math.sin(player_angle) * 4 > player_x:
if not collision(0):
player_x += math.sin(player_angle) * 4
if player_x + math.sin(player_angle) * 4 < player_x:
if not collision(1):
player_x += math.sin(player_angle) * 4
if player_y - math.cos(player_angle) * 4 > player_y:
if not collision(3):
player_y -= math.cos(player_angle) * 4
if player_y - math.cos(player_angle) * 4 < player_y:
if not collision(2):
player_y -= math.cos(player_angle) * 4
# rendering everything
screen.fill(BLACK)
screen.blit(SKY, (0 - player_angle * 640, 0))
screen.blit(SKY, (0 - player_angle * 640 + 640 * 4, 0))
screen.blit(SKY, (0 - player_angle * 640 + 640 * 8, 0))
screen.blit(SKY, (0 - player_angle * 640 - 640 * 4, 0))
ground = pygame.Surface((640, 180))
ground.set_alpha(128)
ground.fill((32, 64, 0))
screen.blit(ground, (0, 180))
cast_rays()
clock.tick(60)
# get and display fps:
fps = clock.get_fps()
debug_text = FONT.render("fps: " + str(fps) + " | " + "Player Angle: " + str(player_angle), False, WHITE, BLACK)
debug_rect = debug_text.get_rect()
screen.blit(debug_text, debug_rect)
collision(0)
pygame.display.flip()
pygame.quit()
sys.exit()