Author Topic: an ASCII emulated light moving on the screen SUB vs GOSUB version  (Read 4065 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi guys

thanks to all you to read this post...
I have tried to emulate an ASCII screen in 32bit  to create a light moving with arrows...

here is
Code: QB64: [Select]
  1. DIM SHARED A AS LONG, whites(1 TO 50) AS LONG, Black AS LONG
  2. DIM min AS INTEGER, max AS INTEGER, up AS INTEGER, down AS INTEGER
  3.  
  4. Setup
  5. CalculateRows
  6.  
  7. col = 1
  8. row = 10
  9. min = 1
  10. max = 44
  11. up = 1
  12. down = 23
  13.  
  14.     IF _KEYDOWN(19200) THEN
  15.         LOCATE 27, 1: COLOR whites(1): PRINT "Left"
  16.         IF col > min THEN col = col - 1
  17.     END IF
  18.     IF _KEYDOWN(19712) THEN
  19.         LOCATE 27, 1: COLOR whites(1): PRINT "Right"
  20.         IF col < max THEN col = col + 1
  21.     END IF
  22.     IF _KEYDOWN(18432) THEN
  23.         LOCATE 27, 1: COLOR whites(1): PRINT "Up"
  24.         IF row > up THEN row = row - 1
  25.     END IF
  26.     IF _KEYDOWN(20480) THEN
  27.         LOCATE 27, 1: COLOR whites(1): PRINT "Down"
  28.         IF row < down THEN row = row + 1
  29.     END IF
  30.  
  31.     MoveLight row, col, 10, 1, 1
  32.     LOCATE 26, 1: COLOR _RGB(255, 255, 55), Black: PRINT " press arrows to move light, space to quit ";
  33.     _LIMIT 15
  34.     CLS
  35.  
  36. SUB MoveLight (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  37.     outputs row, col, min, max, -steps
  38.     outputs row, min + col, max, min, steps
  39.     outputs row + 1, col, min, max, -steps
  40.     outputs row + 1, min + col, max, min, steps
  41.     outputs row + 2, col, min, max, -steps
  42.     outputs row + 2, min + col, max, min, steps
  43.  
  44. SUB outputs (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  45.     LOCATE row, col
  46.     FOR r = min TO max STEP steps
  47.         COLOR Black, whites(r): PRINT "0";
  48.     NEXT
  49.  
  50. SUB Setup
  51.     A = _NEWIMAGE(500, 500, 32)
  52.     IF A THEN SCREEN A ELSE PRINT "Error create SCREEN"
  53.     Black = _RGB(0, 0, 0)
  54.     whites(1) = _RGB(255, 255, 255)
  55.     r = 255
  56.     b = 255
  57.     g = 255
  58.     counter = 1
  59.     FOR counter = 1 TO 10
  60.         r = r - INT(255 / 10)
  61.         b = b - INT(255 / 10)
  62.         g = g - INT(255 / 10)
  63.         whites(counter) = _RGB(r, g, b)
  64.     NEXT counter
  65.  
  66. SUB CalculateRows
  67.     LOCATE 1, 1: COLOR _RGB(0, 127, 127)
  68.     FOR x = 1 TO 80 STEP 1
  69.         IF x > -1 THEN PRINT LTRIM$(STR$(x MOD 10));
  70.     NEXT x
  71.     SLEEP 3
  72.  
  73.  

and here is the same code  modded to using GOSUB at the place of SUBs
Code: QB64: [Select]
  1. DIM A AS LONG, whites(1 TO 50) AS LONG, Black AS LONG
  2. DIM row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER
  3. DIM row1 AS INTEGER, col1 AS INTEGER
  4. DIM Smin AS INTEGER, Smax AS INTEGER, Sup AS INTEGER, Sdown AS INTEGER
  5.  
  6. GOSUB Setup
  7.  
  8. GOSUB CalculateRows
  9.  
  10. col = 1
  11. row = 10
  12. Smin = 1
  13. Smax = 44
  14. Sup = 1
  15. Sdown = 23
  16. col1 = 0
  17. row1 = 0
  18. steps = 0
  19.     IF _KEYDOWN(19200) THEN
  20.         LOCATE 27, 1: COLOR whites(1): PRINT "Left"
  21.         IF col > Smin THEN col = col - 1
  22.     END IF
  23.     IF _KEYDOWN(19712) THEN
  24.         LOCATE 27, 1: COLOR whites(1): PRINT "Right"
  25.         IF col < Smax THEN col = col + 1
  26.     END IF
  27.     IF _KEYDOWN(18432) THEN
  28.         LOCATE 27, 1: COLOR whites(1): PRINT "Up"
  29.         IF row > Sup THEN row = row - 1
  30.     END IF
  31.     IF _KEYDOWN(20480) THEN
  32.         LOCATE 27, 1: COLOR whites(1): PRINT "Down"
  33.         IF row < Sdown THEN row = row + 1
  34.     END IF
  35.     min = 10
  36.     max = 1
  37.     steps = 1
  38.     GOSUB MoveLight
  39.     LOCATE 26, 1: COLOR _RGB(255, 255, 55), Black: PRINT " press arrows to move light, space to quit ";
  40.     _LIMIT 15
  41.     CLS
  42.  
  43.  
  44.  
  45. MoveLight:
  46.  
  47. col1 = col
  48. steps = -steps
  49. row1 = row
  50. GOSUB outputs
  51. row1 = row1 + 1
  52. GOSUB outputs
  53. row1 = row1 + 1
  54. GOSUB outputs
  55.  
  56. SWAP min, max
  57. steps = -steps
  58. col1 = col1 + 10
  59. row1 = row
  60. GOSUB outputs
  61. row1 = row1 + 1
  62. GOSUB outputs
  63. row1 = row1 + 1
  64. GOSUB outputs
  65.  
  66. outputs:
  67. LOCATE row1, col1
  68. FOR r = min TO max STEP steps
  69.     COLOR Black, whites(r): PRINT "0";
  70.  
  71. Setup:
  72. A = _NEWIMAGE(500, 500, 32)
  73. IF A THEN SCREEN A ELSE PRINT "Error create SCREEN"
  74. Black = _RGB(0, 0, 0)
  75. whites(1) = _RGB(255, 255, 255)
  76. r = 255
  77. b = 255
  78. g = 255
  79. counter = 1
  80. FOR counter = 1 TO 10
  81.     r = r - INT(255 / 10)
  82.     b = b - INT(255 / 10)
  83.     g = g - INT(255 / 10)
  84.     whites(counter) = _RGB(r, g, b)
  85. NEXT counter
  86.  
  87. CalculateRows:
  88. LOCATE 1, 1: COLOR _RGB(0, 127, 127)
  89. FOR x = 1 TO 80 STEP 1
  90.     IF x > -1 THEN PRINT LTRIM$(STR$(x MOD 10));
  91.  

thanks for feedback

Here attached files for who doesn't like to select-copy-paste from codeboxes.

Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #1 on: April 20, 2019, 05:09:17 pm »
Sorry I have missed the background that is here:

Code: QB64: [Select]
  1. DIM SHARED A AS LONG, whites(1 TO 50) AS LONG, Black AS LONG, Black2 AS LONG
  2. DIM min AS INTEGER, max AS INTEGER, up AS INTEGER, down AS INTEGER
  3.  
  4. Setup
  5. 'CalculateRows
  6. '30 rows and 62 columns
  7. col = 1
  8. row = 10
  9. min = 1
  10. max = 44
  11. up = 1
  12. down = 23
  13.  
  14.     IF _KEYDOWN(19200) THEN
  15.         LOCATE 27, 1: COLOR whites(1): PRINT "Left    "
  16.         IF col > min THEN col = col - 1
  17.     END IF
  18.     IF _KEYDOWN(19712) THEN
  19.         LOCATE 27, 1: COLOR whites(1): PRINT "Right    "
  20.         IF col < max THEN col = col + 1
  21.     END IF
  22.     IF _KEYDOWN(18432) THEN
  23.         LOCATE 27, 1: COLOR whites(1): PRINT "Up       "
  24.         IF row > up THEN row = row - 1
  25.     END IF
  26.     IF _KEYDOWN(20480) THEN
  27.         LOCATE 27, 1: COLOR whites(1): PRINT "Down     "
  28.         IF row < down THEN row = row + 1
  29.     END IF
  30.     Arena
  31.     MoveLight row, col, 10, 1, 1
  32.     LOCATE 26, 1: COLOR _RGB(255, 255, 55), Black: PRINT " press arrows to move light, space to quit ";
  33.     _LIMIT 12
  34.     CLS
  35.  
  36. SUB Arena
  37.     LOCATE 1, 1
  38.     COLOR Black2, Black
  39.     FOR x = 1 TO 25 STEP 1
  40.         PRINT STRING$(62, "0");
  41.     NEXT x
  42.  
  43. SUB MoveLight (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  44.     outputs row, col, min, max, -steps
  45.     outputs row, min + col, max, min, steps
  46.     outputs row + 1, col, min, max, -steps
  47.     outputs row + 1, min + col, max, min, steps
  48.     outputs row + 2, col, min, max, -steps
  49.     outputs row + 2, min + col, max, min, steps
  50.  
  51. SUB outputs (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  52.     LOCATE row, col
  53.     FOR r = min TO max STEP steps
  54.         COLOR Black, whites(r): PRINT "0";
  55.     NEXT
  56.  
  57.  
  58. SUB Setup
  59.     A = _NEWIMAGE(500, 500, 32)
  60.     IF A THEN SCREEN A ELSE PRINT "Error create SCREEN"
  61.     Black = _RGB(0, 0, 0)
  62.     Black2 = _RGB(0, 0, 15)
  63.     whites(1) = _RGB(255, 255, 255)
  64.     r = 255
  65.     b = 255
  66.     g = 255
  67.     counter = 1
  68.     FOR counter = 1 TO 10
  69.         r = r - INT(255 / 10)
  70.         b = b - INT(255 / 10)
  71.         g = g - INT(255 / 10)
  72.         whites(counter) = _RGB(r, g, b)
  73.     NEXT counter
  74.  
  75. SUB CalculateRows
  76.     LOCATE 1, 1: COLOR _RGB(0, 127, 127)
  77.     FOR x = 1 TO 80 STEP 1
  78.         IF x > -1 THEN PRINT LTRIM$(STR$(x MOD 10));
  79.     NEXT x
  80.     PRINT
  81.     FOR x = 1 TO 30 STEP 1
  82.         IF x > -1 THEN PRINT LTRIM$(STR$(x MOD 10))
  83.     NEXT x
  84.  
  85.     SLEEP
  86.  
now you can see a rectangular light on the background
Programming isn't difficult, only it's  consuming time and coffee

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #2 on: April 24, 2019, 04:17:32 pm »
Neat effect. This should work well in a game.
QB64 is the best!

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #3 on: April 24, 2019, 05:45:10 pm »
Hi guys

thanks to all you to read this post...
I have tried to emulate an ASCII screen in 32bit  to create a light moving with arrows...

here is
Code: QB64: [Select]
  1. DIM SHARED A AS LONG, whites(1 TO 50) AS LONG, Black AS LONG
  2. DIM min AS INTEGER, max AS INTEGER, up AS INTEGER, down AS INTEGER
  3.  
  4. Setup
  5. CalculateRows
  6.  
  7. col = 1
  8. row = 10
  9. min = 1
  10. max = 44
  11. up = 1
  12. down = 23
  13.  
  14.     IF _KEYDOWN(19200) THEN
  15.         LOCATE 27, 1: COLOR whites(1): PRINT "Left"
  16.         IF col > min THEN col = col - 1
  17.     END IF
  18.     IF _KEYDOWN(19712) THEN
  19.         LOCATE 27, 1: COLOR whites(1): PRINT "Right"
  20.         IF col < max THEN col = col + 1
  21.     END IF
  22.     IF _KEYDOWN(18432) THEN
  23.         LOCATE 27, 1: COLOR whites(1): PRINT "Up"
  24.         IF row > up THEN row = row - 1
  25.     END IF
  26.     IF _KEYDOWN(20480) THEN
  27.         LOCATE 27, 1: COLOR whites(1): PRINT "Down"
  28.         IF row < down THEN row = row + 1
  29.     END IF
  30.  
  31.     MoveLight row, col, 10, 1, 1
  32.     LOCATE 26, 1: COLOR _RGB(255, 255, 55), Black: PRINT " press arrows to move light, space to quit ";
  33.     _LIMIT 15
  34.     CLS
  35.  
  36. SUB MoveLight (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  37.     outputs row, col, min, max, -steps
  38.     outputs row, min + col, max, min, steps
  39.     outputs row + 1, col, min, max, -steps
  40.     outputs row + 1, min + col, max, min, steps
  41.     outputs row + 2, col, min, max, -steps
  42.     outputs row + 2, min + col, max, min, steps
  43.  
  44. SUB outputs (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)
  45.     LOCATE row, col
  46.     FOR r = min TO max STEP steps
  47.         COLOR Black, whites(r): PRINT "0";
  48.     NEXT
  49.  
  50. SUB Setup
  51.     A = _NEWIMAGE(500, 500, 32)
  52.     IF A THEN SCREEN A ELSE PRINT "Error create SCREEN"
  53.     Black = _RGB(0, 0, 0)
  54.     whites(1) = _RGB(255, 255, 255)
  55.     r = 255
  56.     b = 255
  57.     g = 255
  58.     counter = 1
  59.     FOR counter = 1 TO 10
  60.         r = r - INT(255 / 10)
  61.         b = b - INT(255 / 10)
  62.         g = g - INT(255 / 10)
  63.         whites(counter) = _RGB(r, g, b)
  64.     NEXT counter
  65.  
  66. SUB CalculateRows
  67.     LOCATE 1, 1: COLOR _RGB(0, 127, 127)
  68.     FOR x = 1 TO 80 STEP 1
  69.         IF x > -1 THEN PRINT LTRIM$(STR$(x MOD 10));
  70.     NEXT x
  71.     SLEEP 3
  72.  
  73.  

Hi Tempodi,

I just started using QB64.  Regarding your SUB version, how about using this as the first line in the code:
DEFINT A-Z

I believe that would automatically define all variables as integers unless otherwise DIM'ed as longs.

Then the line:
SUB MoveLight (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)

could be simplified as:
SUB MoveLight (row, col, min, max, steps)

It would also negate the need for the lines:
DIM row AS INTEGER, col AS INTEGER
DIM min AS INTEGER, max AS INTEGER, up AS INTEGER, down AS INTEGER
and just use:
col = 1
row = 10
min = 1
max = 44
up = 1
down = 23

I'm new to QB64 so I'm not sure if doing the above has any cons when most variables are integers.
PS:  I'm learning from your code as a clean example of how to use SUBS.
« Last Edit: April 24, 2019, 05:53:00 pm by Raptor88 »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #4 on: April 24, 2019, 08:43:17 pm »
Quote
I just started using QB64.  Regarding your SUB version, how about using this as the first line in the code:
DEFINT A-Z

I believe that would automatically define all variables as integers unless otherwise DIM'ed as longs.

Then the line:
SUB MoveLight (row AS INTEGER, col AS INTEGER, min AS INTEGER, max AS INTEGER, steps AS INTEGER)

could be simplified as:
SUB MoveLight (row, col, min, max, steps)

Personally, I prefer to make SUBs with the variables explicitly defined, such as in the original.

The reason?

I prefer modular coding techniques and want to make my subs and functions as simple as possible to move from one program to another.

Let’s say I have a program like this:

DEFINT A-Z
..... stuff
SUB Whatever (x, y)
.....more stuff
END SUB

Now, x and y are integers and everything runs just fine...

BUT....

What if I want to reuse that sub in other code that starts with _DEFINE A-Z AS _FLOAT?  Will it still function and work as intended?  Will it generate rounding errors over time?  Can I now get edge-case exceptions where 3.2 + 4.4 = 7.6, which might round to 8??

By explicitly defining SUB Whatever (x AS INTEGER, y AS INTEGER), it can be ripped out and reused in another program, with fewer concerns and issues.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #5 on: April 24, 2019, 08:49:06 pm »
Personally, I prefer to make SUBs with the variables explicitly defined, such as in the original.

The reason?

I prefer modular coding techniques and want to make my subs and functions as simple as possible to move from one program to another.

Let’s say I have a program like this:

DEFINT A-Z
..... stuff
SUB Whatever (x, y)
.....more stuff
END SUB

Now, x and y are integers and everything runs just fine...

BUT....

What if I want to reuse that sub in other code that starts with _DEFINE A-Z AS _FLOAT?  Will it still function and work as intended?  Will it generate rounding errors over time?  Can I now get edge-case exceptions where 3.2 + 4.4 = 7.6, which might round to 8??

By explicitly defining SUB Whatever (x AS INTEGER, y AS INTEGER), it can be ripped out and reused in another program, with fewer concerns and issues.

Explicitly defining SUB variables for reuse of SUBs in another program is something I had not thought of.  Thanks for this info.
Raptor88

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #6 on: April 25, 2019, 04:50:14 pm »
Hi Raptor88

thanks to take a look at my code

about why I don't use a global definition of the type of the variables at the begin of the code....

1. the first reason is that has explained with supreme clearness Steve, the reusability of the code if you use the SUBs and Functions. I started this habit in writing code in the time of when I was coding in Qbasic,   in which I realized that more times I had had coded for the same functions (mouse implementation by ASM and Call Absolute, ASCII windowing in MSDOS etc). Moreover in QB you can make an your own library that you could compile using a command line option on loading QB, today in QB64 you can use your personal library splitting your code in declaration part (that you $include: at the start of the program) and in code part (that you $include: at the end of the program).

2. the second reason is that also if you use DEFINT or DEFSTR or someother definition,  you must DIM SHARED or COMMON variables
Quote
DIM SHARED A AS LONG, whites(1 TO 50) AS LONG, Black AS LONG
DIM SHARED r AS INTEGER, g AS INTEGER, b AS INTEGER, counter AS INTEGER

3. the third reason is that I am following some instructions to write clear code to be readable also after much time and avoid bugs. These instructions are born for coding clear C but they works fine also for Basic. The concepts behind are writing names (constants, variables, labels, subs and functions) that tell you what do that code, chunk the work in miniworks to assemble in sequence, initialize variables to default values before to use them and to avoid garbage in RAM, release the resources that are not more used (opened files, images loaded, sound loaded, arrays and variables dynamic).
 
Thanks to read
Programming isn't difficult, only it's  consuming time and coffee

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #7 on: April 25, 2019, 05:13:56 pm »
Hi Tempodi,

Thanks for taking the time to explain.  All makes sense. 

Am learning as I go,
Raptor88

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #8 on: April 25, 2019, 05:21:31 pm »
Quote
2. the second reason is that also if you use DEFINT or DEFSTR or someother definition,  you must DIM SHARED or COMMON variables
Quote
DIM SHARED A AS LONG, whites(1 TO 50) AS LONG, Black AS LONG
DIM SHARED r AS INTEGER, g AS INTEGER, b AS INTEGER, counter AS INTEGER

I confess, I don't understand "you must" ? You don't even have to DIM other types if you use suffixes, so why "must" they also be SHARED or COMMON?

It is probably good form to use OPTION _EXPLICIT which forces DIM for everything. C coders would approve, I think. :)
« Last Edit: April 25, 2019, 05:25:08 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: an ASCII emulated light moving on the screen SUB vs GOSUB version
« Reply #9 on: April 25, 2019, 05:43:56 pm »
Hi Bplus
sorry for my bad communication
about
Quote
2. the second reason is that also if you use DEFINT or DEFSTR or someother definition,  you must DIM SHARED or COMMON variables
I'm trying to say that also if I use DEFINT (or another definition type of data) when I need Global variables I must declare them also if the type has been declare before with the command of type definition...
for example

DEFINT A-C   '<--- this force all variables typed and that their name starts with A or B or C to be integer
DIM SHARED Account [AS INTEGER]  '<--- to create this integer variable as global (DIM SHARED) I must declare there variable  [ You are not allowed to view this attachment ]  

Programming isn't difficult, only it's  consuming time and coffee