TurtleABC для Linux

This commit is contained in:
Mikhalkovich Stanislav 2025-10-05 21:25:01 +03:00
parent 620c5c624c
commit cb30e56434
9 changed files with 314 additions and 8 deletions

View file

@ -15,7 +15,7 @@ internal static class RevisionClass
public const string Major = "3";
public const string Minor = "11";
public const string Build = "0";
public const string Revision = "3662";
public const string Revision = "3665";
public const string MainVersion = Major + "." + Minor;
public const string FullVersion = Major + "." + Minor + "." + Build + "." + Revision;

View file

@ -1,4 +1,4 @@
%COREVERSION%=0
%REVISION%=3662
%MINOR%=11
%REVISION%=3665
%COREVERSION%=0
%MAJOR%=3

View file

@ -0,0 +1,52 @@
uses TurtleABC;
begin
Mark;
SetScale(4);
SetOrigin(5,-35);
Mark;
SetColor(Color.Blue);
Down;
loop 4 do
begin
Forw(20);
Turn(90);
end;
Up;
MoveTo(-30, 30);
Down;
SetColor(Color.Red);
Mark;
loop 3 do
begin
Forw(15);
Turn(120);
end;
Up;
MoveTo(30, 40);
Down;
SetColor(Color.Green);
Mark;
loop 20 do
begin
Forw(30);
Turn(100);
end;
Up;
MoveTo(-30, 90);
Mark;
Down;
SetColor(Color.Orange);
loop 5 do
begin
Forw(30);
Turn(144);
end;
end.

View file

@ -1 +1 @@
3.11.0.3662
3.11.0.3665

View file

@ -1 +1 @@
!define VERSION '3.11.0.3662'
!define VERSION '3.11.0.3665'

View file

@ -3,8 +3,7 @@ uses
PABCSystem, PABCExtensions, __RedirectIOMode, __RunMode, ABCButtons, ABCHouse, ABCObjects,
ABCSprites, CRT, DMCollect, DMTaskMaker, DMZadan, Drawman,
DrawManField, Events, FilesOperations,
GraphABC,
GraphABCHelper,
GraphABC, GraphABCHelper, TurtleABC,
GraphWPFBase,
Graph3D,
GraphWPF, Coords, Turtle, TurtleWPF,

View file

@ -161,6 +161,7 @@
File ..\bin\Lib\School.pcu
File ..\bin\Lib\SF.pcu
File ..\bin\Lib\Turtle.pcu
File ..\bin\Lib\TurtleABC.pcu
File ..\bin\Lib\Coords.pcu
File ..\bin\Lib\TurtleWPF.pcu
File ..\bin\Lib\LightPT.pcu
@ -260,6 +261,7 @@
${AddFile} "School.pcu"
${AddFile} "SF.pcu"
${AddFile} "Turtle.pcu"
${AddFile} "TurtleABC.pcu"
${AddFile} "Coords.pcu"
${AddFile} "TurtleWPF.pcu"
${AddFile} "LightPT.pcu"
@ -382,6 +384,7 @@
File ..\bin\Lib\School.pas
File ..\bin\Lib\SF.pas
File ..\bin\Lib\Turtle.pas
File ..\bin\Lib\TurtleABC.pas
File ..\bin\Lib\Coords.pas
File ..\bin\Lib\TurtleWPF.pas
File ..\bin\Lib\LightPT.pas
@ -470,6 +473,7 @@
${AddFile} "School.pas"
${AddFile} "SF.pas"
${AddFile} "Turtle.pas"
${AddFile} "TurtleABC.pas"
${AddFile} "Coords.pas"
${AddFile} "TurtleWPF.pas"
${AddFile} "LightPT.pas"

View file

@ -110,6 +110,7 @@ copy bin\Lib\Speech.pcu Release\PascalABCNETLinux\Lib\Speech.pcu
copy bin\Lib\Tasks.pcu Release\PascalABCNETLinux\Lib\Tasks.pcu
copy bin\Lib\Timers.pcu Release\PascalABCNETLinux\Lib\Timers.pcu
copy bin\Lib\Turtle.pcu Release\PascalABCNETLinux\Lib\Turtle.pcu
copy bin\Lib\TurtleABC.pcu Release\PascalABCNETLinux\Lib\TurtleABC.pcu
copy bin\Lib\ABCDatabases.pas Release\PascalABCNETLinux\LibSource\ABCDatabases.pas
copy bin\Lib\BBCMicrobit.pas Release\PascalABCNETLinux\LibSource\BBCMicrobit.pas
@ -145,6 +146,7 @@ copy bin\Lib\Speech.pas Release\PascalABCNETLinux\LibSource\Speech.pas
copy bin\Lib\Tasks.pas Release\PascalABCNETLinux\LibSource\Tasks.pas
copy bin\Lib\Timers.pas Release\PascalABCNETLinux\LibSource\Timers.pas
copy bin\Lib\Turtle.pas Release\PascalABCNETLinux\LibSource\Turtle.pas
copy bin\Lib\TurtleABC.pas Release\PascalABCNETLinux\LibSource\TurtleABC.pas
copy bin\Lib\TwoPanelsWindow.pas Release\PascalABCNETLinux\LibSource\TwoPanelsWindow.pas
copy bin\Lib\XLSX.pas Release\PascalABCNETLinux\LibSource\XLSX.pas
copy bin\Lib\__RedirectIOMode.pas Release\PascalABCNETLinux\LibSource\__RedirectIOMode.pas

249
bin/Lib/TurtleABC.pas Normal file
View file

