Author Topic: A demo of IMP vs OR for moving character on the screen  (Read 3184 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
A demo of IMP vs OR for moving character on the screen
« on: March 26, 2021, 05:00:56 am »
Hi guys and gals

Here I post a demonstration to show how to use IMP in the logical managing of the move of a character on the screen and the alternative more known OR.  In the top left corner you can see the result of detector FUNCTION, in the Top Right corner you can see the type of detector FUNCTION at work, in the bottom of the window you can read the help for user, the title of window remembers you that it is a Demo.

Code: QB64: [Select]
  1. ' Demo in two languages  / Dimostrazione in due linguaggi
  2. Screen 0 'screen text mode / modalit… schermo solo testo
  3. _Title "Demo OR and IMP to move a character on screen" ' setting title of window / impostare il titolo della finestra
  4. ' defining useful constants  / definire costanti utili
  5. Const X = 1, Y = 2, Life = 3, True = -1, False = 0, ModeOr = 10, ModeImp = 20
  6. ' defining variables and arrays  / definire variabili ed array
  7. Dim Hero(1 To 3) As Integer, Limit(1 To 2, 1 To 2) As Integer, ModeEvaluation As Integer
  8.  
  9. 'initialization of variables  / inizializzazione delle variabili
  10. Hero(X) = (Rnd * 79) + 1
  11. Hero(Y) = (Rnd * 22) + 1
  12. Hero(Life) = True
  13. Limit(1, X) = 10
  14. Limit(1, Y) = 2
  15. Limit(2, X) = 70
  16. Limit(2, Y) = 19
  17. ModeEvaluation = ModeImp
  18.  
  19. ' main loop  /ciclo principale
  20.     Cls ' refresh screen  / ripulisce lo schermo
  21.  
  22.     ' manage evaluation mode and its results / gestisce la modalit… di valutazione e i suoi risultati
  23.     Locate 1, 1
  24.     If ModeEvaluation = ModeImp Then
  25.         If outheRange(Limit(1, X), Limit(2, X), Hero(X)) Or outheRange(Limit(1, Y), Limit(2, Y), Hero(Y)) Then Print "Out of game" Else Print " In game"
  26.  
  27.     ElseIf ModeEvaluation = ModeOr Then
  28.         If IntheRange(Limit(1, X), Limit(2, X), Hero(X)) And IntheRange(Limit(1, Y), Limit(2, Y), Hero(Y)) Then Print " In Game" Else Print "Out Of Game"
  29.     End If
  30.  
  31.     ' border of game field  / bordo del campo di gioco
  32.     Locate Limit(1, Y), Limit(1, X)
  33.     Print String$(Abs(Limit(1, X) - Limit(2, X)), Chr$(178));
  34.     For a = Limit(1, Y) To Limit(2, Y) Step 1
  35.         Locate a, Limit(1, X): Print Chr$(178)
  36.         Locate a, Limit(2, X): Print Chr$(178)
  37.     Next
  38.     Locate Limit(2, Y), Limit(1, X)
  39.     Print String$(Abs(Limit(1, X) - Limit(2, X)), Chr$(178))
  40.  
  41.     'hero / l'eroe
  42.     Locate Hero(Y), Hero(X)
  43.     Print "";
  44.  
  45.     'Helpscreen   / la guida a schermo
  46.     Locate 1, 60: Print "ModeEvaluation: ";
  47.     If ModeEvaluation = ModeImp Then Print "IMP"; Else Print " OR";
  48.     Locate 24, 1: Print " cursors to  move Hero, Enter key to switch ModeEvaluation, Space to quit";
  49.  
  50.     ' input and command manager     / gestore dell'input e dei comandi
  51.     a$ = InKey$
  52.     Select Case a$
  53.         Case Chr$(0) + "M"
  54.             If Hero(X) < 80 Then Hero(X) = Hero(X) + 1
  55.         Case Chr$(0) + "H"
  56.             If Hero(Y) > 1 Then Hero(Y) = Hero(Y) - 1
  57.         Case Chr$(0) + "P"
  58.             If Hero(Y) < 25 Then Hero(Y) = Hero(Y) + 1
  59.         Case Chr$(0) + "K"
  60.             If Hero(X) > 1 Then Hero(X) = Hero(X) - 1
  61.         Case "K"
  62.             Hero(Life) = False
  63.         Case Chr$(32)
  64.             End
  65.         Case Chr$(13)
  66.             If ModeEvaluation = ModeImp Then ModeEvaluation = ModeOr Else ModeEvaluation = ModeImp
  67.     End Select
  68.     _Limit 20 ' save CPU  / risparmia la CPU
  69. End ' Endo of program  /fine del programma
  70.  
  71. ' SUB and FUNCTION area  / area delle SUB e delle FUNCTION
  72. Function outheRange (min As Integer, max As Integer, value As Integer)
  73.     outheRange = (min < value Imp value > max)
  74.  
  75. Function IntheRange (min As Integer, max As Integer, Value As Integer)
  76.     If min > Value Or max < Value Then IntheRange = 0 Else IntheRange = -1
  77.  

As you can observe, if you run the demo, there is a little difference between detection using IMP and using OR, but for all values inner to  the range, that's no matter, while the difference is visible on the borderline, so each tecnique need an adjustment to be more accurate.

Thanks to read and to try
Programming isn't difficult, only it's  consuming time and coffee