Recent

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

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #135 on: October 07, 2017, 12:24:47 pm »
i don't know how i should start coding for those ideas

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #136 on: October 07, 2017, 12:58:22 pm »
You didn't tell us which one do you choose.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #137 on: October 07, 2017, 01:28:48 pm »
creating buffer

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #138 on: October 07, 2017, 01:29:54 pm »
You picked the hardest one. :D

As usual, give me some time.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #139 on: October 07, 2017, 01:50:42 pm »
oh thank you please take your time :)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: how to delete the canvas rectangle
« Reply #140 on: October 07, 2017, 06:55:38 pm »
oh thank you please take your time :)
Why are you not trying this yourself?
Just asking for code and solutions isn't going to make you learn to code, I can assure you.

Just try it really simple.
You want to have a buffer. A buffer is just an array. Let's say an array of 5 words (5 keys maximum).

You want to do the FIFO method. (First in, First out)
So you are going to put, lets say 3 words in the array from the beginning.
All the steps:
0 0 0 0 0
Then you put 1 in
1 0 0 0 0
Then you put 2 in
2 1 0 0 0
Then put 3 in
3 2 1 0 0

Now when you want to take out a number you just go from the end and traverse back to the beginning until you have a word <> 0. You change the direction but that's for later concern.

Could you code the first step... putting those 3 numbers in the array? As you can see for the second number you need to push the whole array to the right 1 place. Remember we did this for you highscore ranking procedure. Just try to program this first (trust me, otherwise you'll never learn).

Framework:
Code: Pascal  [Select][+][-]
  1. Program test;
  2. var
  3.   Buffer: array[1..5] of Word;
  4. begin
  5.   // go ahead. first set all the buffer to 0
  6.   // and then put 1, 2 and 3 in, shifting the numbers on each step.
  7. end.
« Last Edit: October 07, 2017, 07:07:38 pm by rvk »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #141 on: October 07, 2017, 09:51:56 pm »
@rvk

I don't think TS was asking for the code. He perhaps never know what a data buffer is.

@ shs

I won't explain what a buffer is, because you can read here:
https://en.wikipedia.org/wiki/Data_buffer

I also won't explain the advantages of using buffer, why and when because someday you will know it by yourself.

First, you need to know why your code doesn't work (as what you want) if you press arrow keys quickly.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #142 on: October 07, 2017, 09:54:22 pm »
So what we're going to do is to add a buffer to your code. But you need understand how the concept works.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: how to delete the canvas rectangle
« Reply #143 on: October 07, 2017, 11:04:40 pm »
I don't think TS was asking for the code.
Mmm, Ok, I thought otherwise.

umm nope more explanations please?
can you give me an example of the code please?
I also explained a lot about arrays in another topic to TS. So he could use that to at least begin coding this himself (just in a simple console project to get an understanding of a buffer). (see the topic about calendar which turned into a highscore ranking and sorting topic).

Seeing the level of understanding there, I don't think he's quite there yet....

But I'll leave it up to you how fast or slow you are going with this.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #144 on: October 08, 2017, 07:00:34 am »
I also explained a lot about arrays in another topic to TS. So he could use that to at least begin coding this himself (just in a simple console project to get an understanding of a buffer). (see the topic about calendar which turned into a highscore ranking and sorting topic).

Seeing the level of understanding there, I don't think he's quite there yet....

I knew and I agree. And that's why I don't want to join in the 2 balls game.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #145 on: October 08, 2017, 07:04:00 am »
@shs

Many data collections can be used to create a data buffer. Using array or TList are suitable for your case. Because rvk already gave you the explanation of using array, I will provide you the method of using TList.

I think you have pushed yourself to try to do the things beyond your level of understanding. So I will provide you the code of KeyBuffer, which I wrote in simple and easy to understand way so you can study it if you want.

You need to put this in the data declaration section:
Code: Pascal  [Select][+][-]
  1. var
  2.   KeyBuffer: TList;

And this is the code of KeyBuffer:
Code: Pascal  [Select][+][-]
  1. procedure KeyBufferSetSize(NewSize: Integer);
  2. var
  3.   DataSegment: ^Word;
  4.   i: Integer;
  5. begin
  6.   if (NewSize < 1) or (NewSize > 10) then Exit;
  7.   if (NewSize < KeyBuffer.Count) then
  8.     for i := NewSize to (KeyBuffer.Count-1) do // Free the allocated memory
  9.       begin
  10.         DataSegment := KeyBuffer[i];
  11.         Freemem(DataSegment);
  12.       end;
  13.   KeyBuffer.Capacity := NewSize;
  14. end;
  15.  
  16. procedure KeyBufferStore(Key: Word);
  17. var
  18.   DataSegment: ^Word;
  19. begin
  20.   if (KeyBuffer.Count = KeyBuffer.Capacity) then Exit; // The buffer is full
  21.   New(DataSegment);
  22.   DataSegment^ := Key;
  23.   KeyBuffer.Add(DataSegment);
  24. end;
  25.  
  26. function KeyBufferUse: Word;
  27. var
  28.   DataSegment: ^Word;
  29. begin
  30.   Result := VK_UNDEFINED;
  31.   if (KeyBuffer.Count <= 0) then Exit; // The buffer is empty
  32.   DataSegment := KeyBuffer[0];
  33.   Result := DataSegment^;
  34.   Freemem(DataSegment);
  35.   KeyBuffer.Delete(0);
  36. end;
  37.  
  38. function KeyBufferPeek(Index: Integer): Word;
  39. var
  40.   DataSegment: ^Word;
  41. begin
  42.   Result := VK_UNDEFINED;
  43.   if (KeyBuffer.Count <= 0) then Exit; // The buffer is empty
  44.   if (Index < 0) or (Index >= KeyBuffer.Count) then Exit; // Invalid index
  45.   DataSegment := KeyBuffer[Index];
  46. end;

Note:
Item on the lower position in the buffer uses smaller index, the bottommost has index = 0.
« Last Edit: October 08, 2017, 07:09:28 am by Handoko »

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: how to delete the canvas rectangle
« Reply #146 on: October 08, 2017, 08:11:01 am »
TQueue would apply here.

Code: Pascal  [Select][+][-]
  1. uses contnrs;
  2. var KeyBuffer:TQueue;
  3.  
  4. function KeyBufferUse: Word;
  5. var
  6.   DataSegment: ^Word;
  7. begin
  8.   DataSegment := KeyBuffer.Pop;
  9.   if DataSegment=nil then exit(VK_UNDEFINED);
  10.   Result := DataSegment^;
  11.   Freemem(DataSegment);
  12. end;      

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: how to delete the canvas rectangle
« Reply #147 on: October 08, 2017, 08:15:32 am »
Yes, you're right.

TQueue will be more appropriate for this case but because the TS has just learned how to use TList recently so I show him using TList. Also he's just a novice, it will hard for him to understand if we show him too many new things in a very short period.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: how to delete the canvas rectangle
« Reply #148 on: October 09, 2017, 10:08:11 am »
wow that's a lot thank you so much i will ask you some questions if i don't get it.
also for restarting the game

i did this
Code: Pascal  [Select][+][-]
  1.  For i := 0 to (SnakeBody.Count - 1) do
  2.     begin
  3.       SnakeSegment := SnakeBody[i];
  4.       FreeMem(SnakeSegment);
  5.     end;
  6.    SnakeBody.clear;        

but the tails are still remained

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: how to delete the canvas rectangle
« Reply #149 on: October 09, 2017, 01:07:49 pm »
Code: Pascal  [Select][+][-]
  1. ClearWorld;

 

TinyPortal © 2005-2018