fromkandinskyimport*fromionimport*frommathimportcos,sin,pifromtimeimportmonotonic# Paramètres écran
W,H=320,222HALF_H=H//2# Paramètres joueur
player_x=1.5player_y=1.5player_angle=0.0FOV=pi/3HALF_FOV=FOV/2NUM_RAYS=40# RÉDUIT pour plus de FPS
MAX_DEPTH=15DELTA_ANGLE=FOV/NUM_RAYSSTRIP_WIDTH=W//NUM_RAYS# 8 pixels par colonne
# Vitesses
MOVE_SPEED=0.1ROT_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=Falsefps_toggle_pressed=Falsedefcast_ray(angle):"""Lance un rayon - VERSION OPTIMISÉE"""cos_a=cos(angle)sin_a=sin(angle)x,y=player_x,player_yfordepthinrange(MAX_DEPTH*5):x+=cos_a*0.2y+=sin_a*0.2col=int(x)row=int(y)ifcol<0orcol>=MAP_Worrow<0orrow>=MAP_H:returndepth*0.2ifMAP[row][col]==1:returndepth*0.2returnMAX_DEPTHdefrender():"""Rendu 3D OPTIMISÉ"""ray_angle=player_angle-HALF_FOVforrayinrange(NUM_RAYS):dist=cast_ray(ray_angle)# Correction fish-eye
dist*=cos(player_angle-ray_angle)# Hauteur du mur
ifdist<0.1:dist=0.1wall_h=min(int(H/dist),H)wall_top=HALF_H-wall_h//2wall_bot=HALF_H+wall_h//2x_pos=ray*STRIP_WIDTH# Plafond
ifwall_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
ifwall_bot<H:fill_rect(x_pos,wall_bot,STRIP_WIDTH,H-wall_bot,COL_FLOOR)ray_angle+=DELTA_ANGLEdefhandle_input():"""Gestion des touches"""globalplayer_x,player_y,player_angle,show_fps,fps_toggle_presseddx=cos(player_angle)*MOVE_SPEEDdy=sin(player_angle)*MOVE_SPEEDifkeydown(KEY_UP):new_x=player_x+dxnew_y=player_y+dyifMAP[int(new_y)][int(new_x)]==0:player_x,player_y=new_x,new_yifkeydown(KEY_DOWN):new_x=player_x-dxnew_y=player_y-dyifMAP[int(new_y)][int(new_x)]==0:player_x,player_y=new_x,new_yifkeydown(KEY_LEFT):new_x=player_x+dynew_y=player_y-dxifMAP[int(new_y)][int(new_x)]==0:player_x,player_y=new_x,new_yifkeydown(KEY_RIGHT):new_x=player_x-dynew_y=player_y+dxifMAP[int(new_y)][int(new_x)]==0:player_x,player_y=new_x,new_yifkeydown(21):player_angle-=ROT_SPEEDifkeydown(23):player_angle+=ROT_SPEEDifkeydown(16):player_angle-=ROT_SPEEDifkeydown(22):player_angle+=ROT_SPEEDifkeydown(12):ifnotfps_toggle_pressed:show_fps=notshow_fpsfps_toggle_pressed=Trueelse:fps_toggle_pressed=False# Boucle principale
fill_rect(0,0,W,H,COL_FLOOR)frame_count=0start_time=monotonic()whileTrue:handle_input()render()ifshow_fps:frame_count+=1elapsed=monotonic()-start_timeifelapsed>0:fps=int(frame_count/elapsed)draw_string("FPS:"+str(fps),5,5,color(255,255,0),COL_CEIL)ifkeydown(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:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.