program CircleMoving;
uses SwinGame, sgTypes;
procedure Main();
var
x, y, CIRCLE_RADIUS: Single; screenColour: Color;
begin
OpenGraphicsWindow('Character Moving', 800, 600);
Delay(5000);
x := 400;
y := 300;
CIRCLE_RADIUS := 150;
screenColour := ColorWhite;
repeat
clearScreen(screenColour);
fillCircle(ColorGreen, x, y, CIRCLE_RADIUS);
RefreshScreen(60);
Processevents();
if KeyDown(LeftKey) and (x - CIRCLE_RADIUS < ScreenWidth()) then
begin
x := x - 1;
end;
if KeyDown(RightKey) and (x + CIRCLE_RADIUS < ScreenWidth()) then
begin
x := x + 1;
end;
if KeyDown(UpKey) and (y - CIRCLE_RADIUS < ScreenHeight()) then
begin
y := y - 1;
end;
if KeyDown(DownKey) and (y + CIRCLE_RADIUS < ScreenHeight()) then
begin
y := y + 1;
end;
until WindowCloseRequested();
end;
begin
Main();
end.
在我运行代码的那一刻,它可以正常工作,然后当我将圆圈向左移动并向上移动它离开窗口时,我不希望这种情况发生。我希望它受到限制,以便圆圈停止,一旦碰到边缘就不会再进一步了。我希望它在所有方面都是相同的,所以当我向任何方向移动圆圈时它将停在窗口的边缘。将圆圈向右移动向下,但向左和向上移动不会。我认为问题出在从第22行开始的if语句。如何更改我的代码以解决此问题?