Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Copyright (c) 2023 Zeromega
- Drop a link or a Sub on one of my videos if this script help you, copy the link below
- https://www.youtube.com/channel/UCfqUJ4rmk6W-ZAjDtkBZ1CA?sub_confirmation=1
- """
- import pygame
- import random
- import math
- pygame.init()
- WIDTH, HEIGHT = 800, 600
- CELL_SIZE = 20
- FOV = math.pi / 3 # FOV 60
- HALF_FOV = FOV / 2
- NUM_RAYS = WIDTH // 1 # Ray casting...
- MINI_MAP_SIZE = 150
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- BLUE = (0, 0, 255)
- maze = [[1 for _ in range(15)] for _ in range(15)]
- def generate_maze(row, col):
- if maze[row][col] == 0:
- return
- maze[row][col] = 0
- directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
- random.shuffle(directions)
- for dr, dc in directions:
- new_row, new_col = row + 2 * dr, col + 2 * dc
- if 0 <= new_row < len(maze) and 0 <= new_col < len(maze[0]):
- generate_maze(new_row, new_col)
- generate_maze(0, 0)
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("First-Person Maze Viewer")
- player_x, player_y = 40, 40
- player_angle = 0
- player_radius = 8
- floor_color = BLACK
- sky_color = BLUE
- RADIO_FLOOR = 0
- RADIO_SKY = 1
- radio_group = pygame.sprite.Group()
- class RadioButton(pygame.sprite.Sprite):
- def __init__(self, text, pos_x, pos_y, value):
- super().__init__(radio_group)
- self.value = value
- self.image = pygame.Surface((20, 20))
- self.rect = self.image.get_rect()
- self.rect.topleft = (pos_x, pos_y)
- self.image.fill(WHITE)
- self.selected = False
- self.text = text
- self.font = pygame.font.Font(None, 20)
- self.update()
- def update(self):
- if self.selected:
- self.image.fill(RED)
- else:
- self.image.fill(WHITE)
- text_render = self.font.render(self.text, True, BLACK)
- self.image.blit(text_render, (25, 2))
- radio_floor = RadioButton("Floor", 20, HEIGHT - 40, RADIO_FLOOR)
- radio_sky = RadioButton("Sky", 20, HEIGHT - 20, RADIO_SKY)
- running = True
- pressed_keys = set()
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- pressed_keys.add(event.key)
- if event.type == pygame.KEYUP:
- pressed_keys.discard(event.key)
- player_speed = 1.5
- new_x = int(player_x + player_speed * math.cos(player_angle))
- new_y = int(player_y + player_speed * math.sin(player_angle))
- 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:
- player_x = new_x
- player_y = new_y
- if pygame.K_DOWN in pressed_keys:
- new_x = int(player_x - player_speed * math.cos(player_angle))
- new_y = int(player_y - player_speed * math.sin(player_angle))
- if 0 <= new_x < WIDTH and 0 <= new_y < HEIGHT and maze[new_y // CELL_SIZE][new_x // CELL_SIZE] == 0:
- player_x = new_x
- player_y = new_y
- turning_speed = math.pi / 60
- if pygame.K_LEFT in pressed_keys:
- player_angle -= turning_speed
- if pygame.K_RIGHT in pressed_keys:
- player_angle += turning_speed
- screen.fill(sky_color)
- for ray in range(NUM_RAYS):
- ray_angle = player_angle - HALF_FOV + (ray / NUM_RAYS) * FOV
- distance_to_wall = 0
- hit_wall = False
- while not hit_wall and distance_to_wall < 300:
- test_x = int(player_x + distance_to_wall * math.cos(ray_angle))
- test_y = int(player_y + distance_to_wall * math.sin(ray_angle))
- if test_x < 0 or test_x >= WIDTH or test_y < 0 or test_y >= HEIGHT:
- hit_wall = True
- distance_to_wall = 300
- elif maze[test_y // CELL_SIZE][test_x // CELL_SIZE] == 1:
- hit_wall = True
- distance_to_wall += 5
- ceiling = HEIGHT // 2 - HEIGHT // distance_to_wall
- floor = HEIGHT - ceiling
- shade = int(255 * (1 - distance_to_wall / 300))
- pygame.draw.line(screen, (shade, shade, shade), (ray, ceiling), (ray, floor), 2)
- pygame.draw.rect(screen, floor_color, (0, HEIGHT // 2, WIDTH, HEIGHT // 2))
- line_color1 = (max(floor_color[0] + 10, 0), max(floor_color[1] + 10, 0), max(floor_color[2] + 10, 0))
- line_color2 = (max(floor_color[0] - 10, 0), max(floor_color[1] - 10, 0), max(floor_color[2] - 10, 0))
- for y in range(HEIGHT // 2, HEIGHT, 10):
- pygame.draw.line(screen, line_color1, (0, y), (WIDTH, y), 1)
- y += 5
- pygame.draw.line(screen, line_color2, (0, y), (WIDTH, y), 1)
- radio_group.update()
- radio_group.draw(screen)
- pygame.display.flip()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement