The user holds down a mouse button and drags it around to form a track, then presses the Start button to have the robot follow it. There are separate methods for OnMouseDown, OnMouseMove and OnMouseUp events. The MouseDownprocedure sets a flag to tell the MouseMove routine that we're drawing, it also does some initialization stuff like setting a nice fat pen we'll use for drawing the track. MouseMove draws a line from the last point to the current point. It also waits for 10ms so that we won't create more than about 100 points per second of drawing. MouseUp resets the flag and moves the robot to the beginning point.
When the Start button is pressed, (creating an OnClick event), we just start moving the robot point by point to retrace the steps. The same 10ms delay is inserted between moves so it should move about as fast as the original line drawing speed. The only tricky thing about positioning the robot is that we want to center it on the track. All references to locations are by the top left corner though so we subtract half the width and half the height of the robot from the current point to get a location for its top left corner.
A second version is included in the source download. It has a OnPaint routine to redraw the track after the robot moves over it and a few other enhancements that I'll leave for you to check out on your own.
Running/Exploring the Program
Download source | |
Download executable |
0 comments: