program Squares;
uses GraphABC;
type
Square = class
private
colorSquare: color;
x, y: integer;
rotate: boolean;
health: integer;
public
keyLeft, keyRight, keyJump, keyAttack: integer;
moveLeft, moveRight, Jump, Attack: integer;
stateOfSquare, timeOfJump: integer;
constructor Create(colorSquare: color; x, y, health: integer; rotate: boolean; keyLeft, keyRight, keyJump, keyAttack: integer);
procedure DrawSquare;
procedure moveSquare;
procedure freeFall;
procedure doJump;
procedure doAttack;
//метод, группирующий процедуры в объект
procedure Start;
begin
if Jump = 1 then stateOfSquare := 1;
if timeOfJump = 5 then begin stateOfSquare := 0; timeOfJump := 0; end;
if stateOfSquare = 0 then
freeFall
else
begin
doJump;
timeOfJump := timeOfJump + 1;
end;
moveSquare;
DrawSquare;
end;
end;
//инструкции для создания объекта
constructor Square.Create(colorSquare: color; x, y, health: integer; rotate: boolean; keyLeft, keyRight, keyJump, keyAttack: integer);
begin
Self.colorSquare := colorSquare;
Self.x := x; Self.y := y;
Self.rotate := rotate;
Self.health := health;
Self.keyLeft := keyLeft; Self.keyRight := keyRight;
Self.keyJump := keyJump; Self.keyAttack := keyAttack;
end;
//вырисовывание объекта + очки жизней
procedure Square.DrawSquare;
begin
SetPenWidth(3);
SetPenColor(colorSquare);
DrawRectangle(x, y, x + 10, y + 10);
SetFontColor(colorSquare);
TextOut(x, y - 25, health);
end;
//движение влево и вправо по оси
procedure Square.moveSquare;
begin
if moveLeft = 1 then x := x - 1;
if moveRight = 1 then x := x + 1;
end;
//свободное падение
procedure Square.freeFall;
begin
if y + 10 + 1 < WindowHeight then y := y + 1;
end;
//прыжок
procedure Square.doJump;
begin
y := y - 1;
end;
//атака
procedure Square.doAttack;
begin
end;
//описание объектов
var
redSquare: Square := new Square(clRed, 0, WindowHeight - 200, 9, true, vk_A, vk_D, vk_W, vk_ShiftKey);
blueSquare: Square := new Square(clBlue, 0, WindowHeight - 200, 9, true, vk_Left, vk_Right, vk_Up, vk_LShiftKey);
//нажатие клавиши
procedure KeyDown(Key: integer);
begin
//событие нажатия клавиш управления redSquare
if Key = redSquare.keyLeft then redSquare.moveLeft := 1;
if Key = redSquare.keyRight then redSquare.moveRight := 1;
if Key = redSquare.keyJump then redSquare.Jump := 1;
if Key = redSquare.keyAttack then redSquare.Attack := 1;
//событие нажатия клавиш управления blueSquare
if Key = blueSquare.keyLeft then blueSquare.moveLeft := 1;
if Key = blueSquare.keyRight then blueSquare.moveRight := 1;
if Key = blueSquare.keyJump then blueSquare.Jump := 1;
if Key = blueSquare.keyAttack then blueSquare.Attack := 1;
end;
//отжатие клавиши
procedure KeyUp(Key: integer);
begin
//событие отжатия клавиш управления redSquare
if Key = redSquare.keyLeft then redSquare.moveLeft := 0;
if Key = redSquare.keyRight then redSquare.moveRight := 0;
if Key = redSquare.keyJump then redSquare.Jump := 0;
if Key = redSquare.keyAttack then redSquare.Attack := 0;
//событие отжатия клавиш управления blueSquare
if Key = blueSquare.keyLeft then blueSquare.moveLeft := 0;
if Key = blueSquare.keyRight then blueSquare.moveRight := 0;
if Key = blueSquare.keyJump then blueSquare.Jump := 0;
if Key = blueSquare.keyAttack then blueSquare.Attack := 0;
end;
begin
LockDrawing;
SetWindowSize(500, 300);
redSquare.x := WindowWidth div 4;
blueSquare.x := (WindowWidth div 4) * 3;
OnKeyDown := KeyDown;
OnKeyUp := KeyUp;
while (true) do
begin
clearwindow;
Square.Start;
redraw;
end;
end.