Author Topic: Trying to imitate TIS-100  (Read 5989 times)

0 Members and 1 Guest are viewing this topic.

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Trying to imitate TIS-100
« on: September 02, 2020, 12:48:12 am »
As i mentioned before i got into QB64 trhu TIS-100, among other things. I'm opening this topic because ill notice that imitate such a game is a huge task and i'm gonna need a lot of learning and help. For the moment i started with an intro, but i'm having problems with CLS (and probably with _DISPLAY too). I don't clearly understand the relation between these statements. In the example i'm posting after the infect() SUB the CLS statement has no effect... why is this happening? (theres also a nice bag of snippets in the end)

Code: QB64: [Select]
  1. '
  2. ----------------------Û   SETUP   Û----------------
  3.  
  4. CONST ScreenWidth = 800 ' check if this values are ok     ÍÍÍ»
  5. CONST ScreenHeight = 608 '                                   º
  6. '                                                            º
  7. SCREEN _NEWIMAGE(ScreenWidth, ScreenHeight, 32) '            º        ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
  8. _FULLSCREEN '                                                º        º                 º
  9. '                                                            º        º1,1    TEXT       º
  10. CONST WHITE = _RGB32(255, 255, 255) '                        º VIDEO  º      SCREEN      º
  11. CONST MIDGRAY = _RGB32(127, 127, 127) '                      º        º          38y,100xº
  12. CONST DARKGRAY = _RGB32(63, 63, 63) '                        º        º                 º
  13. CONST LIGHTGRAY = _RGB32(191, 191, 191) '                    º        ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
  14. CONST RED = _RGB32(127, 0, 0) '                              º
  15. CONST GREEN = _RGB32(0, 127, 0) '                         ÍÍͼ
  16. '----------------------------------Û   PROGRAM   Û---------------------------------
  17. infect
  18. systemCheck
  19.  
  20.  
  21. '--------------------------------Û SUBROUTINES Û---------------------------
  22. SUB infect () ' intro sketch
  23.  
  24.     row% = 15
  25.     column% = 38
  26.     message1$ = "I'm taking control..."
  27.     message2$ = "And you can't stop me"
  28.  
  29.     'sayHello
  30.     COLOR GREEN
  31.     PRINTslowly row% + 3, column% + 2, message1$
  32.     PRINTslowly row% + 4, column% + 2, message2$
  33.     SLEEP 1
  34.  
  35.     'install
  36.     i% = 0
  37.     ii% = 0
  38.     loadingBar$ = CHR$(219)
  39.     DO
  40.         i% = i% + 1
  41.         ii% = INT(i% / 100)
  42.         _LIMIT 500
  43.  
  44.         COLOR MIDGRAY '                             ¿
  45.         LOCATE RND(1) * 37 + 1, RND(1) * 99 + 1 '   ³ random characthers on the back
  46.         PRINT CHR$(RND(1) * 222 + 32); '            Ù
  47.  
  48.         COLOR GREEN '    a virus is GREEN! 
  49.  
  50.         drawDoubleLineBox row%, column%, 23, 7, ""
  51.         fakeCLS row% + 1, column% + 1, 22, 6
  52.  
  53.         IF ii% MOD 2 = 0 THEN '                   ¿
  54.             LOCATE row% + 1, column% + 2 '        ³ blink: title
  55.             PRINT "CV64 VIRUS V0.2  >:-["; '      ³ underline
  56.             LOCATE row% + 2, column% + 2 '        ³ message1$
  57.             PRINT STRING$(21, CHR$(223)); '       ³ and
  58.             '                                     ³ message2$
  59.             LOCATE row% + 3, column% + 2 '        ³
  60.             PRINT message1$; '                    ³
  61.             '                                     ³
  62.             LOCATE row% + 4, column% + 2 '        ³
  63.             PRINT message2$; '                    Ù
  64.  
  65.         END IF
  66.  
  67.         LOCATE row% + 6, column% + 2 '                              ¿
  68.         IF ii% < 20 THEN PRINT "Overriding OS..."; '                ³
  69.         IF ii% < 40 AND ii% > 19 THEN PRINT "Accesing files..."; '  ³ loading messages
  70.         IF ii% < 60 AND ii% > 39 THEN PRINT "Infecting..."; '       ³
  71.         IF ii% < 80 AND ii% > 59 THEN PRINT "Replicating..."; '     ³
  72.         IF ii% < 100 AND ii% > 79 THEN PRINT "Ereasing nodes..."; ' ³
  73.         IF ii% = 100 THEN PRINT "Nodes killed "; '                 Ù
  74.  
  75.         ii$ = LTRIM$(STR$(ii%)) '                   ¿
  76.         LOCATE row% + 6, column% + 22 - LEN(ii$) '  ³ xx%
  77.         PRINT ii$ + "%"; '                          Ù
  78.  
  79.         IF i% MOD 500 = 0 THEN loadingBar$ = loadingBar$ + CHR$(219) ' ¿
  80.         LOCATE row% + 7, column% + 2 '                                 ³ loading bar
  81.         PRINT loadingBar$; '                                           Ù
  82.  
  83.         _DISPLAY
  84.     LOOP UNTIL i% = 10000
  85.     SLEEP 2 '                                       infection complete XD
  86.  
  87.  
  88. '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  89. SUB systemCheck () ' fake system check as excuse to go at main menu
  90.  
  91.     COLOR LIGHTGRAY
  92.     PRINTslowly 2, 2, "Unexpected restart. Check system..."
  93.  
  94.     FOR i& = 0 TO 247483647 STEP 2048
  95.         _LIMIT 20000
  96.         LOCATE 4, 2
  97.         PRINT "CHECKSUM: ";: PRINT LTRIM$(STR$(i&));
  98.     NEXT i&
  99.     PRINT " OK"
  100.  
  101.     PRINTslowly 6, 2, "Some nodes have been corrupted..."
  102.     PRINTslowly 7, 2, "Reprogram them to recover access to OS..."
  103.     PRINTslowly 9, 2, "Starting debbuger"
  104.  
  105.     SLEEP 1
  106.  
  107.  
  108. 'ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÛ   SNIPPETS    ÛÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
  109. 'save time over recurring things
  110. '----------------------------Û  SUBROUTINES  Û-----------------------
  111. SUB drawDoubleLineBox (originY%, originX%, width%, height%, message$)
  112.  
  113.     IF originY% < 1 THEN EXIT SUB '                     ÄÄÄ¿
  114.     IF originX% < 1 THEN EXIT SUB '                        ³ check if logic
  115.     IF width% < 1 THEN EXIT SUB '                          ³
  116.     IF height% < 1 THEN EXIT SUB '                      ÄÄÄÙ
  117.  
  118.     LOCATE originY%, originX% '                         ÄÄÄ¿
  119.     PRINT CHR$(201); ' É                                   ³
  120.     LOCATE originY% + height% + 1, originX% '              ³
  121.     PRINT CHR$(200); ' È                                   ³ corners
  122.     LOCATE originY%, originX% + width% + 1 '               ³
  123.     PRINT CHR$(187); ' »                                   ³
  124.     LOCATE originY% + height% + 1, originX% + width% + 1 ' ³
  125.     PRINT CHR$(188); ' ¼                                ÄÄÄÙ
  126.  
  127.     FOR y% = 1 TO height% '                             ÄÄÄ¿
  128.         LOCATE originY% + y%, originX% '                   ³
  129.         PRINT CHR$(186); ' º                               ³ sides
  130.         LOCATE originY% + y%, originX% + width% + 1 '      ³
  131.         PRINT CHR$(186); ' º                               ³
  132.     NEXT y% '                                           ÄÄÄÙ
  133.  
  134.     FOR x% = 1 TO width% '                              ÄÄÄ¿
  135.         LOCATE originY%, originX% + x% '                   ³
  136.         PRINT CHR$(205); ' Í                               ³ top &
  137.         LOCATE originY% + height% + 1, originX% + x% '     ³ bottom
  138.         PRINT CHR$(205); ' Í                               ³
  139.     NEXT x% '                                           ÄÄÄÙ
  140.  
  141.     LOCATE originY% + 1, originX% + 1 '                 ÄÄÄ¿ message
  142.     PRINT message$ '                                    ÄÄÄÙ
  143.  
  144.  
  145. '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  146. SUB PRINTslowly (originY%, originX%, message$)
  147.  
  148.     IF originY% < 1 THEN EXIT SUB '     ¿ check if logic
  149.     IF originX% < 1 THEN EXIT SUB '     Ù
  150.  
  151.     PCthinking originY%, originX%
  152.  
  153.     i% = 0 '                                                Ä¿
  154.     DO '                                                     ³
  155.         _DELAY .05 '                                         ³
  156.         i% = i% + 1 '                                        ³ print message$
  157.         buildingMessage$ = LEFT$(message$, i%) + CHR$(219) ' ³ character by character
  158.         LOCATE originY%, originX% '                          ³ and erease the cursor
  159.         PRINT buildingMessage$; '                            ³
  160.     LOOP UNTIL LEN(buildingMessage$) = LEN(message$) + 1 '  ÄÙ
  161.  
  162.     LOCATE CSRLIN, originX% + LEN(message$): PRINT " "; ' erease cursor
  163.  
  164.  
  165. '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  166. SUB PCthinking (originY%, originX%)
  167.  
  168.     IF originY% < 1 THEN EXIT SUB '     ¿ check if logic
  169.     IF originX% < 1 THEN EXIT SUB '     Ù
  170.  
  171.     FOR i% = 1 TO 11 '                  ¿
  172.         _LIMIT 5 '                      ³
  173.         LOCATE originY%, originX% '     ³
  174.         IF i% MOD 2 = 0 THEN '          ³ bliking cursor
  175.             PRINT CHR$(219); '          ³
  176.         ELSE '                          ³
  177.             PRINT " "; '                ³
  178.         END IF '                        ³
  179.     NEXT i% '                           Ù
  180.  
  181.  
  182. '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  183. SUB fakeCLS (originY%, originX%, width%, height%)
  184.  
  185.     IF originY% < 1 THEN EXIT SUB '               ¿
  186.     IF originX% < 1 THEN EXIT SUB '               ³ check if logic
  187.     IF width% < 1 THEN EXIT SUB '                 ³
  188.     IF height% < 1 THEN EXIT SUB '                Ù
  189.  
  190.     FOR x% = 0 TO width% '                        ¿
  191.         FOR y% = 0 TO height% '                   ³ print
  192.             LOCATE originY% + y%, originX% + x% ' ³ " " (space)
  193.             PRINT " "; '                          ³ repeatedly
  194.         NEXT y% '                                 ³
  195.     NEXT x% '                                     Ù
  196.  
  197.  
  198.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trying to imitate TIS-100