@ -0,0 +1,249 @@
unit TurtleABC;
interface
uses GraphABC;
type
TPoint = class
public
X,Y: real;
constructor(xx,yy: real) := (X,Y) := (xx,yy);
end;
Color = GraphABC.Color;
function Pnt(x,y: real): TPoint;
/// Передвигает Черепаху вперёд на расстояние r
procedure Forw(r: real);
/// Передвигает Черепаху назад на расстояние r
procedure Back(r: real);
/// Поворачивает Черепаху на угол da по часовой стрелке
procedure Turn(da: real);
/// Поворачивает Черепаху на угол da влево
procedure TurnLeft(da: real);
/// Поворачивает Черепаху на угол da вправо
procedure TurnRight(da: real);
/// Возвращает цвет по красной, зеленой и синей составляющей (в диапазоне 0..255)
function RGB(r,g,b: byte): Color;
procedure Mark;
/// Опускает перо Черепахи
procedure Down;
/// Поднимает перо Черепахи
procedure Up;
/// Устанавливает ширину линии
procedure SetWidth(w: integer);
/// Устанавливает цвет линии
procedure SetColor(c: Color);
/// Перемещает Черепаху в точку (x,y)
procedure MoveTo(x,y: real);
/// Перемещает Черепаху в точку (x,y)
procedure MoveTo(x,y: integer);
/// Сохраняет изображение, нарисованное Черепахой, в файл fname (например, 'a.png')
procedure Save(fname: string);
/// Список уникальных точек
function TurtlePoints: List<TPoint>;
/// Устанавливает масштаб
procedure SetScale(s: real);
/// Устанавливает начало координат
procedure SetOrigin(x,y: real);
/// Рисует точки в целочисленных координатах на всём экране
procedure DrawCoordPoints;
implementation
var
TurtleX: real := 0;
TurtleY: real := 0;
angle: real := 90; // начальное направление - вверх
penDown: boolean := True;
lstPoints := new List<TPoint>;
scale: real := 1.0;
screenOriginX: real := Window.Width / 2;
screenOriginY: real := Window.Height / 2;
screenCenterX: real := Window.Width / 2;
screenCenterY: real := Window.Height / 2;
function RGB(r,g,b: byte): Color := GraphABC.RGB(r, g, b);
function RealToScreenX(X: real) := Round(screenOriginX + X * scale);
function RealToScreenY(Y: real) := Round(screenOriginY - Y * scale);
function ScreenToRealX(sx: real) := (sx-screenOriginX)/scale;
function ScreenToRealY(sy: real) := (screenOriginY-sy)/scale;
procedure Mark;
begin
var screenX := RealToScreenX(TurtleX);
var screenY := RealToScreenY(TurtleY);
Circle(screenX,screenY,3);
end;
/// Поворачивает Черепаху на угол da по часовой стрелке
procedure Turn(da: real);
begin
angle -= da;
end;
procedure TurnLeft(da: real) := Turn(-da);
procedure TurnRight(da: real) := Turn(da);
function Pnt(x,y: real): TPoint := new TPoint(x,y);
/// Перемещает Черепаху в точку (x,y)
procedure MoveTo(x,y: real);
begin
TurtleX := x;
TurtleY := y;
var screenX := RealToScreenX(TurtleX);
var screenY := RealToScreenY(TurtleY);
GraphABC.MoveTo(screenX, screenY);
lstPoints.Add(Pnt(TurtleX,TurtleY));
end;
/// Перемещает Черепаху в точку (x,y)
procedure MoveTo(x,y: integer);
begin
MoveTo(real(x),real(y));
end;
procedure LineTo(x,y: real);
begin
TurtleX := x;
TurtleY := y;
var screenX := RealToScreenX(TurtleX);
var screenY := RealToScreenY(TurtleY);
GraphABC.LineTo(screenX, screenY);
lstPoints.Add(Pnt(TurtleX,TurtleY));
end;
procedure LineTo(x,y: integer);
begin
LineTo(real(x),real(y));
end;
/// Продвигает Черепаху вперёд на расстояние r
procedure Forw(r: real);
begin
var radAngle := DegToRad(angle);
var dx := r * Cos(radAngle);
var dy := r * Sin(radAngle);
TurtleX += dx;
TurtleY += dy;
if penDown then
LineTo(TurtleX, TurtleY)
else MoveTo(TurtleX, TurtleY);
end;
procedure Back(r: real) := Forw(-r);
/// Опускает перо Черепахи
procedure Down := penDown := True;
/// Поднимает перо Черепахи
procedure Up := penDown := False;
/// Устанавливает ширину линии
procedure SetWidth(w: integer) := GraphABC.Pen.Width := w;
/// Устанавливает цвет линии
procedure SetColor(c: Color) := GraphABC.Pen.Color := c;
procedure Save(fname: string) := GraphABC.SaveWindow(fname);
var eps := 1e-5;
function TurtlePoints: List<TPoint>;
begin
var unique := new List<TPoint>;
foreach var point in lstPoints do
if not unique.Any(p -> Sqrt(Sqr(p.X - point.X) + Sqr(p.Y - point.Y)) < eps) then
unique.Add(point);
Result := unique;
end;
/// Устанавливает масштаб
procedure SetScale(s: real);
begin
scale := s;
end;
/// Устанавливает начало координат
procedure SetOrigin(x,y: real);
begin
(TurtleX,TurtleY) := (0,0);
screenOriginX := screenCenterX + x * scale;
screenOriginY := screenCenterY - y * scale;
GraphABC.MoveTo(Round(screenOriginX),Round(screenOriginY));
end;
procedure DrawCoordPoints;
begin
SetColor(color.Gray);
var x1 := screenOriginX;
while x1 >= 0 do
x1 -= scale;
var xx1 := Round(ScreenToRealX(x1));
x1 := screenOriginX;
while x1 <= Window.Width do
x1 += scale;
var xx2 := Round(ScreenToRealX(x1));
var y1 := screenOriginY;
while y1 >= 0 do
y1 -= scale;
var yy1 := Round(ScreenToRealy(y1));
y1 := screenOriginy;
while y1 <= Window.Height do
y1 += scale;
var yy2 := Round(ScreenToRealY(y1));
if yy1 > yy2 then
Swap(yy1,yy2);
LockDrawing;
for var x := xx1 to xx2 do
for var y := yy1 to yy2 do
begin
var screenX := RealToScreenX(x);
var screenY := RealToScreenY(y);
Brush.Color := Color.Black;
PutPixel(screenX,screenY,Color.Black);
end;
UnLockDrawing
end;
procedure Init;
begin
GraphABC.Window.Title := 'Исполнитель Черепаха ABC';
MoveTo(TurtleX, TurtleY);
OnKeyPress := ch -> begin
if ch = ' ' then
DrawCoordPoints;
end;
end;
begin
Init;
end.