backrroms.py

Created by maelg0000

Created on April 01, 2026

3.91 KB


from kandinsky import *
from ion import *
from math import cos, sin, pi
from time import monotonic

# Paramètres écran
W, H = 320, 222
HALF_H = H // 2

# Paramètres joueur
player_x = 1.5
player_y = 1.5
player_angle = 0.0
FOV = pi / 3
HALF_FOV = FOV / 2
NUM_RAYS = 40  # RÉDUIT pour plus de FPS
MAX_DEPTH = 15
DELTA_ANGLE = FOV / NUM_RAYS
STRIP_WIDTH = W // NUM_RAYS  # 8 pixels par colonne

# Vitesses
MOVE_SPEED = 0.1
ROT_SPEED = 0.1

# Carte simplifiée
MAP = [
    [1,1,1,1,1,1,1,1],
    [1,0,0,0,0,0,0,1],
    [1,0,0,1,1,0,0,1],
    [1,0,0,0,0,0,0,1],
    [1,0,1,0,0,1,0,1],
    [1,0,0,0,0,0,0,1],
    [1,0,0,0,1,0,0,1],
    [1,1,1,1,1,1,1,1]
]

MAP_W = len(MAP[0])
MAP_H = len(MAP)

# Couleurs
COL_CEIL = color(50, 50, 40)
COL_FLOOR = color(80, 80, 70)
COL_WALL = color(220, 220, 150)

show_fps = False
fps_toggle_pressed = False

def cast_ray(angle):
    """Lance un rayon - VERSION OPTIMISÉE"""
    cos_a = cos(angle)
    sin_a = sin(angle)
    
    x, y = player_x, player_y
    
    for depth in range(MAX_DEPTH * 5):
        x += cos_a * 0.2
        y += sin_a * 0.2
        
        col = int(x)
        row = int(y)
        
        if col < 0 or col >= MAP_W or row < 0 or row >= MAP_H:
            return depth * 0.2
        
        if MAP[row][col] == 1:
            return depth * 0.2
    
    return MAX_DEPTH

def render():
    """Rendu 3D OPTIMISÉ"""
    ray_angle = player_angle - HALF_FOV
    
    for ray in range(NUM_RAYS):
        dist = cast_ray(ray_angle)
        
        # Correction fish-eye
        dist *= cos(player_angle - ray_angle)
        
        # Hauteur du mur
        if dist < 0.1:
            dist = 0.1
        wall_h = min(int(H / dist), H)
        
        wall_top = HALF_H - wall_h // 2
        wall_bot = HALF_H + wall_h // 2
        
        x_pos = ray * STRIP_WIDTH
        
        # Plafond
        if wall_top > 0:
            fill_rect(x_pos, 0, STRIP_WIDTH, wall_top, COL_CEIL)
        
        # Mur
        fill_rect(x_pos, wall_top, STRIP_WIDTH, wall_bot - wall_top, COL_WALL)
        
        # Sol
        if wall_bot < H:
            fill_rect(x_pos, wall_bot, STRIP_WIDTH, H - wall_bot, COL_FLOOR)
        
        ray_angle += DELTA_ANGLE

def handle_input():
    """Gestion des touches"""
    global player_x, player_y, player_angle, show_fps, fps_toggle_pressed
    
    dx = cos(player_angle) * MOVE_SPEED
    dy = sin(player_angle) * MOVE_SPEED
    
    if keydown(KEY_UP):
        new_x = player_x + dx
        new_y = player_y + dy
        if MAP[int(new_y)][int(new_x)] == 0:
            player_x, player_y = new_x, new_y
    
    if keydown(KEY_DOWN):
        new_x = player_x - dx
        new_y = player_y - dy
        if MAP[int(new_y)][int(new_x)] == 0:
            player_x, player_y = new_x, new_y
    
    if keydown(KEY_LEFT):
        new_x = player_x + dy
        new_y = player_y - dx
        if MAP[int(new_y)][int(new_x)] == 0:
            player_x, player_y = new_x, new_y
    
    if keydown(KEY_RIGHT):
        new_x = player_x - dy
        new_y = player_y + dx
        if MAP[int(new_y)][int(new_x)] == 0:
            player_x, player_y = new_x, new_y
    
    if keydown(21):
        player_angle -= ROT_SPEED
    
    if keydown(23):
        player_angle += ROT_SPEED
    
    if keydown(16):
        player_angle -= ROT_SPEED
    
    if keydown(22):
        player_angle += ROT_SPEED
    
    if keydown(12):
        if not fps_toggle_pressed:
            show_fps = not show_fps
            fps_toggle_pressed = True
    else:
        fps_toggle_pressed = False

# Boucle principale
fill_rect(0, 0, W, H, COL_FLOOR)
frame_count = 0
start_time = monotonic()

while True:
    handle_input()
    render()
    
    if show_fps:
        frame_count += 1
        elapsed = monotonic() - start_time
        if elapsed > 0:
            fps = int(frame_count / elapsed)
            draw_string("FPS:" + str(fps), 5, 5, color(255, 255, 0), COL_CEIL)
    
    if keydown(KEY_EXE):
        break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.