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

#### shs

• Sr. Member
• Posts: 310
##### Re: how to delete the canvas rectangle
« Reply #105 on: October 04, 2017, 07:52:39 am »
okay thank you

is there any other of writing this code by the way?

Code: Pascal  [Select]
1. case direction of
2.         1,3,4: if key=vk_left then
3.            begin
4.            direction:=1;
5.            end;
6.         2: if key=vk_left then
7.            begin
8.            direction:=2;
9.            end;
10.       end;
11.
12.       case direction of
13.         2,3,4: if key=vk_right then
14.            begin
15.            direction:=2;
16.            end;
17.         1: if key=vk_right then
18.            begin
19.            direction:=1;
20.            end;
21.       end;
22.
23.       case direction of
24.         1,2,3: if key=vk_up then
25.            begin
26.            direction:=3;
27.            end;
28.         4: if key=vk_up then
29.            begin
30.            direction:=4;
31.            end;
32.       end;
33.
34.       case direction of
35.         1,2,4: if key=vk_down then
36.            begin
37.            direction:=4;
38.            end;
39.         3: if key=vk_down then
40.            begin
41.            direction:=3;
42.            end;
43.       end;

#### Handoko

• Hero Member
• Posts: 2566
• My goal: build my own game engine using Lazarus
##### Re: how to delete the canvas rectangle
« Reply #106 on: October 04, 2017, 08:13:28 am »
Try this:

Code: Pascal  [Select]
1. case key of
2.   vk_left:  if not(direction = 2) then direction := 1;
3.   vk_right: if not(direction = 1) then direction := 2;
4.   vk_up:    if not(direction = 4) then direction := 3;
5.   vk_down:  if not(direction = 3) then direction := 4;
6. end;

#### taazz

• Hero Member
• Posts: 5336
##### Re: how to delete the canvas rectangle
« Reply #107 on: October 04, 2017, 09:12:10 am »
is there any other of writing this code by the way?

Code: Pascal  [Select]
1. case direction of
2.         1,3,4: if key=vk_left then
3.            begin
4.            direction:=1;
5.            end;
6.         2: if key=vk_left then
7.            begin
8.            direction:=2;
9.            end;
10.       end;
11.
12.       case direction of
13.         2,3,4: if key=vk_right then
14.            begin
15.            direction:=2;
16.            end;
17.         1: if key=vk_right then
18.            begin
19.            direction:=1;
20.            end;
21.       end;
22.
23.       case direction of
24.         1,2,3: if key=vk_up then
25.            begin
26.            direction:=3;
27.            end;
28.         4: if key=vk_up then
29.            begin
30.            direction:=4;
31.            end;
32.       end;
33.
34.       case direction of
35.         1,2,4: if key=vk_down then
36.            begin
37.            direction:=4;
38.            end;
39.         3: if key=vk_down then
40.            begin
41.            direction:=3;
42.            end;
43.       end;

Code: Pascal  [Select]
2.
3. const  cDirection : array[VK_LEFT..VK_DOWN, 1..4] of integer = ((1,2,1,1),  //vk_left
4.                                                                 (3,3,3,4),  //vk_up
5.                                                                 (1,2,2,2),  //vk_right
6.                                                                 (4,4,3,4)   //vk_down
7.                                                                );
8. begin
10. end;
11.
12.
although I prefare Handoko's code it is easier to understand in a year or two.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

#### bytebites

• Full Member
• Posts: 156
##### Re: how to delete the canvas rectangle
« Reply #108 on: October 04, 2017, 10:00:47 am »
Or remove the intermediate variable direction and

Code: Pascal  [Select]
1.  case key of
2.    vk_left:
3.     if xdirection=0 then begin
4.      xdirection:=-1;
5.      ydirection:=0;
6.     end;
7.
8. etc..
9.

#### shs

• Sr. Member
• Posts: 310
##### Re: how to delete the canvas rectangle
« Reply #109 on: October 04, 2017, 10:22:00 am »
thank you guys it works really well.

but when press 2 keys really quickly it doesn't work.

for example, the snake is in direction 1 (left) but if i press key down and then key right very quickly, the direction becomes 2 (right) instead of becoming 4 (down) and then 2(right)

