raycast.py

Created by maelg0000

Created on March 15, 2026

2.36 KB


from kandinsky import fill_rect as F,color as C,draw_string as D
from math import cos,sin
from ion import keydown as K
from time import monotonic as T

SH=222;SH2=111
CW=5;NR=64
FOV=1.047;HF=FOV*0.5

def wall(x,y):
 x=int(x);y=int(y)
 mx=x%7;my=y%7
 if mx==0 or my==0:return 1
 h=(x*73+y*179)%29
 if h<4:return 1
 return 0

FI=[cos(FOV*(i/NR-0.5))for i in range(NR)]
px=3.5;py=3.5;pa=0.0
pt=0;pz=0.0;vz=0.0
sf=0;pk=0

CC=C(45,40,25)
CG=C(40,35,20)

def ok(x,y):
 m=0.3
 if wall(x-m,y-m) or wall(x+m,y-m):return 0
 if wall(x-m,y+m) or wall(x+m,y+m):return 0
 return 1

def run():
 global px,py,pa,pt,pz,vz,sf,pk
 da=FOV/NR
 cd=cos(da);sd=sin(da)
 lt=T();fps=0
 
 while 1:
  nt=T()
  dt=nt-lt
  if dt>0.001:fps=int(1.0/dt)
  lt=nt
  
  hz=SH2+pt+int(pz*60)
  ca=cos(pa-HF);sa=sin(pa-HF)
  
  for i in range(NR):
   rc=ca;rs=sa
   if-1e-5<rc<1e-5:rc=1e-5
   if-1e-5<rs<1e-5:rs=1e-5
   
   mx=int(px);my=int(py)
   dx=abs(1.0/rc);dy=abs(1.0/rs)
   if rc<0:sx=-1;ex=(px-mx)*dx
   else:sx=1;ex=(mx+1.0-px)*dx
   if rs<0:sy=-1;ey=(py-my)*dy
   else:sy=1;ey=(my+1.0-py)*dy
   
   si=0
   for _ in range(40):
    if ex<ey:ex+=dx;mx+=sx;si=0
    else:ey+=dy;my+=sy;si=1
    if wall(mx,my):break
   
   d=(ex-dx)if si==0 else(ey-dy)
   if d<0.1:d=0.1
   d*=FI[i]
   
   wh=int(SH/d)
   if wh<1:wh=1
   tp=hz-(wh>>1);bt=tp+wh
   ct=0 if tp<0 else tp
   cb=SH if bt>SH else bt
   
   x=i*CW
   
   # Plafond simple
   if ct>0:F(x,0,CW,ct,CC)
   
   # Mur
   f=1.0/(1+d*d*0.025)
   if si:f*=0.65
   if cb>ct:
    F(x,ct,CW,cb-ct,
     C(int(30+170*f),int(25+160*f),int(5+115*f)))
   
   # Sol simple
   if cb<SH:F(x,cb,CW,SH-cb,CG)
   
   nc=ca*cd-sa*sd
   sa=sa*cd+ca*sd
   ca=nc
  
  kn=K(12)
  if kn and not pk:sf=not sf
  pk=kn
  if sf:
   D(str(fps),1,1,C(255,255,0),C(0,0,0))
  
  # Déplacement
  sp=0.12;c=cos(pa);s=sin(pa)
  
  if K(1):  # UP
   nx=px+sp*c;ny=py+sp*s
   if ok(nx,py):px=nx
   if ok(px,ny):py=ny
  if K(2):  # DOWN
   nx=px-sp*c;ny=py-sp*s
   if ok(nx,py):px=nx
   if ok(px,ny):py=ny
  if K(0):  # LEFT
   nx=px+sp*s;ny=py-sp*c
   if ok(nx,py):px=nx
   if ok(px,ny):py=ny
  if K(3):  # RIGHT
   nx=px-sp*s;ny=py+sp*c
   if ok(nx,py):px=nx
   if ok(px,ny):py=ny
  
  # Rotation caméra
  if K(21):pa-=0.08
  if K(23):pa+=0.08
  
  # Look up/down
  if K(16):pt=min(pt+6,80)
  if K(22):pt=max(pt-6,-80)
  
  # Saut
  if K(17)and pz<=0:vz=0.15
  pz+=vz;vz-=0.02
  if pz<=0:pz=0.0;vz=0.0

run()

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.