[Python] So, I broke my raytracing engine with a basic sprite [closed]

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    yeah so I need some help with my code(Shown below). when i try to make it work it kind of does this. [​IMG]. if at all possible can someone please tell me what I did wrong or if I need to rework something entrirely. also the cameral only moves to the right no matter what you use. there is no error that occurs but it will not do as i wish no matter what I do. i am lost and I need lots of help here.

    I just need to know how to Fix my code so that it works enought so that you can see what is happening.

    sorry that this was closed by stack overflow for not even giving people time to look at the files and see what I did wrong.

    below is my main.py

    import pygame
    import math
    from maze import Maze

    # Constants
    SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
    MAZE_WIDTH, MAZE_HEIGHT = 41, 41
    BLOCK_SIZE = 40
    SPEED = 0.1
    FOV = math.pi / 3 # Field of view
    RAY_COUNT = 120 # Number of rays to cast for rendering
    MAX_DEPTH = 20 # Rendering distance
    MOUSE_SENSITIVITY = 0.00009 # Reduced sensitivity

    class Game:
    def __init__(self):
    pygame.init()
    self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("3D Maze Game")
    self.clock = pygame.time.Clock()
    self.maze = Maze(MAZE_WIDTH, MAZE_HEIGHT).generate()
    self.player_x, self.player_y = 1.5, 1.5
    self.angle = math.pi / 4 # Player facing direction

    # Load wall texture
    self.wall_texture = pygame.image.load("wall.png")
    self.wall_texture = pygame.transform.scale(self.wall_texture, (SCREEN_WIDTH // RAY_COUNT, SCREEN_HEIGHT))

    # Mouse control settings
    pygame.mouse.set_visible(False)
    pygame.event.set_grab(True)

    def cast_rays(self):
    """ Raycasting with proper fade-to-black shading and textures """
    for ray in range(RAY_COUNT):
    ray_angle = self.angle - (FOV / 2) + (FOV * ray / RAY_COUNT)
    depth = 0

    while depth < MAX_DEPTH:
    test_x = int(self.player_x + math.cos(ray_angle) * depth)
    test_y = int(self.player_y + math.sin(ray_angle) * depth)

    if self.maze[test_y][test_x] == 1:
    break # Stop at walls

    depth += 0.05

    # Fade into black instead of gray
    line_height = min(SCREEN_HEIGHT, int(SCREEN_HEIGHT / (depth * 0.1)))
    shade = max(0, 255 - min(255, depth * 20)) # DARKER FADE

    pygame.draw.rect(self.screen, (int(shade), int(shade), int(shade)),
    (ray * (SCREEN_WIDTH // RAY_COUNT), SCREEN_HEIGHT // 2 - line_height // 2,
    SCREEN_WIDTH // RAY_COUNT, line_height))

    self.screen.blit(pygame.transform.scale(self.wall_texture, (SCREEN_WIDTH // RAY_COUNT, line_height)),
    (ray * (SCREEN_WIDTH // RAY_COUNT), SCREEN_HEIGHT // 2 - line_height // 2))

    def handle_input(self):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]: # Move forward
    new_x = self.player_x + math.cos(self.angle) * SPEED
    new_y = self.player_y + math.sin(self.angle) * SPEED
    if self.maze[int(new_y)][int(new_x)] == 0:
    self.player_x, self.player_y = new_x, new_y

    if keys[pygame.K_s]: # Move backward
    new_x = self.player_x - math.cos(self.angle) * SPEED
    new_y = self.player_y - math.sin(self.angle) * SPEED
    if self.maze[int(new_y)][int(new_x)] == 0:
    self.player_x, self.player_y = new_x, new_y

    if keys[pygame.K_a]: # Strafe left
    new_x = self.player_x - math.sin(self.angle) * SPEED
    new_y = self.player_y + math.cos(self.angle) * SPEED
    if self.maze[int(new_y)][int(new_x)] == 0:
    self.player_x, self.player_y = new_x, new_y

    if keys[pygame.K_d]: # Strafe right
    new_x = self.player_x + math.sin(self.angle) * SPEED
    new_y = self.player_y - math.cos(self.angle) * SPEED
    if self.maze[int(new_y)][int(new_x)] == 0:
    self.player_x, self.player_y = new_x, new_y

    def handle_events(self):
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    return False

    elif event.type == pygame.MOUSEMOTION:
    self.angle += event.rel[0] * MOUSE_SENSITIVITY # Reduce jitter

    return True

    def run(self):
    while self.handle_events():
    self.clock.tick(60)
    self.handle_input()
    self.screen.fill((0, 0, 0))
    self.cast_rays()
    pygame.display.flip()

    pygame.quit()

    if __name__ == "__main__":
    Game().run()


    below is my maze generation file known simply as maze.py.

    import random

    class Maze:
    def __init__(self, width, height):
    self.width = width
    self.height = height
    self.grid = [[1] * width for _ in range(height)] # Walls everywhere initially

    def generate(self):
    def carve_maze(x, y):
    """ Recursive maze generation with branching paths """
    directions = [(0,1), (1,0), (0,-1), (-1,0)]
    random.shuffle(directions)

    for dx, dy in directions:
    nx, ny = x + dx * 2, y + dy * 2
    if 1 <= nx < self.width - 1 and 1 <= ny < self.height - 1 and self.grid[ny][nx] == 1:
    self.grid[y + dy][x + dx] = 0
    self.grid[ny][nx] = 0
    carve_maze(nx, ny)

    # Ensure proper maze size for complexity
    self.grid[10][10] = 0
    carve_maze(1, 1)

    return self.grid

    if __name__ == "__main__":
    maze = Maze(41, 41) # Larger maze size for a full experience
    maze_map = maze.generate()
    for row in maze_map:
    print("".join(['#' if cell else ' ' for cell in row]))


    i wanted my code to show for the walls[​IMG] and instead it shows[​IMG] if you need more information on my code just ask and I will get it to you as soon as possible

    Continue reading...

Compartilhe esta Página