Text gaming for the web

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Text gaming for the web

Post by mikeg »

This example works. Using this method I should be able to also draw color borders and even make ASCII character images on your in web browser tool.
Bellow I am able to clear anything that I previously plotted without using CLS or CLG
(although I probably would use CLS for whole screen)
Any suggestions for text mode graphics plotting would be appreciated. ( this could be for games and also for interactive tools, like custom calculators)

Code: Select all

      bx=0:by=0:sl%=0:LET w=10:LET h=10
      MESSAGE$="":REM for handling a possible input string.
      PRINT "TEXT only gaming text character movement test"
      PRINT "PRESS W to move up, A to move left, D to move right and S to move down"
      OFF
      REPEAT
  
        REPEAT
          key$ =INKEY$(1)
          MOVE bx,by:PRINT TAB(h,w);MESSAGE$;"O"
          sl%=LEN(MESSAGE$)
        UNTIL key$ <>""
        sl%=LEN(MESSAGE$)
        IF INKEY(-48) sl%=LEN(MESSAGE$)-1:key$=""
        REPEAT UNTIL INKEY(0)=-1
        WAIT 10
        IF key$="w" THEN PRINT TAB(h,w);" ":w=w-1
        IF key$="a" THEN PRINT TAB(h,w);" ":h=h-1
        IF key$="d" THEN PRINT TAB(h,w);" ":h=h+1
        IF key$="s" THEN PRINT TAB(h,w);" ":w=w+1
      UNTIL FALSE
Focus is on code subject. Feel free to judge the quality of my work.
RichardRussell

Re: Text gaming for the web

Post by RichardRussell »

mikeg wrote: Mon 21 Sep 2020, 04:18 Using this method I should be able to also draw color borders and even make ASCII character images on your in web browser tool.
Can I just check that you're not getting confused between the console-mode BBC BASIC (which doesn't support graphics or sound) and the in-browser BBC BASIC (which fully supports 2D graphics and sound)? Why would you want to limit yourself to "text gaming" on the web, or is this just for 'artistic effect'?

I linked previously to in-browser versions of David Williams's Buggy game and his Tyoob game (these are 'live links' which will run the programs if you're using a suitable browser).
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Re: Text gaming for the web

Post by mikeg »

Why would you want to limit yourself to "text gaming" on the web, or is this just for 'artistic effect'?
If this program works with no problems, then I guess I don't need to change my ways.. A text based version of this would probably cut the size of code to minimal.

Tell me if there is a problem with this program working on the web based BBCSDL.. I use this program and others like it every day for trading.
( I am sharing the link so that non programmers that trade with me can use it.. They have seen it in action )

Code: Select all

      REM Stock trader share calculator 3 ranges (July 17,2020)
      REM ************* BEGINNING OF USER AREA (modify for your stock) ******************
      LET money=0:REM  you can put a default value here but you can just edit within the active app
      INPUT "PLAY AMOUNT ? ";money
      LET low=2.30: REM replace this number with the lowest your stock is likely to go
      LET high=2.45: REM replace this number with the highest your stock is likely to go
      LET bought=2.32: REM this will be your current stock purchase (static)
      name$="POU": REM replace this with your stock name
      LET bf=10: REM  <<<<< Your Banking fee per transaction   ***
      REM ************************************ END OF USER INFO AREA ********************
      money2=0
      nde%=0
      bres=0:REM bought shares value
      lres=0:REM low share prediction
      sbou=0:REM variable for high bought cash display
      dif=0:diff$=""
      mx=0:my=0:mb=0
      money=money-bf
      PROCgraphics(280,505)
      REM You must accept ALL financial risks from using this program before using it
      REM The Author of this program offers no promise of profit or functionality
      *REFRESH OFF

      REM live adjustable low (shares needed to buy at this level)
      PROCsbox(10,10,500,300,"6")
      PROCsbox(10,305,500,600,"6")
      PROCsbox(10,605,500,900,"6")
      PROCsbox(10,905,500,1000,"6")
      PROCsbox(505,10,560,900,"0")
      PROCsbox(505,10,560,600,"0")
      PROCsbox(505,10,560,300,"0")
      GCOL 0
      MOVE 10,940:PRINT " Stock Trading Tool "
      GCOL 15:MOVE 20,970:PRINT name$
      REPEAT
        GCOL 6:MOVE 20,450:PRINT "PLAY MONEY $ "+STR$(money)
        v=0
        LET v= FNau(530,210)
        lres= FNlsharecalc(20,200,low,money)
        IF v=1 THEN
          MOVE20,230:GCOL 6:PRINT STR$(low)
          low=low+.01
          WAIT 7
        ENDIF
        v=0
        LET v= FNad(530,90)
        IF v=1 THEN
          MOVE20,230:GCOL 6:PRINT STR$(low)
          low=low-.01
          WAIT 7
        ENDIF
        GCOL 0
        MOVE 20,230:PRINT STR$(low)
        REM live adjustments bought location
        v=0
        LET v= FNau(530,495)
        bres= FNsharecalc(20,520,bought,money)
        IF v=1 THEN
          MOVE20,550:GCOL 6:PRINT STR$(bought):nde%=money/bought/2:MOVE 20,350:PRINT "(1/2 shares) "+STR$(nde%)
          bought=bought+.01
          WAIT 7
        ENDIF
        v=0
        LET v= FNad(530,390)
        IF v=1 THEN
          MOVE20,550:GCOL 6:PRINT STR$(bought):nde%=money/bought/2:MOVE 20,350:PRINT "(1/2 shares) "+STR$(nde%)
          bought=bought-.01
          WAIT 7
        ENDIF
        GCOL 0
        MOVE 20,550:PRINT STR$(bought)
        GCOL 6:MOVE 20,450:PRINT "PLAY MONEY $ "+STR$(money)
        MOVE 20,350:GCOL 15:nde%=money/bought/2:PRINT "(1/2 shares) "+STR$(nde%): GCOL 0:MOVE 20,450:PRINT "PLAY MONEY $ "+STR$(money):*REFRESH
        WAIT 7
        REM live adjustable high location
        MOVE 20,820:GCOL 6:PRINT "$ "+STR$(sbou)
        MOVE 20,350:GCOL 6:PRINT "(1/2 shares) "+STR$(money/bought/2)
        GCOL 6:MOVE 20,450:PRINT "PLAY MONEY $ "+STR$(money)
        nnm=0
        REM:nnm=FNnewcash: IF nnm>0 THEN money= nnm:bres=FNsharecalc(20,520,bought,money)
        sbou=money/bought
        sbou=sbou*high
        v=0
        MOVE 20,880:GCOL 1:PRINT"SELL POINT"
        LET v= FNau(530,800)
        IF v=1 THEN
          MOVE20,850:GCOL 6:PRINT STR$(high)
          high=high+.01
          WAIT 7
        ENDIF
        v=0
        LET v= FNad(530,690)
        IF v=1 THEN
          MOVE20,850:GCOL 6:PRINT STR$(high)
          high=high-.01
          WAIT 7
        ENDIF
        MOVE 20,820:GCOL 0:PRINT "$ "+STR$(sbou)
        GCOL 0
        MOVE 20,850:PRINT STR$(high)
        GCOL 1
        MOVE 20,790
        PRINT"*** CHANGE ***"
        GCOL 6
        MOVE 20,760:PRINT "$ "+diff$
        IF sbou>money THEN dif=sbou-money:diff$=STR$(dif)
        IF sbou<money THEN dif=money-sbou:diff$="-"+STR$(dif)
        GCOL 0
        MOVE 20,760:PRINT "$ "+diff$
        WAIT 5
  
      UNTIL FALSE
      *REFRESH ON
      END
      DEF FNnewcash
      LOCAL x,y,b,p,money2
      MOUSE x,y,b
      MOVE 20,400:GCOL 12: PRINT"CHANGE PLAY MONEY"
      IF x>20 AND y>370 AND x<300 AND  y<410 THEN
        MOVE 20,400:GCOL 15:PRINT"CHANGE PLAY MONEY"
        IF b=4 THEN money2=VAL(FNinput(20,400,25))-10
      ENDIF
      =money2
      REM remember this allows input message to be completed before it cycles.
      REM this command takes control until message is entered.
      REM I am working on a more flexible version that can be left and revisited like in windows
      REM so sorta like live multi processes
      REM H,V,TEXTLIMIT (simpler?)
      DEF FNinput(bx,by,textlimit)
      LOCAL fill,MESSAGE$
      initialx%=0:sl%=0:key$="":MESSAGE$="":MES$=""
      initialx%=textlimit*16.2
      FOR fill=1 TO 58
        GCOL 15:LINE bx-4,by+20-fill,bx+initialx%,by+20-fill
      NEXT fill
      GCOL 0:LINE bx+3,by+20,bx+initialx%,by+20:LINE bx+3,by+20-fill,bx+initialx%,by+20-fill
      REPEAT
        REPEAT
          key$ =INKEY$(1)
          GCOL 0
          MOVE bx,by:PRINT MESSAGE$;"_" :* REFRESH
          sl%=LEN(MESSAGE$)
        UNTIL key$ <>""
        sl%=LEN(MESSAGE$)
        IF INKEY(-48) sl%=LEN(MESSAGE$)-1:key$=""
        REPEAT UNTIL INKEY(0)=-1
        IF sl%<LEN(MESSAGE$) THEN
          GCOL 15
          MOVE bx,by
          PRINT MESSAGE$;"_"
        ENDIF
        MES$=MID$(MESSAGE$,0,sl%)
        MESSAGE$=MES$
        GCOL 15:MOVE bx,by:PRINT MESSAGE$;"_"
        IF LEN(key$) = 1 THEN
          IF LEN(MESSAGE$)<textlimit THEN GCOL 15:MOVE bx,by:PRINT MESSAGE$;"_": MESSAGE$=MESSAGE$+key$:* REFRESH OFF
          REM (jump)
        ENDIF
      UNTIL INKEY(-74)
      =MESSAGE$
      REM low buy shares predict
      DEFFNlsharecalc(x,y,p,cash)
      PRIVATE oldres%
      LOCAL result%
      MOVE x,y
      result%=cash/p
      GCOL 6
      PRINT STR$(oldres%)+" shares"
      GCOL 0
      MOVE x,y
      PRINT STR$(result%)+" shares"
      oldres%=result%
      =result%

      REM bought
      DEFFNsharecalc(x,y,p,cash)
      PRIVATE oldres%
      LOCAL result%
      MOVE x,y
      result%=cash/p
      GCOL 6
      PRINT STR$(oldres%)+" shares"
      GCOL 0
      MOVE x,y
      PRINT STR$(result%)+" shares"
      oldres%=result%
      =result%
      REM arrowup function
      DEF FNau(x,y)
      LOCAL c
      MOUSE mx,my,mb
      IF mx>x-20 AND mx<x+10 AND my>y AND my<y+50 THEN c=15 ELSE c=7
      PROCarrowup(x,y,c)
      IF c=15 AND mb=4 THEN =1
      =0
      DEF FNad(x,y)
      LOCAL c
      MOUSE mx,my,mb
      IF mx>x-20 AND mx<x+10 AND my>y-30 AND my<y+15 THEN c=15 ELSE c=7
      PROCarrowdown(x,y,c)
      IF c=15 AND mb=4 THEN =1
      =0
      REM lets stick with 16 colors **********************************************************************
      DEF PROCsbox(x%,y%,w%,h%,c$)
      LOCAL ry%,sx%,sy%
      sx%=x%:sy%=y%
      IF x%>w% THEN x%=w%:w%=sx%
      IF y%>h% THEN y%=h%:h%=sy%
      ry%=y%
      GCOL VAL(c$)
      REPEAT
        LINE x%,y%,w%,y%
        y%=y%+1
      UNTIL y%=h%
      y%=ry%
      IF c$<>"0" THEN GCOL 0 ELSE GCOL 15
      LINE x%+2,y%+2,w%-2,y%+2
      LINE w%-2,y%+2,w%-2,h%-4
      LINE w%-2,h%-4,x%+2,h%-4
      LINE x%+2,h%-4,x%+2,y%+2
      ENDPROC
      DEF PROCgraphics(x,y)
      VDU 23,22,x;y;8,15,16,1
      VDU 5
      ENDPROC
      DEFPROCarrowup(x,y,c)
      LOCAL ax,aw
      GCOL c
      FOR ax=1 TO 20
        PROCLR(x,y+ax,10)
      NEXT ax
      ax=0:aw=20
      FOR ax=1 TO 20
        PROCLR(x,y+20+ax,aw)
        aw=aw-1
      NEXT ax
      ENDPROC
      DEFPROCarrowdown(x,y,c)
      LOCAL ax,aw
      GCOL c
      FOR ax=1 TO 20
        PROCLR(x,y-ax,10)
      NEXT ax
      ax=0:aw=20
      FOR ax=1 TO 20
        PROCLR(x,y-20-ax,aw)
        aw=aw-1
      NEXT ax
      ENDPROC
      REM Left and Right from center x,y, and width w
      DEFPROCLR(x,y,w)
      LINE x-w,y,x+w,y
      ENDPROC
