Advertisement
EdmundC

3dgame

Sep 1st, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 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. clock = pygame.time.Clock()
  11.  
  12. # Colors
  13. LIGHT_BLUE = (173, 216, 230)
  14. DARK_GRAY = (64, 64, 64)
  15. BLACK = (0, 0, 0)
  16.  
  17. # Updated Map layout
  18. MAP = [
  19.     [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, 1, 1, 1, 1, 1],
  20.     [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1],
  21.     [1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
  22.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
  23.     [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  24.     [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
  25.     [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1],
  26.     [1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1],
  27.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
  28.     [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, 1, 1, 1, 1, 1]
  29. ]
  30.  
  31. TILE_SIZE = 32
  32. MAP_WIDTH = len(MAP[0])
  33. MAP_HEIGHT = len(MAP)
  34.  
  35. # Player settings
  36. player_x = TILE_SIZE * 1.5
  37. player_y = TILE_SIZE * 1.5
  38. player_angle = 0
  39. player_speed = 3
  40.  
  41. # Raycasting settings
  42. FOV = math.pi / 3
  43. HALF_FOV = FOV / 2
  44. NUM_RAYS = 120
  45. MAX_DEPTH = 800
  46. DELTA_ANGLE = FOV / NUM_RAYS
  47. DISTANCE_TO_PROJ_PLANE = (WIDTH / 2) / math.tan(HALF_FOV)
  48. SCALE = WIDTH // NUM_RAYS
  49.  
  50. # Load wall image
  51. try:
  52.     woodwall_img = pygame.image.load('woodwall.png')
  53. except pygame.error as e:
  54.     print(f"Error loading image: {e}")
  55.     pygame.quit()
  56.     exit()
  57.  
  58. def check_collision(x, y):
  59.     """Check if a position collides with a wall."""
  60.     map_x = int(x / TILE_SIZE)
  61.     map_y = int(y / TILE_SIZE)
  62.     if 0 <= map_x < MAP_WIDTH and 0 <= map_y < MAP_HEIGHT:
  63.         return MAP[map_y][map_x] == 1
  64.     return False
  65.  
  66. def draw_walls():
  67.     for ray in range(NUM_RAYS):
  68.         ray_angle = player_angle - HALF_FOV + ray * DELTA_ANGLE
  69.         for depth in range(MAX_DEPTH):
  70.             target_x = player_x + depth * math.cos(ray_angle)
  71.             target_y = player_y + depth * math.sin(ray_angle)
  72.  
  73.             map_x = int(target_x / TILE_SIZE)
  74.             map_y = int(target_y / TILE_SIZE)
  75.  
  76.             if MAP[map_y][map_x] == 1:
  77.                 depth *= math.cos(player_angle - ray_angle)
  78.                 wall_height = 21000 / (depth + 0.0001)
  79.                 wall_top = HEIGHT // 2 - wall_height // 2
  80.  
  81.                 # Scale the wall image to fit the wall height
  82.                 scaled_wall_img = pygame.transform.scale(woodwall_img, (SCALE, int(wall_height)))
  83.                
  84.                 # Draw the wall by tiling the image horizontally
  85.                 for i in range(ray * SCALE, (ray + 1) * SCALE, scaled_wall_img.get_width()):
  86.                     screen.blit(scaled_wall_img, (i, wall_top))
  87.                
  88.                 # Draw the floor below the wall
  89.                 pygame.draw.rect(screen, DARK_GRAY,
  90.                                  (ray * SCALE, wall_top + wall_height, SCALE, HEIGHT - (wall_top + wall_height)))
  91.                
  92.                 break
  93.  
  94. def move_player():
  95.     global player_x, player_y, player_angle
  96.     keys = pygame.key.get_pressed()
  97.  
  98.     dx = 0
  99.     dy = 0
  100.     if keys[pygame.K_w]:
  101.         dx = player_speed * math.cos(player_angle)
  102.         dy = player_speed * math.sin(player_angle)
  103.     if keys[pygame.K_s]:
  104.         dx = -player_speed * math.cos(player_angle)
  105.         dy = -player_speed * math.sin(player_angle)
  106.    
  107.     new_x = player_x + dx
  108.     new_y = player_y + dy
  109.    
  110.     if not check_collision(new_x, player_y):
  111.         player_x = new_x
  112.     if not check_collision(player_x, new_y):
  113.         player_y = new_y
  114.  
  115.     if keys[pygame.K_a]:
  116.         player_angle -= 0.05
  117.     if keys[pygame.K_d]:
  118.         player_angle += 0.05
  119.  
  120. # Main loop
  121. running = True
  122. while running:
  123.     for event in pygame.event.get():
  124.         if event.type == pygame.QUIT:
  125.             running = False
  126.  
  127.     screen.fill((0, 0, 0))
  128.     move_player()
  129.     draw_walls()
  130.  
  131.     pygame.display.flip()
  132.     clock.tick(60)
  133.  
  134. pygame.quit()
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement