LINUX.ORG.RU

История изменений

Исправление Nxx, (текущая версия) :

Короче, вот.

https://filetransfer.io/data-package/6Ln8xiXb#link

Просто сказал, что корабль привязан к точке резинкой и имеет массу.

program SpaceshipGame;

uses
  GraphABC;

const
  WindowWidth = 800;
  WindowHeight = 600;
  PlayerSpeed = 5;
  BulletSpeed = 10;
  NumStars = 100;
  NumEnemies = 10;
  SpringConstant = 0.05;
  DampingFactor = 0.9;

type
  TVector2 = record
    X, Y: Double;
  end;

  TEnemy = record
    Position: TVector2;
    Velocity: TVector2;
    Active: Boolean;
  end;

  TBullet = record
    Position: TVector2;
    Velocity: TVector2;
  end;

var
  PlayerPosition: TVector2;
  PlayerVelocity: TVector2;
  TargetPosition: TVector2;
  Enemies: array [0..NumEnemies - 1] of TEnemy;
  Bullets: array of TBullet;
  Stars: array [0..NumStars - 1] of TVector2;
  IsSpacePressed: Boolean;

procedure DrawShip(Position: TVector2);
begin
  SetPenColor(clRed);
  Line(Round(Position.X), Round(Position.Y - 10), Round(Position.X - 10), Round(Position.Y + 10));
  Line(Round(Position.X - 10), Round(Position.Y + 10), Round(Position.X + 10), Round(Position.Y + 10));
  Line(Round(Position.X + 10), Round(Position.Y + 10), Round(Position.X), Round(Position.Y - 10));
end;

procedure DrawTarget(Position: TVector2);
begin
  SetPenColor(clYellow);
  Circle(Round(Position.X), Round(Position.Y), 5);
end;

procedure DrawEnemies;
var
  i: Integer;
begin
  SetPenColor(clGreen);
  for i := 0 to High(Enemies) do
    if Enemies[i].Active then
      Rectangle(Round(Enemies[i].Position.X - 5), Round(Enemies[i].Position.Y - 5),
                Round(Enemies[i].Position.X + 5), Round(Enemies[i].Position.Y + 5));
end;

procedure DrawBullets;
var
  i: Integer;
begin
  SetPenColor(clRed);
  for i := 0 to High(Bullets) do
    Line(Round(Bullets[i].Position.X), Round(Bullets[i].Position.Y),
         Round(Bullets[i].Position.X + Bullets[i].Velocity.X * 5),
         Round(Bullets[i].Position.Y + Bullets[i].Velocity.Y * 5));
end;

procedure DrawStars;
var
  i: Integer;
begin
  SetPenColor(clWhite);
  for i := 0 to High(Stars) do
    Ellipse(Round(Stars[i].X), Round(Stars[i].Y), Round(Stars[i].X + 2), Round(Stars[i].Y + 2));
end;

procedure UpdatePlayer;
var
  SpringForce, Acceleration: TVector2;
begin
  SpringForce.X := (TargetPosition.X - PlayerPosition.X) * SpringConstant;
  SpringForce.Y := (TargetPosition.Y - PlayerPosition.Y) * SpringConstant;

  Acceleration.X := SpringForce.X;
  Acceleration.Y := SpringForce.Y;

  PlayerVelocity.X := PlayerVelocity.X * DampingFactor + Acceleration.X;
  PlayerVelocity.Y := PlayerVelocity.Y * DampingFactor + Acceleration.Y;

  PlayerPosition.X := PlayerPosition.X + PlayerVelocity.X;
  PlayerPosition.Y := PlayerPosition.Y + PlayerVelocity.Y;
end;

procedure UpdateEnemies;
var
  i: Integer;