#### shs

• Sr. Member
• Posts: 310
##### Re: how to delete the canvas rectangle
« Reply #110 on: October 06, 2017, 09:43:30 am »
i am trying to make the restart button when the game is finished but how can i make the snake length to be 1 again? how can i delete all the tails?

#### Handoko

• Hero Member
• Posts: 2566
• My goal: build my own game engine using Lazarus
##### Re: how to delete the canvas rectangle
« Reply #111 on: October 06, 2017, 01:24:10 pm »
The pseudocode should be:

For each item in SnakeBody do begin
Set SnakeSegment to point to the memory location
Free the SnakeSegment
end
Call SnakeBody Clear

https://www.freepascal.org/docs-html/rtl/classes/tlist.clear.html

And don't forget to call ClearWorld to clear the game's world.
« Last Edit: October 06, 2017, 01:27:56 pm by Handoko »

#### shs

• Sr. Member
• Posts: 310
##### Re: how to delete the canvas rectangle
« Reply #112 on: October 06, 2017, 01:49:21 pm »
oh okay thank you!

thank you guys it works really well.

but when press 2 keys really quickly it doesn't work.

for example, the snake is in direction 1 (left) but if i press key down and then key right very quickly, the direction becomes 2 (right) instead of becoming 4 (down) and then 2(right)

can you help me with this please?

#### bytebites

• Full Member
• Posts: 156
##### Re: how to delete the canvas rectangle
« Reply #113 on: October 06, 2017, 05:54:14 pm »
See previous page.

#### Handoko

• Hero Member
• Posts: 2566
• My goal: build my own game engine using Lazarus
##### Re: how to delete the canvas rectangle
« Reply #114 on: October 06, 2017, 07:23:01 pm »
@shs

You misunderstand how the game works. You give the snake 1 move per the time you give, in your case it is 10 moves per second (or 1 move in 1/10 second).

So no matter how quickly the player press the keyboards, s/he only can move 10 tiles per second. If there are several keys in 1/10 second, only the last one counts.

You can modify the logic of the game, but I don't think that would be a good idea. For example, if I quickly and repeatably pressing forward, then the snake will move faster than it should be. If this is REALLY what you want, simple call the Time1Timer after the keypressing event.

#### shs

• Sr. Member
• Posts: 310
##### Re: how to delete the canvas rectangle
« Reply #115 on: October 07, 2017, 03:01:27 am »
yes but you know how direction changes when i press the different keys right?

Code: Pascal  [Select]
1. case key of
2.   vk_left:  if not(direction = 2) then direction := 1;
3.   vk_right: if not(direction = 1) then direction := 2;
4.   vk_up:    if not(direction = 4) then direction := 3;
5.   vk_down:  if not(direction = 3) then direction := 4;
6. end;

and this is the code i am using

so let's say the direction is 1 so it's going left. And if i press right it won't work because it only works when the direction is not 1. But if change directions really quickly bu pressing up key and right key, it just goes left instead of going up and left.

And i think this is because the direction is changed but the snake didn't move because i pressed so quickly

so it's  vk_left --> vk_up --> vk_right

direction 1 --> direction 3 --> direction 2

so the direction was changed without any problems but the snake just went to right from left without going up.
so is there any way i can fix this problem? if my explanation is not clear please compile the game and see what happens if you press two keys really quickly

#### Handoko

• Hero Member
• Posts: 2566
• My goal: build my own game engine using Lazarus
##### Re: how to delete the canvas rectangle
« Reply #116 on: October 07, 2017, 03:42:46 am »
I fully understood what you're saying but you didn't understand what I said.

These are the key points:

- You use Timer as the main game loop and you set the interval = 100.
- Your FormKeyDown event only set the direction without calling update.
- You update the snake movement in the Timer event only.

#### shs

• Sr. Member
• Posts: 310
##### Re: how to delete the canvas rectangle
« Reply #117 on: October 07, 2017, 03:55:59 am »
so what should i do to fix these problems?

#### Handoko

• Hero Member
• Posts: 2566
• My goal: build my own game engine using Lazarus
##### Re: how to delete the canvas rectangle
« Reply #118 on: October 07, 2017, 04:00:20 am »