Recent

Author Topic: Drive a tank around the screen  (Read 486 times)

TBMan

  • Full Member
  • ***
  • Posts: 120
Drive a tank around the screen
« on: March 24, 2025, 06:22:22 pm »
I've been playing around with rotating points and rectangles. I wanted to try rotating a bitmap and I succeeded this morning. I even managed to get the background to redraw. Here's the code. I don't know what I will do with this, but maybe combining with some sort of acceleration factor would be interesting.

Code: Pascal  [Select][+][-]
  1. program rotatetank3;
  2. {
  3.  version 3
  4.  
  5.  Rotates a 32x32 tile with 0 as the transparent color
  6.  
  7.  Press Q or q or esc to quit.
  8.  
  9.  Keypad controls rotation and movement
  10.  + = clockwise rotation
  11.  - = counterclockwise rotation
  12.  
  13.       7 8 9
  14.       4 5 6
  15.       1 2 3
  16.  The tank will rotate to direction prior to movement
  17.  if not already in that orientation
  18.  
  19.  There isn't any screen range checking done. PTCgraph clips by default
  20. }
  21.  
  22.  
  23. uses
  24.   ptccrt,
  25.   ptcgraph,
  26.   Math;
  27.  
  28. const
  29.   MaxCol = 31;
  30.  
  31. type
  32.   MapIconType = array[0..maxcol, 0..maxcol] of byte;
  33.  
  34. const
  35.  
  36.  
  37.  
  38.   desert01: MapIconType = ((2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  39.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  40.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  41.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  42.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 2, 2, 2, 2, 26, 2, 2, 2, 2, 2, 2, 22, 2, 22, 2, 2, 2, 2, 2),
  43.     (2, 2, 2, 2, 2, 2, 2, 29, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  44.     (2, 2, 2, 2, 2, 26, 29, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2),
  45.     (2, 2, 2, 2, 2, 112, 23, 23, 23, 2, 2, 2, 2, 2, 2, 2, 2, 29, 2, 26, 29, 29, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  46.     (2, 2, 2, 2, 26, 29, 26, 2, 2, 2, 2, 2, 2, 2, 2, 29, 29, 29, 24, 2, 2, 29, 29, 112, 29, 2, 2, 2, 2, 2, 2, 2),
  47.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 24, 26, 24, 24, 24, 2, 2, 26, 24, 24, 2, 2, 2, 2, 2, 2, 2),
  48.     (2, 2, 2, 2, 23, 26, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 24, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  49.     (2, 2, 2, 2, 23, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  50.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  51.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  52.     (2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 26, 2, 2, 2, 2, 2, 2, 2, 2, 2, 29, 29, 29, 2, 2, 2, 2, 2, 2, 2),
  53.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 111, 2, 2, 112, 29, 2, 2, 2, 2, 2),
  54.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 2, 2, 2, 2, 111, 29, 2, 2, 22, 22, 29, 2, 2, 2, 2, 2),
  55.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 19, 2, 2, 2, 2, 22, 29, 112, 29, 2, 2, 2),
  56.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 22, 22, 22, 2, 2, 2),
  57.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 23, 23, 2, 2, 29, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  58.     (2, 2, 2, 2, 2, 2, 2, 23, 2, 2, 2, 2, 2, 29, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26),
  59.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 19, 19, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  60.     (2, 2, 2, 2, 2, 2, 26, 2, 2, 2, 2, 2, 2, 19, 26, 22, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  61.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  62.     (2, 2, 2, 26, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  63.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 29, 22, 26, 22, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 26, 2, 2),
  64.     (2, 2, 2, 2, 29, 29, 2, 2, 2, 112, 19, 26, 29, 22, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  65.     (2, 2, 29, 112, 22, 19, 19, 2, 29, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
  66.     (2, 2, 19, 2, 2, 2, 2, 2, 2, 2, 2, 111, 2, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 112, 29, 112, 29, 2, 2, 2, 2, 2),
  67.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 113, 29, 2, 2, 2, 2, 29, 2, 2, 2, 2),
  68.     (2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 22, 22, 2, 29, 2, 2, 2),
  69.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 29, 22, 22, 2, 2, 22, 2, 112, 2, 2, 2),
  70.     (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2));
  71.  
  72.  
  73.  
  74.   TheTank: MapIconType = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  75.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  76.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  77.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  78.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 41, 0, 0, 0,
  79.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  80.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 26, 0, 0, 0,
  81.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  82.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 26, 0, 0, 0,
  83.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  84.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 26, 0, 0, 0,
  85.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  86.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 26, 0, 0, 0,
  87.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  88.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 20, 20, 26, 26, 20, 20,
  89.     20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  90.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 190, 190, 190, 26, 26, 190,
  91.     190, 190, 20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  92.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 190, 190, 26, 26,
  93.     190, 190, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  94.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 190, 190, 26, 26,
  95.     190, 190, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  96.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 190, 190, 26, 26,
  97.     190, 190, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  98.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 190, 21, 26, 26,
  99.     21, 190, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  100.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 21, 23, 23, 23,
  101.     23, 21, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  102.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 21, 23, 23, 23, 23, 23,
  103.     23, 21, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  104.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 21, 23, 1, 1, 1, 1, 23,
  105.     21, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  106.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 21, 23, 1, 1, 1, 1, 23,
  107.     21, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  108.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 21, 23, 1, 1, 1, 1, 23,
  109.     21, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  110.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 21, 23, 1, 1, 1, 1, 23,
  111.     21, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  112.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 21, 23, 23, 23, 23, 23,
  113.     23, 21, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  114.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 21, 21, 21, 21,
  115.     21, 21, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  116.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 190, 190, 190, 190,
  117.     190, 190, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  118.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 190, 190, 190, 190,
  119.     190, 190, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  120.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 190, 190, 190, 190, 190, 190, 190,
  121.     190, 190, 190, 190, 190, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  122.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 20, 20, 20, 20, 20, 20,
  123.     20, 20, 20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  124.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 20, 20, 20, 20, 20, 20,
  125.     20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  126.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 20, 20, 20, 20, 20, 20,
  127.     20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  128.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  129.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  130.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  131.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  132.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  133.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  134.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  135.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  136.     (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  137.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  138.  
  139.  
  140.  
  141. type
  142.  
  143.   TSinglePoint = record
  144.     x, y: single;
  145.   end;
  146.  
  147.   linedata = record
  148.     P: Tsinglepoint;
  149.     color: byte;
  150.   end;
  151.  
  152.   matrixType = array[0..maxcol, 0..maxcol] of linedata;
  153.  
  154. var
  155.   I: integer;
  156.   ch: char;
  157.   lastdegree: integer;
  158.  
  159.   cx, cy: integer;     // center x and center y
  160.   b: MatrixType;
  161.   gmode, gdriver: smallint;
  162.  
  163.  
  164.   procedure SetUpTank(x, y: integer);
  165.   var
  166.     rr, cc: integer;
  167.   begin
  168.     // setup initial tank coord matrix
  169.     for rr := 0 to maxcol do
  170.     begin
  171.       for cc := 0 to maxcol do
  172.       begin
  173.         b[rr, cc].color := TheTank[rr, cc];
  174.         b[rr, cc].p.x := x + cc;
  175.         b[rr, cc].p.y := y + rr;
  176.       end;
  177.     end;
  178.  
  179.     cx := X + 16;   // x + 1/2 matrix width
  180.     cy := y + 16;   // y + 1/2 matrix depth
  181.  
  182.   end;
  183.  
  184.   procedure drawcell(xx, yy: integer);
  185.   var
  186.     startx, rr, cc: integer;
  187.   begin
  188.     startx := xx;
  189.     for rr := 0 to maxcol do
  190.     begin
  191.       xx := startX;
  192.       for cc := 0 to maxcol do
  193.       begin
  194.         putpixel(xx, yy, desert01[rr, cc]);
  195.         Inc(xx);
  196.       end;
  197.       Inc(yy);
  198.     end;
  199.   end;
  200.  
  201.  
  202.   procedure rotatepoint(var P: Tsinglepoint; degrees, centerx, centerY: float);
  203.   var
  204.     newx, newy, angle: float;
  205.     cx, cy, y, x: single;
  206.   begin
  207.     angle := degTorad(degrees);
  208.     cx := centerx;
  209.     cy := centery;
  210.     x := p.x;
  211.     y := p.y;
  212.     x := x - cx;
  213.     Y := y - cy;
  214.     newx := (x * cos(angle)) - (y * sin(angle));
  215.     newy := (x * sin(angle)) + (y * cos(angle));
  216.     newx := newx + cx;
  217.     newy := newy + cy;
  218.     p.x := newx;
  219.     p.y := newy;
  220.   end;
  221.  
  222.   procedure moveobject(var M: MatrixType; Xc, Yc: integer);
  223.   var
  224.     r, c: integer;
  225.   begin
  226.     for r := 0 to maxcol do
  227.       for c := 0 to maxcol do
  228.       begin
  229.         M[r, c].p.x := M[r, c].p.x + Xc;
  230.         M[r, c].p.Y := M[r, c].p.Y + Yc;
  231.       end;
  232.   end;
  233.  
  234.   procedure rotateobject(var M: Matrixtype; Degrees, CenterX, CenterY: integer);
  235.   var
  236.  
  237.     r, c: integer;
  238.     Row, Column: integer;
  239.   begin
  240.     for r := 0 to maxcol do
  241.       for c := 0 to maxcol do
  242.       begin
  243.         rotatepoint(M[r, c].p, degrees, centerx, centery);
  244.         Column := round(M[r, c].p.x) mod (maxcol + 1);  // map x,y to background tile pixel
  245.         Row := round(m[r, c].p.y) mod (maxcol + 1);
  246.  
  247.         // redraw background
  248.         putpixel(round(m[r, c].p.x), round(m[r, c].p.y), desert01[Row, Column]);
  249.  
  250.         //overlay tank on screen
  251.         if m[r, c].color <> 0 then
  252.           putpixel(round(M[r, c].p.x), round(m[r, c].p.y), m[r, c].color);
  253.       end;
  254.   end;
  255.  
  256.  
  257.   procedure showobject(M: MatrixType);
  258.   var
  259.     r, c: integer;
  260.   begin
  261.     for r := 0 to maxcol do
  262.       for c := 0 to maxcol do
  263.         if m[r, c].color <> 0 then
  264.           putpixel(round(M[r, c].p.x), round(m[r, c].p.y), m[r, c].color);
  265.   end;
  266.  
  267.  
  268.   procedure DrawBackground;
  269.   var
  270.     ScreenX, ScreenY: integer;
  271.     r, c: integer;
  272.   begin
  273.     screenY := 0;
  274.     for r := 0 to 5 do
  275.     begin
  276.       screenx := 0;
  277.       for c := 0 to 8 do
  278.       begin
  279.         drawcell(screenx, screeny);
  280.         Inc(screenx, maxcol + 1);
  281.       end;
  282.       Inc(screeny, maxcol + 1);
  283.     end;
  284.   end;
  285.  
  286.  
  287. begin
  288.  
  289.   gdriver := vesa;  // this is for my system - check documentation for use of initgraph
  290.  
  291.   Gmode := installusermode(320, 200, 256, 1, 8000, 6000);
  292.   WindowTitle := 'Tank Rotation';
  293.   initgraph(gdriver, gmode, '');
  294.  
  295.   SetupTank(160, 128);
  296.  
  297.   DrawBackground;
  298.  
  299.  
  300.   lastdegree := 0;
  301.   ShowObject(b);
  302.  
  303.   repeat
  304.     i := 0;
  305.     ch := readkey;
  306.  
  307.  
  308.     case ch of
  309.       '-': i := -1;      // counter clockwise
  310.       '+': i := 1;       // clockwise
  311.       '3': begin
  312.         if lastdegree <> 135 then
  313.         begin
  314.           if lastdegree > 135 then i := -1;
  315.           if lastdegree < 135 then i := 1;
  316.           if lastdegree > 315 then   // 316 to 360
  317.           begin
  318.             i := 1;
  319.             repeat
  320.               rotateobject(b, i, cx, cy);
  321.               LastDegree := lastDegree + i;
  322.               delay(10);
  323.             until lastdegree = 360;
  324.             lastdegree := 0;
  325.           end;
  326.           repeat
  327.             rotateobject(b, i, cx, cy);
  328.             LastDegree := lastDegree + i;
  329.             delay(10);
  330.           until lastdegree = 135;
  331.         end;
  332.         Inc(cx);
  333.         Inc(cy);
  334.         i := 0;
  335.         moveobject(b, 1, 1);
  336.       end;
  337.       '1': begin
  338.         if lastdegree <> 225 then
  339.         begin
  340.           if lastdegree = 0 then lastdegree := 360;
  341.           if lastdegree > 225 then i := -1
  342.           else
  343.             i := 1;
  344.           if lastdegree <= 45 then
  345.           begin
  346.             i := -1;
  347.             repeat
  348.               rotateobject(b, i, cx, cy);
  349.               LastDegree := lastDegree + i;
  350.               delay(10);
  351.             until lastdegree = 0;
  352.             i := -1;
  353.             lastdegree := 360;
  354.           end;
  355.           repeat
  356.             rotateobject(b, i, cx, cy);
  357.             LastDegree := lastDegree + i;
  358.             delay(10);
  359.           until lastdegree = 225;
  360.         end
  361.         else
  362.  
  363.           i := 0;
  364.         Dec(cx);
  365.         Inc(cy);
  366.         moveobject(b, -1, 1);
  367.       end;
  368.       '9': begin
  369.         if lastdegree <> 45 then
  370.         begin
  371.           if lastdegree < 45 then i := 1;
  372.           if (lastdegree > 45) and (lastdegree <= 225) then
  373.             i := -1;
  374.           if lastdegree > 225 then
  375.           begin
  376.             i := 1;
  377.             repeat
  378.               rotateobject(b, i, cx, cy);
  379.               LastDegree := lastDegree + i;
  380.               delay(10);
  381.             until lastdegree = 360;
  382.             lastdegree := 0;
  383.             // i := 1;
  384.           end;
  385.  
  386.           repeat
  387.             rotateobject(b, i, cx, cy);
  388.             LastDegree := lastDegree + i;
  389.             delay(10);
  390.           until lastdegree = 45;
  391.           ;
  392.         end;
  393.         Inc(cx);
  394.         Dec(cy);
  395.         I := 0;
  396.         moveobject(b, 1, -1);
  397.       end;
  398.  
  399.       '7': begin
  400.         if lastdegree = 0 then lastdegree := 360;
  401.         if lastdegree <= 90 then lastdegree := 360 + lastdegree;
  402.         if lastdegree <> 315 then
  403.         begin
  404.           if (lastdegree > 315) then
  405.             i := -1
  406.           else
  407.             i := 1;
  408.           repeat
  409.             rotateobject(b, i, cx, cy);
  410.             LastDegree := lastDegree + i;
  411.             delay(10);
  412.           until (lastdegree = 315);
  413.         end;
  414.         i := 0;
  415.         Dec(cx);
  416.         Dec(cy);
  417.         moveobject(b, -1, -1);
  418.       end;
  419.       '8': begin
  420.         // rotate to face position
  421.         if (lastdegree <> 0) and (lastdegree <> 360) then
  422.         begin
  423.           if lastdegree > 180 then i := 1
  424.           else
  425.             i := -1;
  426.           repeat
  427.             rotateobject(b, i, cx, cy);
  428.             LastDegree := lastDegree + i;
  429.             delay(10);
  430.           until (lastdegree = 0) or (lastdegree = 360);
  431.         end;
  432.         Dec(cy);
  433.         moveobject(b, 0, -1);
  434.         i := 0;
  435.       end;
  436.       '2': begin
  437.         if (lastdegree <> 180) then
  438.         begin
  439.           if lastdegree > 180 then i := -1
  440.           else
  441.             i := 1;
  442.           repeat
  443.             rotateobject(b, i, cx, cy);
  444.             LastDegree := lastDegree + i;
  445.             delay(10);
  446.           until (lastdegree = 180);
  447.         end;
  448.         Inc(cy);
  449.         moveobject(b, 0, 1);
  450.         i := 0;
  451.       end;
  452.       '4': begin
  453.         if lastdegree <> 270 then
  454.         begin
  455.           if lastdegree < 180 then lastdegree := 360 + lastdegree;
  456.           if lastdegree > 270 then i := -1
  457.           else
  458.             i := 1;
  459.  
  460.           repeat
  461.             rotateobject(b, i, cx, cy);
  462.             LastDegree := lastDegree + i;
  463.             delay(10);
  464.           until (lastdegree = 270);
  465.         end;
  466.         Dec(cx);
  467.         moveobject(b, -1, 0);
  468.         i := 0;
  469.       end;
  470.       '6': begin
  471.         if lastdegree <> 90 then
  472.         begin
  473.           if lastdegree > 90 then i := -1
  474.           else
  475.             i := 1;
  476.           repeat
  477.             rotateobject(b, i, cx, cy);
  478.             LastDegree := lastDegree + i;
  479.             delay(10);
  480.           until (lastdegree = 90);
  481.         end;
  482.         i := 0;
  483.         Inc(cx);
  484.         moveobject(b, 1, 0);
  485.       end;
  486.     end;
  487.     lastdegree := lastdegree + i;
  488.     if LastDegree < 1 then lastdegree := 360;
  489.     if lastdegree >= 360 then lastdegree := 0;
  490.  
  491.     RotateObject(b, i, cx, cy);
  492.  
  493.     while keypressed do readkey;  // eat up extra keypresses
  494.  
  495.  
  496.   until (ch = 'q') or (ch = 'Q') or (ch = chr(27));
  497.  
  498.   closegraph;
  499. end.
  500.  
  501.  
  502.  
« Last Edit: March 24, 2025, 10:46:03 pm by TBMan »

Boleeman

  • Hero Member
  • *****
  • Posts: 887
Re: Drive a tank around the screen
« Reply #1 on: March 25, 2025, 06:22:40 am »
Not a Tank but:

Came across a car simulator here: https://github.com/GuvaCode/mini_projects/tree/main/miniprojects/Parken

Demo shows how to implement a chain of "vehicles" that are controlled by a single force.


Also attached are 3 misc opensource Tank Game Sources in Delphi (from github)
Perhaps might be able to get some ideas from these.
« Last Edit: March 25, 2025, 07:32:30 am by Boleeman »

 

TinyPortal © 2005-2018