begin
  for i := 0 to High(Enemies) do
  begin
    if Enemies[i].Active then
    begin
      Enemies[i].Position.X := Enemies[i].Position.X + Enemies[i].Velocity.X;
      Enemies[i].Position.Y := Enemies[i].Position.Y + Enemies[i].Velocity.Y;

      // Keep enemies within the field of view
      if (Enemies[i].Position.X < -10) then
        Enemies[i].Position.X := WindowWidth + 10;
      if (Enemies[i].Position.X > WindowWidth + 10) then
        Enemies[i].Position.X := -10;
      if (Enemies[i].Position.Y < -10) then
        Enemies[i].Position.Y := WindowHeight + 10;
      if (Enemies[i].Position.Y > WindowHeight + 10) then
        Enemies[i].Position.Y := -10;
    end;
  end;
end;

procedure UpdateBullets;
var
  i, j: Integer;
begin
  for i := 0 to High(Bullets) do
  begin
    Bullets[i].Position.X := Bullets[i].Position.X + Bullets[i].Velocity.X;
    Bullets[i].Position.Y := Bullets[i].Position.Y + Bullets[i].Velocity.Y;

    // Check for collisions with enemies
    for j := 0 to High(Enemies) do
    begin
      if Enemies[j].Active and
         (Abs(Bullets[i].Position.X - Enemies[j].Position.X) < 10) and
         (Abs(Bullets[i].Position.Y - Enemies[j].Position.Y) < 10) then
      begin
        Enemies[j].Active := False;
        Bullets[i].Position.X := -100; // Move the bullet off-screen
        Break;
      end;
    end;
  end;
end;

procedure MouseMove(x, y, mb: Integer);
begin
  TargetPosition.X := x;
  TargetPosition.Y := y;
end;

procedure KeyDown(Key: Integer);
begin
  if Key = vk_Space then
  begin
    IsSpacePressed := True;
    SetLength(Bullets, Length(Bullets) + 1);
    Bullets[High(Bullets)].Position := PlayerPosition;
    Bullets[High(Bullets)].Velocity.X := (TargetPosition.X - PlayerPosition.X) / BulletSpeed;
    Bullets[High(Bullets)].Velocity.Y := (TargetPosition.Y - PlayerPosition.Y) / BulletSpeed;
  end;
end;

procedure KeyUp(Key: Integer);
begin
  if Key = vk_Space then
    IsSpacePressed := False;
end;

begin
  SetWindowSize(WindowWidth, WindowHeight);
  SetWindowCaption('Spaceship Game');
  SetBrushColor(clBlack);
  ClearWindow(clBlack);
  PlayerPosition.X := WindowWidth / 2;
  PlayerPosition.Y := WindowHeight / 2;
  TargetPosition := PlayerPosition;

  for var i := 0 to High(Stars) do
  begin
    Stars[i].X := Random(WindowWidth);
    Stars[i].Y := Random(WindowHeight);
  end;

  for var i := 0 to High(Enemies) do
  begin
    Enemies[i].Position.X := Random(WindowWidth);
    Enemies[i].Position.Y := Random(WindowHeight);
    Enemies[i].Velocity.X := Random(11) - 5;
    Enemies[i].Velocity.Y := Random(11) - 5;
    Enemies[i].Active := True;
  end;
  
  OnMouseMove := MouseMove;
  OnKeyDown := KeyDown;
  OnKeyUp := KeyUp;

  while True do
  begin
    UpdatePlayer;
    UpdateEnemies;
    UpdateBullets;
    LockDrawing;
    ClearWindow(clBlack);
    DrawStars;
    DrawShip(PlayerPosition);
    DrawTarget(TargetPosition);
    DrawEnemies;
    DrawBullets;

    if IsSpacePressed then
    begin
      SetLength(Bullets, Length(Bullets) + 1);
      Bullets[High(Bullets)].Position := PlayerPosition;
      Bullets[High(Bullets)].Velocity.X := (TargetPosition.X - PlayerPosition.X) / BulletSpeed;
      Bullets[High(Bullets)].Velocity.Y := (TargetPosition.Y - PlayerPosition.Y) / BulletSpeed;
    end;
    Redraw;

    Sleep(10);

  end;
end.

Исходная версия Nxx, :

Короче, вот.

https://filetransfer.io/data-package/6Ln8xiXb#link

Просто сказал, что корабль привязан к точке резинкой и имеет массу.