Advertisement
zeromega64twenty

An attempt at one script 3d game

Aug 27th, 2023 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | Gaming | 0 0
  1. """
  2. Copyright (c) 2023 Zeromega
  3. Drop a link or a Sub on one of my videos if this script help you, copy the link below
  4. https://www.youtube.com/channel/UCfqUJ4rmk6W-ZAjDtkBZ1CA?sub_confirmation=1
  5. """
  6.  
  7. import pygame
  8. import random
  9. import math
  10.  
  11. pygame.init()
  12.  
  13. WIDTH, HEIGHT = 800, 600
  14. CELL_SIZE = 20
  15. FOV = math.pi / 3  # FOV 60
  16. HALF_FOV = FOV / 2
  17. NUM_RAYS = WIDTH // 1  # Ray casting...
  18. MINI_MAP_SIZE = 150
  19.  
  20. WHITE = (255, 255, 255)
  21. BLACK = (0, 0, 0)
  22. RED = (255, 0, 0)
  23. BLUE = (0, 0, 255)
  24.  
  25. maze = [[1 for _ in range(15)] for _ in range(15)]
  26.  
  27. def generate_maze(row, col):
  28.     if maze[row][col] == 0:
  29.         return
  30.     maze[row][col] = 0
  31.     directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
  32.     random.shuffle(directions)
  33.     for dr, dc in directions:
  34.         new_row, new_col = row + 2 * dr, col + 2 * dc
  35.         if 0 <= new_row < len(maze) and 0 <= new_col < len(maze[0]):
  36.             generate_maze(new_row, new_col)
  37.  
  38. generate_maze(0, 0)
  39.  
  40. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  41. pygame.display.set_caption("First-Person Maze Viewer")
  42.  
  43. player_x, player_y = 40, 40
  44. player_angle = 0
  45. player_radius = 8
  46.  
  47. floor_color = BLACK
  48. sky_color = BLUE
  49.  
  50. RADIO_FLOOR = 0
  51. RADIO_SKY = 1
  52.  
  53. radio_group = pygame.sprite.Group()
  54.  
  55. class RadioButton(pygame.sprite.Sprite):
  56.     def __init__(self, text, pos_x, pos_y, value):
  57.         super().__init__(radio_group)
  58.         self.value = value
  59.         self.image = pygame.Surface((20, 20))
  60.         self.rect = self.image.get_rect()
  61.         self.rect.topleft = (pos_x, pos_y)
  62.         self.image.fill(WHITE)
  63.         self.selected = False
  64.         self.text = text
  65.         self.font = pygame.font.Font(None, 20)
  66.         self.update()
  67.  
  68.     def update(self):
  69.         if self.selected:
  70.             self.image.fill(RED)
  71.         else:
  72.             self.image.fill(WHITE)
  73.         text_render = self.font.render(self.text, True, BLACK)
  74.         self.image.blit(text_render, (25, 2))
  75.  
  76. radio_floor = RadioButton("Floor", 20, HEIGHT - 40, RADIO_FLOOR)
  77. radio_sky = RadioButton("Sky", 20, HEIGHT - 20, RADIO_SKY)
  78.  
  79. running = True
  80. pressed_keys = set()
  81.  
  82. while running:
  83.     for event in pygame.event.get():
  84.         if event.type == pygame.QUIT:
  85.             running = False
  86.  
  87.         if event.type == pygame.KEYDOWN:
  88.             pressed_keys.add(event.key)
  89.         if event.type == pygame.KEYUP:
  90.             pressed_keys.discard(event.key)
  91.  
  92.  
  93.     player_speed = 1.5
  94.     new_x = int(player_x + player_speed * math.cos(player_angle))
  95.     new_y = int(player_y + player_speed * math.sin(player_angle))
  96.  
  97.     if pygame.K_UP in pressed_keys and 0 <= new_x < WIDTH and 0 <= new_y < HEIGHT and maze[new_y // CELL_SIZE][new_x // CELL_SIZE] == 0:
  98.         player_x = new_x
  99.         player_y = new_y
  100.     if pygame.K_DOWN in pressed_keys:
  101.         new_x = int(player_x - player_speed * math.cos(player_angle))
  102.         new_y = int(player_y - player_speed * math.sin(player_angle))
  103.         if 0 <= new_x < WIDTH and 0 <= new_y < HEIGHT and maze[new_y // CELL_SIZE][new_x // CELL_SIZE] == 0:
  104.             player_x = new_x
  105.             player_y = new_y
  106.  
  107.     turning_speed = math.pi / 60
  108.     if pygame.K_LEFT in pressed_keys:
  109.         player_angle -= turning_speed
  110.     if pygame.K_RIGHT in pressed_keys:
  111.         player_angle += turning_speed
  112.  
  113.     screen.fill(sky_color)
  114.  
  115.     for ray in range(NUM_RAYS):
  116.         ray_angle = player_angle - HALF_FOV + (ray / NUM_RAYS) * FOV
  117.         distance_to_wall = 0
  118.         hit_wall = False
  119.  
  120.         while not hit_wall and distance_to_wall < 300:
  121.             test_x = int(player_x + distance_to_wall * math.cos(ray_angle))
  122.             test_y = int(player_y + distance_to_wall * math.sin(ray_angle))
  123.  
  124.             if test_x < 0 or test_x >= WIDTH or test_y < 0 or test_y >= HEIGHT:
  125.                 hit_wall = True
  126.                 distance_to_wall = 300
  127.             elif maze[test_y // CELL_SIZE][test_x // CELL_SIZE] == 1:
  128.                 hit_wall = True
  129.  
  130.             distance_to_wall += 5
  131.  
  132.         ceiling = HEIGHT // 2 - HEIGHT // distance_to_wall
  133.         floor = HEIGHT - ceiling
  134.  
  135.         shade = int(255 * (1 - distance_to_wall / 300))
  136.  
  137.         pygame.draw.line(screen, (shade, shade, shade), (ray, ceiling), (ray, floor), 2)
  138.  
  139.     pygame.draw.rect(screen, floor_color, (0, HEIGHT // 2, WIDTH, HEIGHT // 2))
  140.  
  141.     line_color1 = (max(floor_color[0] + 10, 0), max(floor_color[1] + 10, 0), max(floor_color[2] + 10, 0))
  142.     line_color2 = (max(floor_color[0] - 10, 0), max(floor_color[1] - 10, 0), max(floor_color[2] - 10, 0))
  143.     for y in range(HEIGHT // 2, HEIGHT, 10):
  144.         pygame.draw.line(screen, line_color1, (0, y), (WIDTH, y), 1)
  145.         y += 5
  146.         pygame.draw.line(screen, line_color2, (0, y), (WIDTH, y), 1)
  147.  
  148.     radio_group.update()
  149.     radio_group.draw(screen)
  150.     pygame.display.flip()
  151.  
  152. pygame.quit()
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement