home *** CD-ROM | disk | FTP | other *** search
- program JoyDemo2;
-
- {
- Author: David Howorth
- Last updated: March 29, 1987
- Application: A demonstration of procedure ReadJoy, a procedure for
- reading the joystick, using variable parameters. Shows
- how to use the joystick as a DIRECTION indicator.
- }
- {$C-}
- { Above compiler directive necessary so that 'keypressed' function works
- properly. Also speeds up screen output. }
-
- var
- ch : char;
- Done : boolean;
- InitX,
- InitY : integer;
- JoyX,
- JoyY,
- Dummy : integer;
- ScreenX,
- ScreenY : byte;
-
- {$I READJOY.INC}
-
- {--------------------------------------}
-
- procedure SetUpScreen;
-
- {-------------}
-
- procedure Box(x1,y1,x2,y2 : integer);
-
- var
- i : integer;
-
- begin
- gotoXY(x1,y1);
- write(char(218));
- for i := x1 + 1 to x2 - 1 do write(char(196));
- write(char(191));
- for i := y1 + 1 to y2 - 1 do
- begin
- gotoXY(x1,i);
- write(char(179));
- gotoXY(x2,i);
- write(char(179));
- end;
- gotoXY(x1,y2);
- write(char(192));
- for i := x1 + 1 to x2 - 1 do write(char(196));
- write(char(217));
- end;
-
- {-------------}
-
- begin
- clrscr;
- writeln(' JOYSTICK DEMONSTRATION NUMBER 2');
- writeln;
- writeln(' (Joystick as direction indicator)');
- gotoxy(1,25);
- write(' Press ''Q'' to quit.');
- Box(1,5,80,24);
- end;
-
- {--------------------------------------}
-
- begin { main program }
- ReadJoy(InitX,InitY,Dummy,Dummy); { calibrate joystick }
-
- { Note that ReadJoy expects four parameters, even if your program doesn't
- need them all, as it won't if you are only reading one joystick. As for
- the parameters you don't care about, there's no harm in passing the same
- one twice, as done here. }
-
- SetUpScreen;
- while keypressed do read(kbd,ch); { empty keyboard buffer }
- Done := false;
- ScreenX := 40;
- ScreenY := 14;
- repeat
- gotoxy(ScreenX,ScreenY);
- write(' ');
- ReadJoy(JoyX,JoyY,Dummy,Dummy);
- if JoyX > InitX + (InitX div 5)
- then ScreenX := ScreenX + 1
- else if JoyX < InitX - (InitX div 5)
- then ScreenX := ScreenX - 1;
- if JoyY > InitY + (InitY div 5)
- then ScreenY := ScreenY + 1
- else if JoyY < InitY - (InitY div 5)
- then ScreenY := ScreenY - 1;
-
- { If all you are interested in is what direction the joystick has been moved,
- all you need to do is compare the current position with the initial position
- calibrated by the program. If you want your program to be relatively
- portable, you ought to make the comparison in as relative terms as possible,
- i.e., use something like 'if JoyX > InitX + (InitX div 5)', rather than
- something like 'if JoyX > InitX + 10'. It's possible the latter expression
- won't work effectively with some makes of joystick or at the speed of some
- chips. }
-
- if ScreenX > 78 then ScreenX := 78; { make sure }
- if ScreenX < 2 then ScreenX := 2; { the 'X ' }
- if ScreenY > 23 then ScreenY := 23; { doesn't get }
- if ScreenY < 6 then ScreenY := 6; { out of the box }
- gotoxy(ScreenX,ScreenY);
- write('X');
- delay(30); { slow things down a bit }
- if keypressed
- then begin
- read(kbd,ch);
- if upcase(ch) = 'Q' then Done := true;
- end;
- until Done;
- clrscr;
- end.