Demonstration of a two-dimensional array of integer

This program demonstrates a two-dimensional array of integer (described above. If TrackDemo does not run in your current browser, please try another (such as Chrome). If you see no display at school, the security system might have blocked it. You can try instead this direct link to the program running on its own page. Refresh the screen to see a different terrain.

TrackDemo

The Code

This code will compile using Versions 2.2 and 3.0 of Smart Mobile Studio.

unit Unit1;

interface

uses 
  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics;

type
  TCanvasProject = class(TW3CustomGameApplication)
  private
    const SCALE_FACTOR = 1 / 3;
    const RADIUS = 5;
    FrameNumber: integer = 0;
    Index1, Index2: integer;
    const TrackCoords: array [1..227, 1..2] of integer = [//From Peter Hearnshaw's 3D-Driving
    [1300, 300], [1299, 281], [1293, 259], [1283, 239], [1269, 221], [1249, 207],
    [1229, 201], [1203, 196], [1155, 196], [1177, 196], [1105, 198], [1057, 196],
    [1003, 196], [906, 201], [802, 196], [702, 198], [599, 196], [501, 196], [449, 196],
    [551, 196], [425, 196], [399, 196], [375, 198], [347, 201], [329, 211], [325, 237],
    [325, 259], [329, 281], [347, 291], [377, 293], [399, 301], [431, 303], [451, 301],
    [501, 299], [551, 299], [599, 299], [648, 299], [702, 301], [676, 301], [734, 303],
    [758, 307], [786, 313], [812, 325], [836, 339], [856, 361], [872, 381], [888, 405],
    [896, 435], [900, 465], [904, 501], [902, 549], [902, 525], [900, 602], [902, 650],
    [902, 702], [900, 800], [902, 902], [902, 1003], [902, 1101], [902, 1201], [902, 1253],
    [902, 1301], [904, 1347], [904, 1375], [904, 1402], [908, 1422], [914, 1438], [922, 1456],
    [930, 1466], [944, 1482], [966, 1490], [982, 1500], [1003, 1506], [1031, 1504], [1053, 1504],
    [1077, 1506], [1103, 1504], [1133, 1504], [1153, 1504], [1175, 1506], [1201, 1504],
    [1227, 1500], [1247, 1494], [1263, 1486], [1277, 1472], [1289, 1460], [1295, 1438],
    [1297, 1418], [1299, 1402], [1303, 1375], [1303, 1353], [1303, 1303], [1303, 1249],
    [1305, 1203], [1301, 1105], [1301, 1003], [1303, 902], [1303, 800], [1303, 704],
    [1303, 599], [1303, 503], [1305, 453], [1305, 399], [1303, 351], [1303, 325],
    [1399, 301], [1399, 263], [1393, 233], [1385, 201], [1371, 178], [1355, 158],
    [1335, 134], [1311, 118], [1287, 110], [1261, 106], [1233, 98], [1201, 100],
    [1175, 102], [1155, 100], [1105, 100], [1051, 100], [1001, 98], [904, 100], [802, 100],
    [702, 100], [599, 98], [501, 102], [449, 102], [401, 102], [351, 102], [321, 102],
    [299, 98], [277, 106], [255, 112], [231, 124], [213, 144], [201, 170], [198, 198],
    [201, 219], [201, 247], [201, 275], [198, 299], [203, 327], [207, 351], [217, 371],
    [241, 385], [269, 395], [303, 397], [327, 399], [353, 399], [401, 401], [455, 399],
    [497, 397], [551, 399], [599, 401], [652, 401], [676, 399], [698, 399], [728, 403],
    [748, 411], [770, 421], [784, 443], [794, 461], [798, 481], [802, 503], [802, 529],
    [802, 549], [802, 604], [802, 652], [802, 700], [802, 800], [802, 898], [802, 1001],
    [802, 1103], [800, 1203], [802, 1251], [800, 1299], [802, 1341], [802, 1371],
    [804, 1402], [802, 1438], [810, 1470], [816, 1498], [832, 1520], [848, 1542],
    [866, 1564], [890, 1576], [916, 1586], [942, 1594], [968, 1596], [1001, 1598],
    [1031, 1598], [1055, 1598], [1079, 1596], [1105, 1598], [1133, 1598], [1155, 1598],
    [1173, 1598], [1199, 1598], [1237, 1596], [1271, 1594], [1297, 1586], [1321, 1572],
    [1343, 1556], [1365, 1534], [1381, 1514], [1389, 1486], [1395, 1458], [1399, 1434],
    [1400, 1399], [1400, 1375], [1400, 1349], [1400, 1299], [1400, 1251], [1400, 1203],
    [1399, 1103], [1400, 998], [1400, 902], [1399, 798], [1400, 702], [1400, 604],
    [1400, 503], [1400, 451], [1400, 401], [1400, 351], [1400, 325], [1400, 300]];
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
  end;

implementation

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  GameView.Delay := 40;
  GameView.StartSession(True);
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;

procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
begin
  Index1 := FrameNumber mod 120 + 107; //Adjust Index to follow the outside path
  Index2 := FrameNumber mod 104 + 2;   //Adjust Index2 to follow the inside path
  // Clear background to grey
  Canvas.FillStyle := 'grey';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
  // Draw inside of track
  Canvas.FillStyle := 'green';
  Canvas.BeginPath;
  Canvas.MoveToF(TrackCoords[2, 1] * SCALE_FACTOR, TrackCoords[2, 2] * SCALE_FACTOR);
  for var i := 3 to 105 do
    Canvas.LineToF(TrackCoords[i, 1] * SCALE_FACTOR, TrackCoords[i, 2] * SCALE_FACTOR);
  Canvas.ClosePath;
  Canvas.Stroke;

  Canvas.MoveToF(TrackCoords[1, 1] * SCALE_FACTOR, TrackCoords[1, 2] * SCALE_FACTOR);
  for var i := 106 to 227 do
    Canvas.LineToF(TrackCoords[i, 1] * SCALE_FACTOR, TrackCoords[i, 2] * SCALE_FACTOR);
  Canvas.ClosePath;
  Canvas.Stroke;

  // Move red circle round the outside track
  Canvas.FillStyle := 'red';
  Canvas.BeginPath;
  Canvas.Ellipse(TrackCoords[Index1, 1] * SCALE_FACTOR - RADIUS, TrackCoords[Index1, 2] * SCALE_FACTOR - RADIUS,
                 TrackCoords[Index1, 1] * SCALE_FACTOR + RADIUS, TrackCoords[Index1, 2] * SCALE_FACTOR + RADIUS);
  Canvas.ClosePath;
  Canvas.Fill;

  // Move blue circle round the inside track
  Canvas.FillStyle := 'blue';
  Canvas.BeginPath;
  Canvas.Ellipse(TrackCoords[Index2, 1] * SCALE_FACTOR - RADIUS, TrackCoords[Index2, 2] * SCALE_FACTOR - RADIUS,
                 TrackCoords[Index2, 1] * SCALE_FACTOR + RADIUS, TrackCoords[Index2, 2] * SCALE_FACTOR + RADIUS);
  Canvas.ClosePath;
  Canvas.Fill;

  inc(FrameNumber);
end;

end.
Programming - a skill for life!

Declaring and assigning arrays in Smart Pascal