« Reply #1 on: September 02, 2020, 02:24:20 am »
CLS is just clear screen, if want to change background color set backcolor:
Code: QB64: [Select]
  1. COLOR ,&HFFFFFF00 'yellow
  2.  
or just CLS to clear last loop

_DISPLAY ' all screen updates in usually a loop wait until it comes to _DISPLAY this prevents flashing or blinking if you CLS allot.

This means if you print something and need to see it immediately before usually the end of a loop you have to say
_DISPLAY right after the print.

So once you _DISPLAY, you have to _DISPALY every time you want a screen update but you can turn _DISPLAY off with _AUTODISPLAY which is default screen mode.
« Last Edit: September 02, 2020, 02:27:39 am by bplus »

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Trying to imitate TIS-100
« Reply #2 on: September 02, 2020, 03:30:24 am »
brother!! ;-))

You are my new programming lenguage, i will call you "B+" (why QB64 wasn't named like this? It has a lot more of punch! At least phonetycally XD)

Thank you very much for your explanation, i get perfectly the idea. So _DISPLAY is like an OFF switch, _AUTODISPLAY an ON switch, and CLS just erease the screen, and i got to make combinatios of this 3 statements. If i _DISPLAY once i wont see changes until next _DISPLAY, or _AUTODISPLAY (flicker will be back if i _AUTODISPLAY). This is great info!!!

Did I understood correctly?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trying to imitate TIS-100
« Reply #3 on: September 02, 2020, 03:49:55 am »
Pretty close, I would describe _Display as a wait until I say _Display switch :-))

