Array Conundrumn

BlitzMax Forums/BlitzMax Beginners Area/Array Conundrumn

Wether this is down to a problem with arrays or not is a mystery. Which I'd love help resolving and getting it up and running properly if it's possible.

As you'll see that it doesnt clear out and goes into infinity and beyond. Where as it should gradually bring each of the ripples to an end.
'Reflection Effect
'by Thumbz - 
'50% working converted from B2D To BetaMax 

Strict

Const XRES=320
Const YRES=240

Graphics XRES,YRES,16
SeedRnd MilliSecs()
HideMouse

Local  ImageName:Timage	=CreateImage(XRES,YRES,1,DYNAMICIMAGE)
Global Screen:Timage	=CreateImage(XRES,YRES,1,DYNAMICIMAGE)
Global Locked:TPixmap

Global Background[XRES,YRES  ]
Global ReflectMap[XRES,YRES,2]

Global Spread = 40

'
' make a background
'
For Local a=0 To 300
	
	SetColor Rand(64,128),64,Rand(64,196)
	
	DrawRect Rand(XRES),Rand(YRES),Rand(128),Rand(128)
	
Next

GrabImage ImageName,0,0 
'
' store colour info.
'
Locked=LockImage(ImageName)

For Local y=0 To YRES-1
	For Local x=0 To XRES-1
		
		Background[x,y]=ReadPixel(Locked,x,y) 
		
	Next
Next
UnlockImage (ImageName)


'
' the main program loop.
'
While Not KeyHit(KEY_ESCAPE)
Cls
	
	UserControl			()
	UpdateReflections	()	
	
	DrawImage Screen,0,0
	
	SetColor 255,255,255
	Plot MouseX(),MouseY()
	
	FlushMem()
Flip
Wend



Function UserControl()

If MouseHit(1) Then 
	ReflectMap[MouseX(),MouseY(),1] = ReflectMap[MouseX(),MouseY(),1] - 100
End If

End Function



Function UpdateReflections()

Local i
Local x,y

Local rgb
Local alp
Local red
Local grn
Local blu

Local Reflect

Local nx
Local ny

Locked=LockImage(Screen)

For y = 1 To YRES - 2
	For x = 1 To XRES - 2
	
		Reflect 			= (( ReflectMap[x - 1,y,1] + ReflectMap[x + 1,y,1] + ReflectMap[x,y - 1,1] ..

							   + ReflectMap[x,y + 1,1] ) / 2) - ReflectMap[x,y,2]
          
		ReflectMap[x,y,2] 	= Reflect - (Reflect / Spread)
     
	Next
Next


For y = 1 To YRES - 2
	For x = 1 To XRES - 2
     
		Local xdis = (( ReflectMap[x, y + 1, 2] - ReflectMap[x,y - 1,2] ) - ReflectMap[x,y,2] ) / 4
    	Local ydis = (( ReflectMap[x + 1, y, 2] - ReflectMap[x - 1,y,2] ) - ReflectMap[x,y,2] ) / 4
     
		Local wx = x + xdis
     	Local wy = y + ydis
     
		If wx <= 0 		Then wx = 1
     	If wx >= XRES 	Then wx = XRES - 1
     	If wy <= 0 		Then wy = 1
     	If wy >= YRES 	Then wy = YRES - 1
     
		rgb = Background[wx,wy]
		
		alp	=255
		red	=(rgb & $FF0000)	/$FFFF
		grn =(rgb & $FF00)		/$FF
		blu	=(rgb & $FF)
		
		rgb = (alp Shl 24) | (red Shl 16) | (grn Shl 8) | (Blu Shl 0)
		
		'
		' draw to screen 
	    '
	 	If rgb<>0 Then
           		
			WritePixel (Locked,x,y,rgb)
		
		End If
		'sort / temp
		Reflect		 		= ReflectMap[x,y,2]
    	ReflectMap[x,y,2] 	= ReflectMap[x,y,1]
		ReflectMap[x,y,1] 	= Reflect
	
	Next
	'
    ' All Clear 
	'
	ReflectMap[0	   ,y,1] = 0
	ReflectMap[0	   ,y,2] = 0
	ReflectMap[XRES - 1,y,1] = 0
	ReflectMap[XRES - 1,y,2] = 0

Next

For x = 0 To XRES - 1
	
	ReflectMap[x,0		 ,1] = 0
	ReflectMap[x,0	   	 ,2] = 0
    ReflectMap[x,YRES - 1,1] = 0
    ReflectMap[x,YRES - 1,2] = 0

Next

UnlockImage (Screen)

End Function


if you could help that would be amazing and thank you,
THUMBZ