SetDirectory[NotebookDirectory[]];
L=1.0;Nlast=1000;np=100;
xl=-L/2;xr=L/2;yl=-L/2;yr=L/2;
dr=0.01L;
(*initialisation*)
For[j=1,j≤np,j++,
m[j]=1.0;
x[j,0]=RandomReal[{xl,xr}];
y[j,0]=RandomReal[{yl,yr}];
vx[j]=RandomReal[{-1,1}];
vy[j]=RandomReal[{-1,1}];
];
(*
m[1]=1.0;
m[2]=10.0;
x[1,0]=xl;x[2,0]=xr;
y[1,0]=yl;y[2,0]=yr;
vx[1]=1;vx[2]=-1;
vy[1]=1;vy[2]=-1;
*)
vave=Sqrt[Sum[(vx[j]^2+vy[j]^2),{j,1,np}]/np];
T=vave*(2L);tlast=10T;
Deltat=tlast/Nlast;
itlast=IntegerPart[tlast/Deltat];
it=0; 
(*end of initialisatino*)

While[it≤itlast,
it=it+1;
For[j=1,j≤np,j++,
x[j,it]=x[j,it-1]+vx[j]*Deltat;
y[j,it]=y[j,it-1]+vy[j]*Deltat;
(*Print[{j,it,x[j,it],y[j,it]}];*)
];(*end for j*)

(*check for collision*)
For[j=1,j≤np,j++ ,
For[k=1+j,k≤np,k++ ,
If[Sqrt[(x[j,it]-x[k,it])^2+(y[j,it]-y[k,it])^2]≤3*dr,
vjorigx=vx[j];
vx[j]=((m[j]-m[k])/(m[j]+m[k])) vx[j]+(2 m[k]/(m[j]+m[k]))*vx[k];
vx[k]=(2 m[j]/(m[j]+m[k])) vjorigx+((m[k]-m[j])/(m[j]+m[k])) vx[k];
vjorigy=vy[j];
vy[j]=((m[j]-m[k])/(m[j]+m[k])) vy[j]+(2 m[k]/(m[j]+m[k]))*vy[k];
vy[k]=(2 m[j]/(m[j]+m[k])) vjorigy+((m[k]-m[j])/(m[j]+m[k])) vy[k];
(*end if*)

(*revert to original positions if collision happens*)
x[k,it]=x[k,it-1];
y[k,it]=y[k,it-1];
];(*end for k*)
(*end of revert to original positions if collision happens*)];
];(*end for j*)
(*end check for collision*)

For[j=1,j≤np,j++,
(*boundary condition*)
If[x[j,it]>xr,vx[j]=-vx[j]];(*end if *)
If[x[j,it]<xl,vx[j]=-vx[j]];(*end if *)
If[y[j,it]>yr,vy[j]=-vy[j]];(*end if *)
If[y[j,it]<yl,vy[j]=-vy[j]];(*end if *)
];(*end for j*)
(*end of boundary condition*)

];(*end while*)

Manipulate[
Graphics[
{
Table[{PointSize->Large,Red,Point[{x[q,it],y[q,it]}]},{q,1,np}]
(*{PointSize->Large,Red,Point[{x[1,it],y[1,it]}]},
{PointSize->Large,Blue,Point[{x[2,it],y[2,it]}]}
*)
},PlotRange->{{1.05xl,1.05xr},{1.05yl,1.05yr}},Frame->True
]
,{it,0,itlast,1}
]

it=200;
Manipulate[
ListPlot[Table[{x[q,it],y[q,it]},{q,1,np}],PlotRange->{{1.1xl,1.1xr},{1.1yl,1.1yr}},AspectRatio->1,Frame->True],{it,1,itlast,1}]

it=963;
ListPlot[Table[{x[q,it],y[q,it]},{q,1,np}],PlotRange->{{1.1xl,1.1xr},{1.1yl,1.1yr}},AspectRatio->1,Frame->True]

1.1xr

Table[{x[q,it],y[q,it]},{q,1,np}]