"Sample code 0: Simulate a particle in a box"
L=1.0;
coordinates[1]={0,0};
coordinates[2]={0,L};
coordinates[3]={L,L};
coordinates[4]={L,0};
coordinates[5]={0,0};
"First, generate the box"
glbox=Graphics[Line[{Table[coordinates[n],{n,1,5}]}],AspectRatio->1]

"Generate random location and initial velocities" 
v0=1.0;
v0x=v0*(2*Random[]-1);v0y=v0*(2*Random[]-1);
Deltax=Random[];Deltay=Random[];
{x[0] ,y[0]}={Deltax*L,Deltay*L};
lp=ListPlot[{{x[0] ,y[0]}},PlotRange->{{0,L},{0,L}},AspectRatio->1];
Show[{glbox,lp}]

"Modify the previous code to make the cooridinate t-dependent"
"Specify the parameter Deltat and itlast. When the Do loop runs for itlast time, the simulation will stop" 

Ctimes=5;(*roughly the number of times the particle hitting the walls throughtout the simulation times*)
timescale=2L/v0;
Deltat=0.01*timescale;
itlast=Ctimes*timescale/Deltat;
(*tlast=Deltat*itlast;*)

"Now, evolve the cooridnates. "
xp=x[0];yp=y[0];
Do[
xn=xp+v0x*Deltat;(*the next x-position, xn, is calculated based on the previous postions xp*)
yn=yp+v0y*Deltat;(*the next y-position, yn, is calculated based on the previous postions yp*)
x[it]=xn;y[it]=yn;(*coordinates at time it are rocorded *)

(* choose boundary condition. Choose only one *)

(** Fixed boundary conditions **)
If[x[it]<0,x[it]=-x[it];v0x=-v0x];(*end if *)
If[y[it]<0,y[it]=-y[it];v0y=-v0y];(*end if *)
If[x[it]>L,x[it]=2L-x[it];v0x=-v0x];(*end if *)
If[y[it]>L,y[it]=2L-y[it];v0y=-v0y];(*end if *)
(** end of Fixed boundary conditions **)

(*
(** Periodic boundary conditions **)
If[x[it]<0,x[it]=L+x[it]];(*end if *)
If[y[it]<0,y[it]=L+y[it]];(*end if *)
If[x[it]>L,x[it]=x[it]-L];(*end if *)
If[y[it]>L,y[it]=y[it]-L];(*end if *)
(** end Periodic boundary conditions **)
*)

(* keep the record of xp, yp so that it can be used in the begining of the loop *)
xp=x[it];yp=y[it];
(* end of keep the record of xp, yp *)

,{it,1,itlast}];(*End Do*)

"Output the simulation"
llp[it_]:=ListPlot[{{x[it],y[it]}},PlotRange->{{0,L},{0,L}},AspectRatio->1,PlotMarkers->{"●",Medium}];
Manipulate[
(*Show[{glbox,llp[it]}],*)
Show[{glbox,llp[it]}],
{it,0,itlast,1}]

"Output the simulation using a better alternative: Graphics[Point]"
coordxy[it_]:={x[it],y[it]};

GP[it_]:=Graphics[{PointSize[0.025],Point[coordxy[it]]},
PlotRange->{{0,L},{0,L}},AspectRatio->1];
Manipulate[
(*Show[{glbox,llp[it]}],*)
Show[{glbox,GP[it]}],
{it,0,itlast,1}]