Focus is on code subject. Feel free to judge the quality of my work.
RichardRussell

Re: Text gaming for the web

Post by RichardRussell »

mikeg wrote: Tue 22 Sep 2020, 00:12Tell me if there is a problem with this program working on the web based BBCSDL.
You could have easily tried it for yourself: click here if you have a suitable browser (up-to-date desktop version of Chrome, Edge, Firefox or Opera).

What I would say is that it uses a lot of power, either running locally or in the browser, making my laptop quite hot (and draining the battery). This is because you are constantly redrawing graphics even if nothing has changed! You should try to get into the habit of only updating the screen when necessary, commonly my programs have a changed% flag that I set if I know something needs to be redrawn.

If the program is for your own use and you don't care that's fine, but you'll have some unhappy users if you offer it to others without fixing this.
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Re: Text gaming for the web

Post by mikeg »

What I would say is that it uses a lot of power, either running locally or in the browser, making my laptop quite hot (and draining the battery)
I'll take a closer look. I am not sure how many have started to use it. Efficiency is important. The mouse movement in the zone I think is also affected if say another window is active over it..

The program was tweaked for lots of WAIT calls and I tried to make it function more like how Windows might manage many things. The thing was to make it immediately responsive to any action, as when I trade, the faster I can react, the more chance I can get more shares in a stock when I sell and try to buy a lower priced location. Being able to calculate quickly the amount of shares I can buy with the same amount of money is critical.

