How to resolve the algorithm Joystick position step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Joystick position step by step in the Action! 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 Action! programming language

Source code in the action! programming language

BYTE lastStick=[255]
BYTE lastTrig=[255]

PROC DrawCross(BYTE s)
  BYTE size=[5]
  CARD x
  BYTE y

  IF s>=9 AND s<=11 THEN
    x=size
  ELSEIF s>=5 AND s<=7 THEN
    x=159-size
  ELSE
    x=79
  FI

  IF s=6 OR s=10 OR s=14 THEN
    y=size
  ELSEIF s=5 OR s=9 OR s=13 THEN
    y=79-size
  ELSE
    y=39
  FI
  
  Plot(x-size,y)
  DrawTo(x+size,y)
  Plot(x,y-size)
  DrawTo(x,y+size)
RETURN

PROC UpdateStatus(BYTE currStick,currTrig)
  IF currStick#lastStick THEN
    Color=0 DrawCross(lastStick)
    Color=1 DrawCross(currStick)
    lastStick=currStick
  FI

  IF currTrig#lastTrig THEN
    Print("Button pressed: ")
    IF currTrig THEN
      PrintE("no ")
    ELSE
      PrintE("yes")
    FI
    Put(28) ;move cursor up
    lastTrig=currTrig
  FI
RETURN

PROC Main()
  BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6,
    CRSINH=$02F0 ;Controls visibility of cursor
  BYTE currStick,currTrig

  Graphics(7)
  Color=1
  COLOR1=$0C
  COLOR2=$02
  CRSINH=1 ;hide cursor

  DO
    currStick=Stick(0)
    currTrig=STrig(0)
    UpdateStatus(currStick,currTrig)
  UNTIL CH#$FF
  OD
  CH=$FF
RETURN

  

You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Simula programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the EGL programming language
You may also check:How to resolve the algorithm Anonymous recursion step by step in the WDTE programming language
You may also check:How to resolve the algorithm Cantor set step by step in the Python programming language
You may also check:How to resolve the algorithm A+B step by step in the Nutt programming language