Recent

Author Topic: how to delete the canvas rectangle  (Read 79046 times)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #45 on: September 27, 2017, 03:43:40 pm »
please find the attachment

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #46 on: September 27, 2017, 03:54:09 pm »
@helmi

You mentioned link list.

Yes, it is a very hard topic. I remember first time I learned it. I got a book of Assembly Programming, on the last chapter there was the topic about Singly linked lists and Doubly linked list. What, I learned linked list using Assembly Language. Yes it is crazy! :o

It took me months to fully understand it. So I wrote the module and tested it properly and saved the pascal code (I learned Assembly but I don't like it). I now still keep the code. Free Pascal is great, it has ready to use TList so I do not use my own version of TList code anymore.

The TS is a novice, back buffer maybe to advanced for him to understand. I am sure he will later ask, how to improve the performance and reduce flicker.

@rhong7

On your previous code, I saw snakepos but I cannot found it anymore. You deleted it accidentally?

Add this:
Code: Pascal  [Select][+][-]
  1. var
  2.   snakepos: TRect;

Also, please remove this line:
Code: Pascal  [Select][+][-]
  1.   i := SnakeBody.Count;
I used it for testing but I forgot to delete that line.
« Last Edit: September 27, 2017, 03:59:59 pm by Handoko »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #47 on: September 27, 2017, 04:00:18 pm »
Code: Pascal  [Select][+][-]
  1. SnakeSegment^:= snakepos;
C:\Users\rhong\Desktop\snake game by hondoko\unit1.pas(61,16) Error: Illegal qualifier

i still get error for this one

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #48 on: September 27, 2017, 04:03:35 pm »
What is the version of your Lazarus? What is the version of your FPC? What is your OS?

Lazarus main menu > Help > About

It can compile here on my computer.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #49 on: September 27, 2017, 04:06:20 pm »
FPC 2.6.4

where can i find os?

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #50 on: September 27, 2017, 04:09:59 pm »
I'm not very sure but maybe you can try, change all of this in your code:

Code: Pascal  [Select][+][-]
  1. var
  2.   SnakeSegment: PRect;

to

Code: Pascal  [Select][+][-]
  1. var
  2.   SnakeSegment: ^TRect;

PRect means ^TRect, but I guess your version does not support PRect.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #51 on: September 27, 2017, 04:15:13 pm »
I can compile now but when i compile it the error message comes up saying "Project 1 raised exception class 'External:SIGSEGV'.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #52 on: September 27, 2017, 04:15:56 pm »
also what's the difference between
Code: Pascal  [Select][+][-]
  1. Trect

and

Code: Pascal  [Select][+][-]
  1. ^Trect

rvk

  • Hero Member
  • *****
  • Posts: 6693
Re: how to delete the canvas rectangle
« Reply #53 on: September 27, 2017, 04:18:08 pm »
B.T.W. PRect is defined in Types.
So adding this under implementation works too:
Code: Pascal  [Select][+][-]
  1. implementation
  2. uses Types;

The exception that follows is probably because rhong7 added this
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.   var
  3.   SnakeSegment: PRect;
  4. begin
  5.   SnakeBody := TList.Create;
  6.   // ...
  7.   SnakeSegment^:= snakepos; // Put this line after ...random(10)
  8.   SnakeBody.Add(SnakeSegment); // Put this line after SnakeSegment^ := ...
  9. // ...
  10. end;
But where are the random-lines Handoko is speaking of?

I think you are both working with different base-code. Just saying add this won't work in that case  %)

also what's the difference between
Code: Pascal  [Select][+][-]
  1. Trect
and
Code: Pascal  [Select][+][-]
  1. ^Trect
TRect is a record structure for a rectangle. With Left, Top etc.
PRect is a pointer to a TRect. It is defined in Types.

« Last Edit: September 27, 2017, 04:20:05 pm by rvk »

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #54 on: September 27, 2017, 04:23:49 pm »
@rhong7

Of course it caused error. Because you have not fully follow my instruction. Try to read it carefully again and compare it the actual code. I know what they are  :-X

Quote
^Trect

That is pointer type variable. What is pointer? Well it is a loooong story. Try to read the documentation on the link I gave you.

Pointer is a headache topic for beginners. To proper use it, users must be very careful. A small mistake may cause the code hard to debug.

But once after you understand and able to use it properly, your programming skill will improve a lot. You can solve many new things programmatically.

@rhong7 & @rvk

Don't simply copy paste the code. You should compare it with the original code.

This 3 dots
Code: Pascal  [Select][+][-]
  1.   // ...
means the original code.

Quote
But where are the random-lines Handoko is speaking of?

The original code (TS's code) call random function.

You can get the original code here:
http://forum.lazarus.freepascal.org/index.php/topic,38136.msg259652.html#msg259652

And the on the IDE, type [CTRL]+[F] and search "random" on the code.
« Last Edit: September 27, 2017, 04:28:48 pm by Handoko »

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #55 on: September 27, 2017, 04:34:43 pm »
Can anyone understand what I just talking? Maybe I need to rephrase the words.

This is the original code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   PutItem(snakepos.left, snakepos.top, snake);
  4.   snakepos.left:=random(10);
  5.   snakepos.top:=random(10);
  6.   drawgameworld;
  7.  
  8. end;

I said "Put this line after ...random(10)", it means put it on the line between line 5 and 6. But TS remove all the code in the procedure with mine.

Hope, anyone can understand now.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #56 on: September 27, 2017, 04:36:30 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, LCLType, ExtCtrls;
  9.  
  10. type
  11.  
  12.   TItem = (Emtpy, Snake, Fruit);
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Timer1: TTimer;
  18.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  21.     procedure Timer1Timer(Sender: TObject);
  22.   private
  23.     procedure ClearWorld;
  24.     procedure PutItem(X, Y: Integer; Item: TItem);
  25.     function GetItem(X, Y: Integer): TItem;
  26.     procedure DrawGameWorld;
  27.     Procedure Movesnake(NewHead: TRect);
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.   snakepos:trect;
  33.   direction:integer;
  34.   SnakeBody: TList;
  35.   SnakeIsGrowing: Boolean = False;
  36.  
  37. implementation
  38. uses
  39.   types;
  40.  
  41. const
  42.   WorldWidth  = 25;
  43.   WorldHeight = 25;
  44.   Scale       = 20;
  45.  
  46. var
  47.   GameWorld: array[1..WorldWidth, 1..WorldHeight] of TItem;
  48.  
  49. {$R *.lfm}
  50.  
  51. { TForm1 }
  52.  
  53. procedure TForm1.FormCreate(Sender: TObject);
  54. var
  55.   SnakeSegment: PRect;
  56. begin
  57.   SnakeBody := TList.Create;
  58.   PutItem(snakepos.left, snakepos.top, snake);
  59.   snakepos.left:=random(10);
  60.   snakepos.top:=random(10);
  61.   SnakeSegment^ := snakepos;
  62.   SnakeBody.Add(SnakeSegment);
  63.  
  64.   drawgameworld;
  65.  
  66. end;
  67.  
  68. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  69. begin
  70.   SnakeBody.Free;
  71. end;
  72.  
  73. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  74.   );
  75. begin
  76.   PutItem(snakepos.left, snakepos.top, snake);
  77.   Drawgameworld;
  78.   if key=vk_left then
  79.   begin
  80.   direction:=1;
  81.   end;
  82.  
  83.   if key=vk_right then
  84.   begin
  85.   direction:=2;
  86.   end;
  87.  
  88.   if key=vk_up then
  89.   begin
  90.   direction:=3;
  91.   end;
  92.  
  93.   if key=vk_down then
  94.   begin
  95.   direction:=4;
  96.   end;
  97.  
  98.      if key=VK_ADD then
  99.   begin
  100.     SnakeIsGrowing := True;
  101.   end;
  102.  
  103. end;
  104.  
  105. procedure TForm1.Timer1Timer(Sender: TObject);
  106. begin
  107.  
  108.   MoveSnake(snakepos);
  109.  
  110.   if direction = 1 then
  111.   begin
  112.   snakepos.left:=snakepos.left-1;
  113.   end;
  114.  
  115.   if direction = 2 then
  116.   begin
  117.   snakepos.left:=snakepos.left+1;
  118.   end;
  119.  
  120.   if direction = 3 then
  121.   begin
  122.   snakepos.top:=snakepos.top-1;
  123.   end;
  124.  
  125.   if direction = 4 then
  126.   begin
  127.   snakepos.top:=snakepos.top+1;
  128.   end;
  129.  
  130.  
  131.  
  132. end;
  133.  
  134. procedure TForm1.ClearWorld;
  135. var
  136.   X, Y: Integer;
  137. begin
  138.   for X := 1 to WorldWidth do
  139.     for Y := 1 to WorldHeight do
  140.       GameWorld[X, Y] := Emtpy;
  141. end;
  142.  
  143. procedure TForm1.PutItem(X, Y: Integer; Item: TItem);
  144. begin
  145.   if (X < 1) or (X > WorldWidth) then Exit;
  146.   if (Y < 1) or (Y > WorldHeight) then Exit;
  147.   GameWorld[X, Y] := Item;
  148. end;
  149.  
  150. function TForm1.GetItem(X, Y: Integer): TItem;
  151. begin
  152.   Result := Emtpy;
  153.   if (X < 1) or (X > WorldWidth) then Exit;
  154.   if (Y < 1) or (Y > WorldHeight) then Exit;
  155.   Result := GameWorld[X, Y];
  156. end;
  157.  
  158. procedure TForm1.DrawGameWorld;
  159. const
  160.   Padding = 2;
  161. var
  162.   X, Y: Integer;
  163.   ScreenX, ScreenY: Integer;
  164. begin
  165.   Refresh;
  166.   for X := 1 to WorldWidth do
  167.     for Y := 1 to WorldHeight do
  168.       begin
  169.         ScreenX := X * Scale;
  170.         ScreenY := Y * Scale;
  171.         case GameWorld[X, Y] of
  172.           Emtpy: ; // do nothing
  173.           Snake: begin
  174.                    Canvas.Pen.Color := clBlue;
  175.                    Canvas.Brush.Color := clwhite;
  176.                    Canvas.Rectangle(ScreenX, ScreenY, ScreenX+Scale-Padding, ScreenY+Scale-Padding);
  177.                  end;
  178.           Fruit: begin
  179.                    Canvas.Pen.Color := clBlack;
  180.                    Canvas.Brush.Color := clRed;
  181.                    Canvas.Ellipse(ScreenX, ScreenY, ScreenX+Scale-Padding, ScreenY+Scale-Padding);
  182.                  end;
  183.           end;
  184.       end;
  185. end;
  186.  
  187. procedure TForm1.MoveSnake(NewHead: TRect);
  188. var
  189.   SnakeSegment: PRect;
  190. begin
  191.   New(SnakeSegment);
  192.   SnakeSegment^ := NewHead;
  193.   SnakeBody.Insert(0, SnakeSegment);
  194.   if not(SnakeIsGrowing) then begin
  195.     SnakeSegment := SnakeBody[SnakeBody.Count-1];
  196.     PutItem(SnakeSegment^.Left, SnakeSegment^.Top, Emtpy);
  197.     Freemem(SnakeSegment);
  198.     SnakeBody.Delete(SnakeBody.Count-1);
  199.   end;
  200.   SnakeIsGrowing := False;
  201. end;
  202.  
  203. end.
  204.                                            
'
hi i still get the same error message when i compile it

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #57 on: September 27, 2017, 04:40:36 pm »
Okay it was my fault, I forgot to mention New(SnakeSegment), this is the correct version:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   SnakeSegment: PRect;
  4. begin
  5.   SnakeBody := TList.Create;
  6.   PutItem(snakepos.left, snakepos.top, snake);
  7.   snakepos.left:=random(10);
  8.   snakepos.top:=random(10);
  9.   New(SnakeSegment);
  10.   SnakeSegment^ := snakepos;
  11.   SnakeBody.Add(SnakeSegment);
  12.   drawgameworld;
  13. end;

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #58 on: September 27, 2017, 04:45:12 pm »
i can now compile but the snake is not moving and when try to move it, it disappears. do you know why it doesn't work?

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #59 on: September 27, 2017, 04:51:07 pm »
I knew, I am testing it. Mine is working correctly, I now trying to check where you did wrong. Give me some time.

 

TinyPortal © 2005-2018