You could make it like IF Y<0 THEN Y=0 that way Y is set to 0 if it hits the top of the screen and for the bottom of the screen IF Y>240 THEN Y=240