This repository was archived by the owner on Sep 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycaster.py
More file actions
390 lines (319 loc) · 15.1 KB
/
raycaster.py
File metadata and controls
390 lines (319 loc) · 15.1 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import array
import pygame
import math
from tkinter import *
# tutorial from https://lodev.org/cgtutor/raycasting.html
# constants
mapWidth = 24
mapHeight = 24
screenWidth = 900 #640
screenHeight = 600 #480
# world map
worldMap = [
[1, 1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0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 4, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 4, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 4, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 4, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 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]
]
# initialize pygame
pygame.init()
# initialize font
pygame.font.init()
# create the screen
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Python Raycaster")
# create game clock
clock = pygame.time.Clock()
def switch(number: int):
"""acts like a switch statement. switches the ints of the map with a color value to display.
Args:
number: the number to switch on
"""
switcher = {
1: (255, 0, 0), # red
2: (0, 255, 0), # green
3: (0, 0, 255), # blue
4: (255, 255, 255) # white
}
return switcher.get(number, pygame.Color("#ffff00")) # default is yellow
def stop():
pygame.quit()
quit()
def display_text(text: str, position: tuple, color: tuple):
"""displays text to the screen at the given position.
Args:
text: the text to display.
position: the position to display the text.
color: the color of the text.
"""
fpsFont = pygame.font.SysFont('Consolas',15)
screen.blit(fpsFont.render(text, False, color), position)
def display_text_centered(text: str, position: tuple, color: tuple, size: int, font: str):
"""displays text to the screen with the given parameters
Args:
text: the text to display
position: the position to display the text.
color: the color of the text.
size: the font size
font: the system font to use
"""
font_obj = pygame.font.SysFont(font, size)
rendered = font_obj.render(text, False, color)
rect = rendered.get_rect().width
screen.blit(rendered, (position[0] - rect // 2,position[1]))
return rendered
def get_text_object(text: str, color: tuple, fontSize: int, font: str):
font_obj = pygame.font.SysFont(font, fontSize)
return font_obj.render(text, False, color)
def game_intro():
intro = True
while intro:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("exiting...")
intro = False
stop()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
intro = False
display_text_centered(text="Python raycaster", position=(screenWidth // 2, 100), color=(200, 200, 200), size=30, font='Consolas')
display_text_centered(text="Press 'SPACE' to start", position=(screenWidth // 2, 200), color=(200, 40, 40), size=20, font='Consolas')
display_text_centered(text="WASD to move, left and right arrow keys to look", position=(screenWidth//2, 250), color=(50, 100, 100), size=16, font='Consolas')
copy_right_text = get_text_object(text="made by Sem van der Hoeven", color=(100, 100, 100), fontSize=12, font='Consolas')
rect = copy_right_text.get_rect()
screen.blit(copy_right_text, (0, screenHeight - rect.height))
pygame.display.update()
clock.tick(30)
def game_loop(clock, screen):
"""main game loop that runs the game.
Args:
clock: The pygame clock object.
screen: the pygame screen object.
"""
posX = 22.0 # x start position
posY = 12.0 # y start position
dirX = -1.0 # initial x of direction vector
dirY = 0.0 # initial y of direction vector
planeX = 0.0 # 2d raycaster version of camera plane x
planeY = 0.66 # 2d raycaster version of camera plane y
running = True
rotateLeft = False
rotateRight = False
moveForward = False
moveBackward = False
moveLeft = False
moveRight = False
debugMode = False
# main game loop
print("starting game...")
while running:
# update the next frame at 60 fps
ms = clock.tick(60) / 1000.0
moveSpeed = ms * 3.0
rotSpeed = ms * 3.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("exiting...")
del screen
del clock
running = False
if event.type == pygame.KEYDOWN:
if event.key == ord('a'):
moveLeft = True
if event.key == ord('d'):
moveRight = True
if event.key == ord('w'):
moveForward = True
if event.key == ord('s'):
moveBackward = True
if event.key == pygame.K_LEFT:
rotateLeft = True
if event.key == pygame.K_RIGHT:
rotateRight = True
if event.key == pygame.K_TAB:
debugMode = not debugMode
print("setting debug mode to" ,debugMode)
if event.type == pygame.KEYUP:
if event.key == ord('a'):
moveLeft = False
if event.key == ord('d'):
moveRight = False
if event.key == ord('w'):
moveForward = False
if event.key == ord('s'):
moveBackward = False
if event.key == pygame.K_LEFT:
rotateLeft = False
if event.key == pygame.K_RIGHT:
rotateRight = False
if running == False:
continue # if the user has pressed the quit key, stop the loop
# loop that goes over every x (vertical line)
screen.fill((0, 0, 0))
for x in range(screenWidth):
# calculate camera x, the x coordinate on the camera plane that the current
# x-coordinate represents.
# This way, the right side of the screen will get coordinate 1, center will get coordinate 0,
# and left will get coordinate -1
cameraX = 2 * x / float(screenWidth) - 1
rayDirX = dirX + planeX * cameraX
rayDirY = dirY + planeY * cameraX
# which box of the map we're in
mapX = int(posX)
mapY = int(posY)
# length of the ray from current position to next x or y side
sideDistX = 0.0 # (double)
sideDistY = 0.0 # (double)
# length of ray from one x or y-side to next x or y-side
# rayDirX and rayDirY can be 0, so then division by 0 would occur if
# we did only abs(1/rayDirX), so we need to check if it is 0
deltaDistX = 0 if rayDirY == 0 else (
1 if rayDirX == 0 else abs(1/rayDirX))
deltaDistY = 0 if rayDirX == 0 else (
1 if rayDirY == 0 else abs(1/rayDirY))
perpWallDist = 0.0 # used to calculate the length of the ray (double)
# what direction to step in x or y-direction (either +1 or -1 for positive or negative direction)
# if the ray direction has a negative x-component, stepX will be -1. If it has a positive x-component,
# it will be +1. If the x-component is 0 the value of stepX won't matter because it won't be used.
# same for stepY
stepX = 0 # (int)
stepY = 0 # (int)
hit = 0 # was there a wall hit?
side = 0 # (int) was a vertical or horizontal wall hit?
# calculate step and initial sideDist
# is the x-component of the ray is negative, sideDistX is the distance to the first vertical line to the left.
if rayDirX < 0:
# if the x-component of the ray is positive, sideDistX is the distance to the first vertical line to the richt.
# same for the y-component, but then to the first horizontal line to the top or bottom
stepX = -1
sideDistX = (posX - mapX) * deltaDistX
else:
stepX = 1
sideDistX = (mapX + 1.0 - posX) * deltaDistX
if rayDirY < 0:
stepY = -1
sideDistY = (posY - mapY) * deltaDistY
else:
stepY = 1
sideDistY = (mapY + 1.0 - posY) * deltaDistY
# actual DDA algorithm
while hit == 0: # while the ray has not hit a wall
# jump to the next map square, OR in x direction OR in y direction
if (sideDistX < sideDistY): # the ray is going morea horizontal than vertical
sideDistX += deltaDistX
mapX += stepX
side = 0
else:
sideDistY += deltaDistY
mapY += stepY
side = 1
# check if the ray has hit a wall
# if it is not 0, so the square does not contain a walkable square, so a wall
if worldMap[mapX][mapY] > 0:
hit = 1
# calculate the distance projected on camera direction (Euclidean distance will give fisheye effect!)
if side == 0: # vertical wall hit
perpWallDist = (mapX - posX + (1 - stepX) / 2) / rayDirX
else: # horizontal wall hit
perpWallDist = (mapY - posY + (1 - stepY) / 2) / rayDirY
# calculate the height of the line to draw on the screen
if (perpWallDist != 0):
lineHeight = int(screenHeight / perpWallDist)
# calculate the lowest and highest pixel to fill in current vertical stripe
drawStart = -lineHeight / 2 + screenHeight / 2
if drawStart < 0:
drawStart = 0
drawEnd = lineHeight / 2 + screenHeight / 2
if drawEnd >= screenHeight:
drawEnd = screenHeight - 1
color = switch(worldMap[mapX][mapY])
if side == 1:
color = (color[0] / 2, color[1] / 2, color[2] / 2)
pygame.draw.line(screen, color, (x, int(drawStart)),
(x, int(drawEnd)), 1)
if moveForward:
movePosX = int(posX + dirX * moveSpeed) # x position to move to next
movePosY = int(posY + dirY * moveSpeed) # y position to move to next
if worldMap[movePosX][movePosY] == 0:
posX += dirX * moveSpeed
posY += dirY * moveSpeed
if moveBackward:
movePosX = int(posX - dirX * moveSpeed)
movePosY = int(posY - dirY * moveSpeed)
if worldMap[movePosX][movePosY] == 0:
posX -= dirX * moveSpeed
posY -= dirY * moveSpeed
if moveLeft:
oldDirX = dirX
oldDirY = dirY
dirX = dirX * math.cos(math.pi/2) - dirY * math.sin(math.pi/2)
dirY = oldDirX * math.sin(math.pi/2) + dirY * math.cos(math.pi/2)
movePosX = int(posX + dirX * moveSpeed) # x position to move to next
movePosY = int(posY + dirY * moveSpeed) # y position to move to next
if worldMap[movePosX][movePosY] == 0:
posX += dirX * moveSpeed
posY += dirY * moveSpeed
dirX = oldDirX
dirY = oldDirY
if moveRight:
oldDirX = dirX
oldDirY = dirY
dirX = dirX * math.cos(-math.pi/2) - dirY * math.sin(-math.pi/2) # rotate 90 degrees to the right
dirY = oldDirX * math.sin(-math.pi/2) + dirY * math.cos(-math.pi/2) # so change the direction to 90 degrees
# apply the move, so move to the right
movePosX = int(posX + dirX * moveSpeed) # x position to move to next
movePosY = int(posY + dirY * moveSpeed) # y position to move to next
if worldMap[movePosX][movePosY] == 0:
posX += dirX * moveSpeed
posY += dirY * moveSpeed
# reset the direction vector because we still want to look forward
dirX = oldDirX
dirY = oldDirY
if rotateRight:
oldDirX = dirX
dirX = dirX * math.cos(-rotSpeed) - dirY * math.sin(-rotSpeed)
dirY = oldDirX * math.sin(-rotSpeed) + dirY * math.cos(-rotSpeed)
oldPlaneX = planeX
planeX = planeX * math.cos(-rotSpeed) - planeY * math.sin(-rotSpeed)
planeY = oldPlaneX * math.sin(-rotSpeed) + planeY * math.cos(-rotSpeed)
if rotateLeft:
oldDirX = dirX
dirX = dirX * math.cos(rotSpeed) - dirY * math.sin(rotSpeed)
dirY = oldDirX * math.sin(rotSpeed) + dirY * math.cos(rotSpeed)
oldPlaneX = planeX
planeX = planeX * math.cos(rotSpeed) - planeY * math.sin(rotSpeed)
planeY = oldPlaneX * math.sin(rotSpeed) + planeY * math.cos(rotSpeed)
textToRender = "FPS: " + str(int(1 // ms))
if debugMode: textToRender = textToRender + " posX: " + str(int(
posX)) + " posY: " + str(int(
posY)) + " dirX: " + str(dirX) + " dirY: " + str(dirY)
display_text(textToRender, (0, 0), (0, 255, 255))
copy_right_text = get_text_object(text="press TAB for debug info", color=(100, 100, 100), fontSize=12, font='Consolas')
rect = copy_right_text.get_rect()
screen.blit(copy_right_text, (0, screenHeight - rect.height))
# uodate the display
pygame.display.update()
game_intro()
game_loop(clock,screen)
stop()