I don't want my computer to work too hard and a pause button just will make it harder to respond quickly, so I will just make it more efficient on the redraws.

The program worked surprisingly well in the browser by the way.. I was impressed. ( this has potential, for presentations, and education, gaming, commercial and anything web based including interactive web sites.)
I knew when I saw this, that it was a huge step beyond anything so far seen.
Richard, this is huge, and this on its own should be having an effect soon, as word is spreading fast in the east. I know from invites in the FB group.
Focus is on code subject. Feel free to judge the quality of my work.
RichardRussell

Re: Text gaming for the web

Post by RichardRussell »

mikeg wrote: Wed 23 Sep 2020, 00:52The program was tweaked for lots of WAIT calls
The WAITs are effective in reducing the CPU load in the interpreter thread, but between those WAITs you are drawing graphics or text, and this keeps the GUI thread busy (for performance reasons the GUI thread keeps spinning for a while before going idle).
The thing was to make it immediately responsive to any action
I'm not suggesting that you make it any less responsive, but that you remove unnecessary drawing calls when they are simply redrawing what is already there. There are loads of examples in your main REPEAT ... UNTIL loop, such as this which draws the "PLAY MONEY..." text whether the value of money has changed or not:

Code: Select all

        GCOL 6:MOVE 20,450:PRINT "PLAY MONEY $ "+STR$(money)