_AUTODISPLAY, automatic draw or print, no waiting switch.

In main animation loop, I usually:

Code: QB64: [Select]
  1.   cls
  2.   draw and print stuff...
  3.   _limit 60 ' loop around 60 times a second or another wait to keep sequence steady
  4.                 'assuming you can draw and print faster this save CPU fan from coming on also
« Last Edit: September 02, 2020, 03:51:57 am by bplus »

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Trying to imitate TIS-100
« Reply #4 on: September 02, 2020, 04:03:54 am »
Right, right... So, let me see if i get now: once _DISPLAY is found it actualizes the screen, from now on until _DISPLAY isn't found again nothing that is printed will be show in the screen until the next _DISPLAY. In the loop this happens continously. And once you leave the loop if you wanna see further printing you have to _AUTODISPLAY and CLS, keep printing and CLSing the usual newbie way (my way XD, or now that i think it you called it "Default screen mode")..., until the next loop, wich surely will contain another _DISPLAY, and then again when out of the loop for non-looping printing i come back to the _AUTODISPLAY mode.

Better? :P Or i'm messing things up? lol

Thank you for your teachings and time

Another question by the way: is there any diference between:

SUB subroutineName
    <code>
END SUB

and

SUB subroutineName ()
    <code>
END SUB

Does the parenthesis means something?

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Trying to imitate TIS-100
« Reply #5 on: September 02, 2020, 07:07:01 am »
You have a clear and interesting way of commenting code.  I've never seen it before.  Doesn't
it slow you down to find the right symbols? 

