import pygame import math pygame.init() # Constants SCREEN_WIDTH, SCREEN_HEIGHT = 1920, 1080 TILE_SIZE = 40 MAZE_WIDTH, MAZE_HEIGHT = 9999, 9999 MAZE = [ "#################" "# #", "# #", "# #", "# #", "#################" ] # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Player attributes player_pos = [1.5, 1.5] # Player starts in the middle of a tile player_angle = 0.0 # Player starts looking to the right # Create the screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Maze Game") # Game loop running = True clock = pygame.time.Clock() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False # Handle player input keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_angle -= 0.05 if keys[pygame.K_RIGHT]: player_angle += 0.05 if keys[pygame.K_w]: new_x = player_pos[0] + math.cos(player_angle) * 0.05 new_y = player_pos[1] + math.sin(player_angle) * 0.05 if MAZE[int(new_y)][int(new_x)] == ' ': player_pos[0] = new_x player_pos[1] = new_y if keys[pygame.K_s]: new_x = player_pos[0] - math.cos(player_angle) * 0.05 new_y = player_pos[1] - math.sin(player_angle) * 0.05 if MAZE[int(new_y)][int(new_x)] == ' ': player_pos[0] = new_x player_pos[1] = new_y # Clear the screen screen.fill(BLACK) # Draw the walls from the player's perspective for x in range(SCREEN_WIDTH): angle = player_angle - math.pi / 6 + (x / SCREEN_WIDTH) * (math.pi / 3) distance_to_wall = 0 hit_wall = False while not hit_wall and distance_to_wall < 10: distance_to_wall += 0.25 test_x = int(player_pos[0] + math.cos(angle) * distance_to_wall) test_y = int(player_pos[1] + math.sin(angle) * distance_to_wall) if test_x < 0 or test_x >= MAZE_WIDTH or test_y < 0 or test_y >= MAZE_HEIGHT or MAZE[test_y][test_x] == '#': hit_wall = True ceiling = SCREEN_HEIGHT // 2 - SCREEN_HEIGHT // distance_to_wall // 2 floor = SCREEN_HEIGHT - ceiling pygame.draw.line(screen, WHITE, (x, ceiling), (x, floor)) pygame.display.flip() clock.tick(30) pygame.quit()