1. No Need To Use "(" and ")"If the procedure doesn't have any parameter, you can remove that brackets:
procedure Adding;
procedure Delete;
You don't have to, but I personally will move the
Adding and
Delete declaration to private section. I left the auto generated by IDE on the top (like FormCreate, FormKeyDown, Timer1Timer). See point #2 below.
2. Put Things On The Lowest ScopeThere are 3 visibilities (actually there are 6) in class:
- Private
- Protected
- Public
It is good to put your variables, functions or procedures on the lowest visibility scope unless you really need to put them on the higher visibility levels.
So your "
box" and "
food" declaration should be in
private section:
private
Box: array of TRect;
Food: TRect;
end;
I personally will put my procedures and functions on this section too (Adding, Delete).
3. BoxLength Should Be Started With 1Do you not think it is strange, your BoxLength started with the value of 2? So, change the declaration of BoxLength with this:
And you also need to change the code of BoxLength in
Timer1Timer to become:
if Length(Box) > BoxLength then Delete;
4. Use Better Names rx, ry : Integer;
scl = 20;
If you ever tried to the read codes written by professionals, you will notice they always use meaningful names. It will make the code easier to understand especially when the code grows to thousands of lines. Except for i, j, k ..., which are used for looping.
5. Use Space To Make It More ReadableThis code below will look nicer:
case Key of
vk_Right: Direction := 1;
vk_Left: Direction := 2;
vk_Down: Direction := 3;
vk_Up: Direction := 4;
end;
6. No Need To Use Begin-End For Single Line For-LoopYou can simplify the for-loop statement by removing the begin-end if it only contains 1 command. So the for-loop in your Delete procedure will become:
for i:= 1 to Length(Box)-1 do Box[i-1] := Box[i];
Alternatively, some programmers will prefer to write it in 2 lines:
for i:= 1 to Length(Box)-1 do
Box[i-1] := Box[i];
7. Use Camel CaseExcept for reserved keywords, using camel case will make the code looks better.
https://en.wikipedia.org/wiki/Camel_case