Most of your variables are integers.  You could save yourself a lot of typing % by starting off
your code with DEFINT A-Z.

I don't believe there's any difference with a SUB to include ().  The IDE will flag any discrepancy
if you DO have parameters and don't call the sub with parameters, or the other way around, as
you've probably noticed already.


It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trying to imitate TIS-100
« Reply #6 on: September 02, 2020, 11:02:13 am »
Quote
SUB subroutineName ()
    <code>
END SUB

Does the parenthesis means something?

The (parenthesis) are meant to contain parameters for instance:
Code: QB64: [Select]
  1. SUB mySub(parameter1%, parameter2 as _UNSIGNED LONG, parameter3 AS STRING)

If the SUB does not need any, they are not used
Code: QB64: [Select]
  1. SUB Hello
  2.     PRINT "Hello World!"

One very important thing to keep in mind is if the arguments passed through the SUB with a variable of the same type as the SUB's Parameter AND that variable gets changed in the SUB, that change will be passed back into the main program with variable's value changed, some PL's (Programming Languages) dont do that.

Demo
Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3. PRINT " Here in main x, y are integers I will modify in SUB Hello."
  4. x = 1: y = 1
  5. PRINT "x, y before call to Hello "; x; ","; y ' 1, 1
  6. Hello x, y ' 2, 2
  7. PRINT "After Hello SUB call x, y are:"; x; ","; y '1, 2    x did not get changed because different Type
  8.  
  9. SUB Hello (x AS LONG, y AS INTEGER)
  10.     PRINT "Hello World!, I am going to increment x and y you sent me but,"
  11.     PRINT "check them out back in the main program."
  12.     x = x + 1: y = y + 1
  13.     PRINT "In Hello SUB x, y are now:"; x; ","; y
  14.  
  15.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trying to imitate TIS-100
« Reply #7 on: September 02, 2020, 03:18:06 pm »
You have a clear and interesting way of commenting code.  I've never seen it before.  Doesn't
it slow you down to find the right symbols? 

Most of your variables are integers.  You could save yourself a lot of typing % by starting off
your code with DEFINT A-Z.

I don't believe there's any difference with a SUB to include ().  The IDE will flag any discrepancy
if you DO have parameters and don't call the sub with parameters, or the other way around, as
you've probably noticed already.

@Juan Tamarit

Yes +1, those comments may be even better than Terry's! Truly unique and it does look like it takes quite an effort unless you have shortcuts, a special editor?

Suffixes are nice for tracking Type as you code but % for integers drives me crazy (also ~&for _UNSIGNED LONG).

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Trying to imitate TIS-100
« Reply #8 on: September 02, 2020, 05:00:39 pm »
Thank you for your replies, guys. The way i comment dosnt really shine here on the forum. The "
Code: QB64: [Select]
  1.  
" dosnt show how i actually see he comments. I have an Ascii table at hand, i also use the INSERT key to change cursor function (if it fits me better) and then just Alt+ ASCII code. After doin some commenting you start to remember some combinations and turn easier.

things like:

FOR i% = 0 TO 20 ' ┐ comment entire
    array(i%) = 1 '   │ block here
NEXT i% '               ┘

i THINK them literally as "ALT191", "ALT179", and so...

But i do this lately, for a begginer commenting line by line sound lke a better idea. I have also seen a conference/class by Uncle Bob who was talking about "Clean code", and he did some mentions on comments. I recall things he said, like:

