A single timer and a few images combine to result in a simple game.
Three windows METAFILES race across a form until one wins. The race
action is controlled by a single timer, with some simple buttons to
control gameplay.
The Components:
What follows is a list of components needed for this application,
along with some properties that need to be set for those components.
Be systematic and TIDY placing your components.
- save your project, naming the FORM file lakeside.pas and
the PROJECT file as speedy.dpr. change the CAPTION property
of the form to Go Speed Racer!, change the NAME property
of the form to read racetrack.
- Add a BUTTON to the form, change it's NAME to quit and
change it's CAPTION to read &Quit
- Add a BUTTON to the form, change it's NAME to raceControl
and change it's CAPTION to read &Start
- Add a TIMER to the form, change it's NAME to raceTimer,
change it's ENABLED property to FALSE, change it's INTERVAL
to 1
- Add a LABEL to the form, change it's NAME to info, change
it's CAPTION property to read Go Speed Racer!, and change
it's FONT pitch and colour to suit.
- Add an IMAGE to the form, change it's NAME to car1, load
the image red.wmf into it, set the STRETCH
property toTRUE, position it on the form, then set it's LEFT
property to 1. Change it's HEIGHT to 50, change it's
WIDTH to 60. If your browser has screwy file associations,
a zip file of all the graphics used is also available. JPGS are also available
- Do the same as previous step for IMAGES called car2 (load
green.wmf) and car3 (load blue.wmf)
The Event Handlers
There are a number of event handlers that are necessary for the successful
operation of this project. The complication here is that we have to
detect (and therefore react to) one or more images reaching the right-hand
edge of the form...
Control Name |
Event Handler Code |
raceControl.onClick |
if raceControl.caption = '&Start'
then begin
raceControl.caption := 'Sto&p';
info.caption := 'Go Speed Racer!'
end
else raceControl.caption := '&Start';
raceTimer.enabled := not(raceTimer.enabled);
car1.left := 1;
car2.left := 1;
car3.left := 1
|
quit.onClick |
close;
|
raceTimer.onTimer |
var racing : boolean;
begin
car1.left := car1.left + random(3);
car2.left := car2.left + random(3);
car3.left := car3.left + random(3);
racing := not(((car1.left + car1.width) >= clientWidth) or
((car2.left + car2.width) >= clientWidth) or
((car3.left + car3.width) >= clientWidth));
raceTimer.enabled := racing;
if not racing
then begin
if (car1.left + car1.width) >= clientWidth
then info.caption := 'Racer 1 WON!'
else if (car2.left + car2.width) >= clientWidth
then info.caption := 'Racer 2 WON!'
else info.caption := 'Racer 3 WON!';
raceControl.caption := '&Start';
end;
end;
|
raceTrack.formCreate |
randomize;
|
Final Touches
COMPILE the program to check for errors of 'SYNTAX',
fix any that show, then RUN it to check it works correctly.
Check it terminates correctly as one car hits the right-hand edge
of the form. RaceControl.caption and info.caption should change at
the end of a race.
Set the COLOR property of the form (ie. racetrack.color) to black
to improve the animation.
Experts would realise that the current project does not allow
ties (or rather ignores them, reporting only the first racer (in
the list of 1..3) that has finished. A good addition to this system
would be the correct reporting of ties