Pascal графіка тип руху - технічний форум

Так, наприклад, можна задати цикл руху до тих пір, поки не буде натиснуто кнопку. У циклі в змінну зчитувати випадкове значення напрямку руху, яке буде служити умовою для вкладеного циклу переміщення об'єкта на випадкові dx і dy. Після досягнення меж екрану внутрішній цикл переривається і управління передається зовнішньому циклу, в якому знову генерується значення для спрямування.

Приклад використання методу PutImage. Реалізовано рух тільки по горизонталі.

У мене ось як вийшло

Program RGZ;
Uses Crt, Graph;
Const FigureHight = 55;
FigureWidth = 35;
FigureColor = red;
FigureBackground = blue;
MenuText: array [1..3] of string = ( 'Svobodni', 'Upravlenie', 'Exit');
var select, i, gd, gm: integer;
konez: boolean;
ch: char;
procedure Gagarin (x, y, color, background: integer);
begin
setcolor (color);
line (x, y, x + 40, y + 25);
line (x, y, x + 20, y + 25);
line (x + 20, y + 25, x + 100, y + 25);
line (x + 20, y + 55, x + 100, y + 55);
line (x + 20, y + 25, x + 20, y + 55);
line (x + 100, y + 55, x + 130, y + 40);
line (x + 100, y + 25, x + 130, y + 40);
line (x + 20, y + 55, x, y + 80);
line (x, y + 80, x + 40, y + 55);
circle (x + 90, y + 40, 10);
circle (x + 70, y + 40, 10);
circle (x + 50, y + 40, 10);
end;
procedure Upravlenie;
var x, y, ckorost: integer;
begin
x: = getmaxx div 2;
y: = getmaxy div 2;
ckorost: = 1;
repeat
Gagarin (x, y, FigureColor, FigureBackground);
ch: = readkey;
Gagarin (x, y, black, black);
case ch of
# 72: if (y-ckorost)> 10 then dec (y, ckorost);
# 80: if (y + ckorost + FigureWidth)<480 then inc(y,ckorost);
# 75: if (x-ckorost)> 10 then dec (x, ckorost);
# 77: if (x + ckorost + FigureHight)<640 then inc(x,ckorost);
'+': If ckorost<60 then inc(ckorost);
'-': if ckorost> 0 then dec (ckorost);
end;
until (ch = # 27) or (ch = # 13);
end;
Procedure Svobodnoe;
Var X, Y, DX, DY. Integer;
Ch. Char;
Begin
X: = Random (605);
Y: = Random (480);
DX: = 1;
DY: = 1;
While TRUE do
Begin
ClearDevice;
Gagarin (X, Y, FigureColor, FigureBackground);
if Y> = 480 then DY: = -DY;
if Y <= 0 then DY := -DY;
if X> = 640 then DX: = -DX;
if X <= 0 then DX := -DX;
X: = X + DX;
Y: = Y + DY;
Delay (1000);
if KeyPressed then Begin
Ch: = ReadKey;
if Ch = # 27 then Break
End
End
End;
Begin
gd: = detect;
initgraph (gd, gm, '');
randomize;
konez: = false;
repeat
cleardevice; select: = 1;
repeat
for i: = 1 to 3 do begin
if i = select then setcolor (Red) else setcolor (green);
outtextxy ((getmaxx div 2) -50, (getmaxy div 2) -50 + i * 16, MenuText [i]);
end;
ch: = readkey;
case ch of
# 72: dec (select);
# 80: inc (select);
end;
if select<1 then select:=3;
if select> 3 then select: = 1;
if ch = # 27 then begin
select: = 3;
ch: = # 13;
end;
until (ch = # 13);
cleardevice;
case select of
1: Svobodnoe;
2: Upravlenie;
3: konez: = true;
end;
until konez;
closegraph;
End.

Схожі статті