# Simple Raycasting example v1 (Pygame)

Jan 18th, 2022
1,468
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import pygame
2. import sys
3. import math
4.
5. pygame.init()
6.
7. SCREEN_HEIGHT = 480
8. SCREEN_WIDTH = SCREEN_HEIGHT * 2
9. MAP_SIZE = 8
10. TILE_SIZE = ((SCREEN_WIDTH / 2) / MAP_SIZE)
11. MAX_DEPTH = int(MAP_SIZE * TILE_SIZE)
12. FOV = math.pi / 3
13. HALF_FOV = FOV / 2
14. CASTED_RAYS = 120
15. STEP_ANGLE = FOV / CASTED_RAYS
16. SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS
17.
18.
19. player_x = (SCREEN_WIDTH / 2) / 2
20. player_y = (SCREEN_WIDTH / 2) / 2
21. player_angle = math.pi
22.
23.
24. MAP = (
25.      '########'
26.      '#   ## #'
27.      '#      #'
28.      '#    ###'
29.      '##     #'
30.      '#   #  #'
31.      '#   #  #'
32.      '########'
33. )
34.
35.
36.
37. win = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
38.
39. pygame.display.set_caption("Raycasting by Network Skeleton")
40.
41. clock = pygame.time.Clock()
42.
43. def draw_map():
44.     for row in range(8):
45.         for col in range(8):
46.             square = row * MAP_SIZE + col
47.
48.             pygame.draw.rect(
49.                 win,
50.                 (200,200,200) if MAP[square] == '#' else (100,100,100),
51.                 (col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE - 2, TILE_SIZE - 2)
52.
53.                 )
54.     pygame.draw.circle(win, (255, 0, 0), (int(player_x),int(player_y)), 8)
55.     pygame.draw.line(win, (0,255,0),(player_x,player_y),(player_x - math.sin(player_angle) * 50,player_y + math.cos(player_angle) * 50),3)
56.     pygame.draw.line(win, (0,255,0),(player_x,player_y),(player_x - math.sin(player_angle - HALF_FOV) * 50,player_y + math.cos(player_angle - HALF_FOV) * 50),3)
57.     pygame.draw.line(win, (0,255,0),(player_x,player_y),(player_x - math.sin(player_angle + HALF_FOV) * 50,player_y + math.cos(player_angle + HALF_FOV) * 50),3)
58.
59.
60. def cast_rays():
61.     start_angle = player_angle - HALF_FOV
62.
63.     for ray in range(CASTED_RAYS):
64.         for depth in range(MAX_DEPTH):
65.             target_x = player_x - math.sin(start_angle) * depth
66.             target_y = player_y + math.cos(start_angle) * depth
67.             col = int(target_x / TILE_SIZE)
68.             row = int(target_y / TILE_SIZE)
69.
70.             square = row * MAP_SIZE + col
71.             (target_y / TILE_SIZE) * MAP_SIZE + target_x / TILE_SIZE
72.             if MAP[square] == '#':
73.                 pygame.draw.rect(win,(0,255,0),(col * TILE_SIZE,
74.                                                 row * TILE_SIZE,
75.                                                 TILE_SIZE - 2,
76.                                                 TILE_SIZE - 2))
77.                 pygame.draw.line(win, (255,255,0),(player_x,player_y),(target_x,target_y))
78.                 color = 50 / (1 + depth * depth * 0.0001)
79.
80.                 depth *= math.cos(player_angle - start_angle)
81.
82.                 wall_height = 21000 / (depth + 0.0001)
83.
84.                 if wall_height > SCREEN_HEIGHT: wall_height == SCREEN_HEIGHT
85.
86.                 pygame.draw.rect(win,(color,color,color), (SCREEN_HEIGHT + ray * SCALE,(SCREEN_HEIGHT / 2) - wall_height / 2,SCALE,wall_height))
87.
88.                 break
89.
90.         start_angle += STEP_ANGLE
91.
92. forward = True
93.
94.
95. while True:
96.     for event in pygame.event.get():
97.       if event.type == pygame.QUIT:
98.           pygame.quit()
99.           sys.exit(0)
100.
101.     col = int(player_x / TILE_SIZE)
102.     row = int(player_y / TILE_SIZE)
103.
104.     square = row * MAP_SIZE + col
105.     (player_y / TILE_SIZE) * MAP_SIZE + player_x / TILE_SIZE
106.     if MAP[square] == '#':
107.             if forward == True:
108.                 player_x -= -math.sin(player_angle) * 5
109.                 player_y -= math.cos(player_angle) * 5
110.             else:
111.                 player_x += -math.sin(player_angle) * 5
112.                 player_y += math.cos(player_angle) * 5
113.
114.
115.
116.     pygame.draw.rect(win,(0,0,0),(0,0,SCREEN_HEIGHT,SCREEN_HEIGHT))
117.
118.     pygame.draw.rect(win,(100,0,0),(480,SCREEN_HEIGHT / 2,SCREEN_HEIGHT,SCREEN_HEIGHT))
119.     pygame.draw.rect(win,(200,0,0),(480,-SCREEN_HEIGHT / 2,SCREEN_HEIGHT,SCREEN_HEIGHT))
120.
121.
122.     draw_map()
123.     cast_rays()
124.     keys = pygame.key.get_pressed()
125.
126.     if keys[pygame.K_LEFT]: player_angle -= 0.1
127.     if keys[pygame.K_RIGHT]: player_angle += 0.1
128.     if keys[pygame.K_UP]:
129.         forward = True
130.         player_x += -math.sin(player_angle) * 5
131.         player_y += math.cos(player_angle) * 5
132.     if keys[pygame.K_DOWN]:
133.         forward = False
134.         player_x -= -math.sin(player_angle) * 5
135.         player_y -= math.cos(player_angle) * 5
136.
137.     clock.tick(30)
138.
139.     fps = str(int(clock.get_fps()))
140.     font = pygame.font.SysFont('Monospace Regular', 30)
141.     textsurface = font.render(fps, False, (255,255,255))
142.     win.blit(textsurface,(0,0))
143.     pygame.display.flip()
144.
145.
146.
147.
148.
149.