Practical lesson
In this practical lesson a four-button application, the “Window pilot”, is created: “Right”, “Left”, “Down”, “Up”. With each click the window moves 10 pixels: right – Left +10, left – Left –10, down – Top +10, up – Top –10. Each button’s OnClick procedure gets one statement, for example (code below). Run the application with F9 and press the buttons one after another: compare the movement of the window with the result you calculated. To understand why some buttons do not work, first check the button names in the Object Inspector and then the procedure name (Button1Click, Button2Click …): each procedure must belong to its own button.
1procedure TForm1.Button3Click(Sender: TObject); 2begin 3 Form1.Top := Form1.Top + 10; 4end;
A game character moves with arrow buttons, one square per press. If the buttons are pressed three times right and once up, you can work out where the character will be without playing. Programmers use this to check in advance that a program works correctly.
Game programmers and developers of robot vacuum cleaners and map apps calculate movement as a series of steps.
On squared paper mark a starting square and let a friend say commands such as “right, right, up”; before moving the pencil, say in advance which square you will end on.
| Button | Presses | Change |
|---|---|---|
| Right | 3 | Left + 30 |
| Down | 2 | Top + 20 |
| Left | 1 | Left – 10 |
| Result | Left = 220, Top = 120 |
“The pilot’s route”: a pair writes a route of 6 button presses; another pair calculates the final Left and Top and checks them with the application.