Problem with code. Please help.

Blitz3D Forums/Blitz3D Beginners Area/Problem with code. Please help.

I'm trying to make a pong game from scratch, to get used to the blitz language. However, I'm stuck at the moment, because my bat would get stuck when it reaches the edge of the screen (I'm stopping the bat from going off-screen). Please help. Here's my full code:

Global screenx	= 640
Global screeny	= 480
Global upkey 	= 200
Global leftkey 	= 203
Global rightkey = 205
Global downkey 	= 208

Graphics screenx,screeny,16,2

;Set type for bat info
Type entityinfo
	Field x
	Field y
	Field width
	Field height
	Field xspeed#
	Field yspeed#
End Type

;Create Mybat instance
Mybat.entityinfo 	= New entityinfo
Mybat\x 			= 320
Mybat\y 			= 240
Mybat\width			= 100
Mybat\height		= 25

;Create Ball instance
Ball.entityinfo 	= New entityinfo
Ball\x				= 320
Ball\y				= 240
Ball\width			= 25
Ball\height			= 25

;Create bat and ball image
gfxBat 	= CreateImage(Mybat\width,Mybat\height)
gfxBall	= CreateImage(25,25)

;Draw bat and ball image
SetBuffer ImageBuffer(gfxBat)
	Color 114,108,219
	Rect 0,0,100,25,1
SetBuffer ImageBuffer(gfxBall)
	Color 114,108,219
	Oval 0,0,25,25,1

;Center the images
MidHandle gfxBat
MidHandle gfxBall

;Switch back to back buffer
SetBuffer BackBuffer()

;Game loops starts here
;================================
While Not KeyHit(1)
;================================
	;Clear screen
	Cls
	
	;Just to know x,y values of my bat
	Text 0,0,mybat\x
	Text 0,10,mybat\y
	
	;Draw my bat onto screen
	DrawImage gfxbat,mybat\x,mybat\y
	
	;Make sure bat is on screen
	If (Mybat\x > (Mybat\width)/2) And (Mybat\x < screenx-((Mybat\width)/2)) Then
		If leftkey()=1 Then
			Mybat\x = Mybat\x - 2
		EndIf
		If rightkey()=1 Then
			Mybat\x = Mybat\x + 2
		EndIf
	EndIf
	Flip	
Wend


;Just some functions
Function LeftKey()
	If KeyDown(leftkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function

Function RightKey()
	If KeyDown(rightkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function

Function UpKey()
	If KeyDown(upkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function

Function DownKey()
	If KeyDown(Downkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function