"If you see "for i = 0, i = 23, i++  ' increase counter" it's rather a stupid comment, and unnecessary. Comment things that really need to be commented. Else, read the code... the code is self explainatorie (and if not it's a disaster)."

Also i'm a hobbist, i don't have any rush to finish anything. I can sit for half hour just watching the code and thinking how can i say and ask for things in the most short and easier way, as long as they are self explainatorie. If it's a cryptic mess i try to rearrenge it to looks cleaner at view... and for the processor too.

About variable types with suffixes, i feel more comftable with them, i feel that i can keep track not just the variable name BUT ALSO IT'S TYPE, so i may have a variable name "number$" and another called "number%" (even this is dangerous)

B+: what you told me about SUB's make me remember about the SHARED stuff, so my question would be ¿If you have a variable on main program and another with same name, but inside a SUB, does the variable in main program seems affected by this other one? (as if the variable in the SUB were the main program one)?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trying to imitate TIS-100
« Reply #9 on: September 02, 2020, 06:01:17 pm »
Quote
B+: what you told me about SUB's make me remember about the SHARED stuff, so my question would be ¿If you have a variable on main program and another with same name, but inside a SUB, does the variable in main program seems affected by this other one? (as if the variable in the SUB were the main program one)?

Yes a variable x say, in a SUB is private to an x in the main and other subs unless:
The variable is DIM SHARED in the main or the SUB shares with the main.
Or it is passed to the SUB (or FUNCTION) in the arguments list.
CONST declared in main is shared throughout file.

I feel like I am forgetting something? about a sub sharing with Main. I don't usually use that option, I usually go completely private or completely shared for a variable. But I imagine in extremely large programs one may want to have limited sharing.

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Trying to imitate TIS-100
« Reply #10 on: September 02, 2020, 06:23:16 pm »
Or it is passed to the SUB (or FUNCTION) in the arguments list.

Now this is interesting. So if i pass a variable to a SUB (i won't consider the FUNCTION case for now) and the names are the same in main code AND in the argument list of the SUB (next to the SUB name) the variable on main code will be affected by whatever happens inside the SUB?

PD: Terry can't be superated!!! XD I was thinking in starting a topic for a program that teaches QB64, said like an asistant/teacher, following more or less Terrie's didactic sequence. I'm also a teacher, you know? I teach Chemistry at high-school =)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trying to imitate TIS-100
« Reply #11 on: September 02, 2020, 06:46:57 pm »
Quote
Now this is interesting. So if i pass a variable to a SUB (i won't consider the FUNCTION case for now) and the names are the same in main code AND in the argument list of the SUB (next to the SUB name) the variable on main code will be affected by whatever happens inside the SUB?

Yes if the name and TYPE is the same, see the Hello demo above, same with FUNCTION.

Keep this in mind if you plan on changing the value in the SUB or FUNCTION, this BTW can be an advantage making a SUB behave a bit like a FUNCTION by returning output stuff in the variables that match Type as defined in parameters list.

I wonder what happened to Terry, he asked about using a Pong paddle modification some months ago, haven't heard from him since?
« Last Edit: September 02, 2020, 06:49:00 pm by bplus »

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Trying to imitate TIS-100
« Reply #12 on: September 02, 2020, 07:28:04 pm »
I didn't knew about this DEFINT statement, i will try it in further proyetcs.

ok, the Hello Demo was clear, tried changing it and works the same, ill have this into consideration. Thanks for the warning or bad unexplainable things couuld have happened.

By the way, you may found this interesting. I did a small snippet for the INKEY$. It's a function that get no parameters, but return the INKEY$      =)

Code: QB64: [Select]
  1. FUNCTION getINKEY$ ()
  2.  
  3.     DO
  4.         _LIMIT 20
  5.         keyPress$ = INKEY$
  6.     LOOP UNTIL keyPress$ <> ""
  7.  
  8.     getINKEY$ = keyPress$
  9.  
  10.  

But shurely you already knew this.

Another question! How can i get a 1 or 0 50/50 chance? I seem to be missin something about RND(). I never understand the parameter inside it. I only have seen Terry using it like:

RND(1) * 256

to get a random number between 1 (or 0?) to 256 (or 255?)

Why is the the multiplication necesary?

About the parenthesis on subs: ¿can i say that if they aren't present the SUB recibes no parameters, but if they are there the parameter is null? I'm getting to much phylosophycal here? XD
   

Offline Juan Tamarit

  • Newbie
  • Posts: 53
    • View Profile
Re: Trying to imitate TIS-100
« Reply #13 on: September 02, 2020, 07:33:08 pm »
Or should i say null parameters?
   

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trying to imitate TIS-100
« Reply #14 on: September 02, 2020, 08:45:31 pm »
RND returns a real number from 0 to almost 1 so RND * 256 returns anything from 0 to 255.99???

and Int(RND * 256) returns integers in range 0 to 255. INT() always rounds down.

RND(n) not normally used, I don't use it and I am normal ;-))
« Last Edit: September 02, 2020, 08:46:38 pm by bplus »