Advertisement
EdmundC

doom

Sep 2nd, 2024 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. import pygame
  2. import math
  3.  
  4. # Initialize Pygame
  5. pygame.init()
  6.  
  7. # Screen dimensions
  8. WIDTH, HEIGHT = 640, 480
  9. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  10.  
  11. # Map setup
  12. MAP = [
  13.     "########",
  14.     "#......#",
  15.     "#.##...#",
  16.     "#......#",
  17.     "#..###.#",
  18.     "#......#",
  19.     "#......#",
  20.     "########"
  21. ]
  22. MAP_WIDTH = len(MAP[0])
  23. MAP_HEIGHT = len(MAP)
  24.  
  25. # Player setup
  26. player_x, player_y = 3.5, 3.5  # Start in the center of the map
  27. player_angle = 0
  28. FOV = math.pi / 3  # Field of view (60 degrees)
  29. MOVE_SPEED = 0.1
  30. ROTATE_SPEED = 0.05
  31.  
  32. # Raycasting
  33. def cast_ray(angle):
  34.     sin_a = math.sin(angle)
  35.     cos_a = math.cos(angle)
  36.  
  37.     for depth in range(1, 20):
  38.         target_x = player_x + depth * cos_a
  39.         target_y = player_y + depth * sin_a
  40.  
  41.         if MAP[int(target_y)][int(target_x)] == '#':
  42.             return depth, target_x, target_y
  43.  
  44.     return None, None, None
  45.  
  46. # Main loop
  47. running = True
  48. while running:
  49.     for event in pygame.event.get():
  50.         if event.type == pygame.QUIT:
  51.             running = False
  52.  
  53.     # Player movement
  54.     keys = pygame.key.get_pressed()
  55.  
  56.     if keys[pygame.K_a]:  # Rotate left
  57.         player_angle -= ROTATE_SPEED
  58.     if keys[pygame.K_d]:  # Rotate right
  59.         player_angle += ROTATE_SPEED
  60.  
  61.     if keys[pygame.K_w]:  # Move forward
  62.         new_x = player_x + math.cos(player_angle) * MOVE_SPEED
  63.         new_y = player_y + math.sin(player_angle) * MOVE_SPEED
  64.         if MAP[int(new_y)][int(new_x)] != '#':  # Check for collision
  65.             player_x, player_y = new_x, new_y
  66.  
  67.     if keys[pygame.K_s]:  # Move backward
  68.         new_x = player_x - math.cos(player_angle) * MOVE_SPEED
  69.         new_y = player_y - math.sin(player_angle) * MOVE_SPEED
  70.         if MAP[int(new_y)][int(new_x)] != '#':  # Check for collision
  71.             player_x, player_y = new_x, new_y
  72.  
  73.     # Drawing the scene
  74.     screen.fill((0, 0, 0))
  75.  
  76.     for column in range(WIDTH):
  77.         angle = player_angle - FOV / 2 + FOV * column / WIDTH
  78.         depth, _, _ = cast_ray(angle)
  79.         if depth:
  80.             wall_height = HEIGHT / (depth * math.cos(angle - player_angle))
  81.             color = 255 / (1 + depth * depth * 0.1)
  82.             pygame.draw.rect(screen, (color, color, color),
  83.                              (column, HEIGHT // 2 - wall_height // 2, 1, wall_height))
  84.  
  85.     pygame.display.flip()
  86.  
  87. pygame.quit()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement