How to resolve the algorithm Joystick position step by step in the Applesoft BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Joystick position step by step in the Applesoft BASIC programming language

Table of Contents

Problem Statement

The task is to determine the joystick position and represent this on the display via a crosshair. For a centred joystick, the crosshair should appear in the centre of the screen. If the joystick is pushed left or right, then the cross hair should move left or right according to the extent that the joystick is pushed. If the joystick is pushed forward or pulled back, then the crosshair should move up or down according to the extent that that joystick is pushed or pulled. The edges of the display represent maximum extents for joystick movement. For example, a joystick pushed fully forward would raise the crosshair to the top centre of the screen. A joystick pulled backwards and to the right would move the crosshair to the bottom right of the screen (except for a small area reserved to show joystick status). Implementations can use a graphical display method to produce the crosshair, or alternatively represent the crosshair using a plus symbol on a terminal, and move the plus symbol position according to the joystick. The bottom part of the display can hide or show an alphanumeric sequence to represent the buttons pressed. For example, if pushbuttons 1,4 and 10 are depressed, we could display "1 4 A". The implemented code should continue to redraw the crosshair according to the joystick position and show the current pushbutton statuses until the task is terminated. Digital joysticks that produce no extent data, should have their position indicated as full extent movement of the crosshair. For the purpose of this task, we assume that the joystick is calibrated and that the first joystick is being used. The task implementer could at their option provide a solution that includes a joystick selection facility, enabling the user to choose which joystick is to be used for this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Joystick position step by step in the Applesoft BASIC programming language

Source code in the applesoft programming language

100 GOSUB 400
110 P2 = PDL(2) : IF P2 <> O2 THEN O2 = P2 : VTAB 23 : HTAB 33 : PRINT P2; TAB(37);
120 B2 = FNB(2) : IF B2 <> N2 THEN N2 = B2 : VTAB 24 : HTAB 15 : PRINT P$(B2);
130 P3 = PDL(3) : IF P3 <> O3 THEN O3 = P3 : VTAB 23 : HTAB 37 : PRINT P3; : CALL -868
140 X = INT(P3 * RX) : Y = INT(P2 * RY(F))
150 O = (X1 = X2 AND Y1 = Y2) + 1
160 N = (X = X1 AND Y = Y1) + 1
170 IF X <> X2 OR Y <> Y2 THEN XDRAW N AT X, Y : XDRAW O AT X2, Y2 : X2 = X : Y2 = Y : O = N
200 P0 = PDL(0) : IF P0 <> O0 THEN O0 = P0 : VTAB 22 : HTAB 33 : PRINT P0; TAB(37);
210 B0 = FNB(0) : IF B0 <> N0 THEN N0 = B0 : VTAB 22 : HTAB 15 : PRINT P$(B0);
220 P1 = PDL(1) : IF P1 <> O1 THEN O1 = P1 : VTAB 22 : HTAB 37 : PRINT P1; : CALL -868
230 B1 = FNB(1) : IF B1 <> N1 THEN N1 = B1 : VTAB 23 : HTAB 15 : PRINT P$(B1);
240 X = INT(P0 * RX) : Y = INT(P1 * RY(F))
250 O = (X1 = X2 AND Y1 = Y2) + 1
260 N = (X = X2 AND Y = Y2) + 1
270 IF X <> X1 OR Y <> Y1 THEN XDRAW N AT X, Y : XDRAW O AT X1, Y1 : X1 = X : Y1 = Y
300 K = PEEK(-16384) : IF K < 128 THEN 110
310 POKE-16368,0
320 IF K = 198 OR K = 200 THEN F = PEEK(-16302) ^ 0 : GOTO 110HIDE
330 IF K = 155 OR K = 211 THEN F = PEEK(-16301) * 0 : GOTO 110SHOW
340 TEXT : END
400 HOME : HGR
410 DEF FN B(B) = PEEK(49249 + B) > 127
420 P$(0) = "NOT PRESSED" : P$(1) = "PRESSED    "
430 VTAB 21 : PRINT "BUTTON:";
440 PRINT TAB(28); "JOYSTICK:"
450 PRINT "   OPEN APPLE "P$(0);
460 PRINT TAB(29); "ONE"
470 PRINT " CLOSED APPLE "P$(0);
480 PRINT TAB(29); "TWO" 
490 PRINT TAB(9); "SHIFT "P$(0);
500 RX = 35 / 32 : RY(0) = 5 / 8 : RY(1) = 3 / 4
510 DATA2,0,6,0,3,0,29,15,20,6,0
520 FOR I = 768 TO 778 : READ B
530 POKE I, B : NEXT : POKE 232, 0 : POKE 233, 3
550 ROT = 0 : SCALE = 7 : XDRAW 1 AT X1, Y1
560 O0 = -1 : O1 = O0 : O2 = O0 : O3 = O0
570 RETURN

  

You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Frink programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the min programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Asymptote programming language
You may also check:How to resolve the algorithm Factorial step by step in the KonsolScript programming language
You may also check:How to resolve the algorithm Median filter step by step in the Action! programming language