In your position I'd restructure the code so that all the drawing is done in one routine, something like PROCredraw, which you call only when something has changed, using a global flag like Changed%.

This is basic stuff really. Old computers like the BBC Micro, which consumed the same amount of power however much work the CPU was being asked to do, encouraged 'lazy' programming in which you keep doing the same thing over and over again unnecessarily. But that isn't acceptable these days, especially on battery-powered devices. Anything which is expensive of CPU time (which drawing graphics or text on the screen always is) should be done only when necessary,
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Re: Text gaming for the web

Post by mikeg »

I created a couple in program live tools to fine tune the program. I used the counter to show me how fast the program was cycling.

I made the sleep tool before the counter. I have drastically cut unnecessary cycle times and I think I can even do more adjustment.. maybe to WAIT 50 when mouse is not within my arrows range.
I'm not suggesting that you make it any less responsive, but that you remove unnecessary drawing calls when they are simply redrawing what is already there
I just read what you posted as I had finished this current solution I posted.
I will work on what you are talking about also, but this was a quick fix and it made some tools for me to use for future projects)
Thanks Richard.
The *REFRESH could be removed if someone chooses to not use *REFRESH (anyone can use these tools if they want)
I plan to improve these tools over time so they are more plug an play friendly

Code: Select all

      REM EFFICIENCY COUNTER program cycles ( faster numbers= inefficient )
      REM x,y is location of the counter
      REM USE- if you are counting program cycles in an area you want to keep low activity (mouse areas)
      DEFPROCtick(x,y)
      PRIVATE t%
      t%+=1
      MOVE x,y:GCOL 15:PRINT STR$(t%):*REFRESH
      GCOL 128+0:WAIT 5
      GCOL 6:MOVE x,y:PRINT STR$(t%)
      ENDPROC
      REM for active area. If mouse is outside this area SLEEP (bx%,by% lower left tx%,ty% upper right corner)
      DEFPROCsleep(bx%,by%,tx%,ty%)
      LOCAL mx%,my%,mb%,pass%
      REPEAT
        MOUSE mx%,my%,mb%
        IF mx%>bx% AND mx%<tx% AND my%>by% AND my%<ty% THEN pass%=1 ELSE WAIT 40:pass%=1
      UNTIL pass%=1
      ENDPROC

Focus is on code subject. Feel free to judge the quality of my work.
RichardRussell

Re: Text gaming for the web

Post by RichardRussell »

mikeg wrote: Wed 23 Sep 2020, 16:01 I created a couple in program live tools to fine tune the program. I used the counter to show me how fast the program was cycling.
It's not easy to 'tune' multithreaded programs. For example only a little while ago I posted here about a problem I encountered in which one of my programs was using virtually no time in the interpreter thread (as measured using my Profiler tool) but 100% of a core in the GUI thread, which turned out to be because I had an OFF statement in a loop!

So your primary measure should be what Windows' Task Manager is saying is the total CPU usage of your program. When idling (i.e. waiting for input, with the mouse stationary) it should ideally be using no more than 1% total CPU time. SDLIDE.bbc (my IDE for BBCSDL) is a large and complex program - more than 4,600 lines - yet in an idle condition it uses typically only 0.5% CPU which is acceptable.

Your Stock Trader program in the equivalent idle state is using 35% CPU on this laptop, which is terrible! Unfortunately the measurements you are making in your Efficiency Counter aren't taking account of CPU usage of the GUI thread, but only in the interpreter thread, which is not going to be helpful in solving the problem.

Anyway, doesn't the Profiler tool (in BB4W and BBCSDL) tell you just as much about CPU usage in the interpreter thread as your Efficiency Counter?