Author Topic: BatLib/ReCode Examples  (Read 24141 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
BatLib/ReCode Examples
« on: April 08, 2011, 11:47:31 am »
Most of what I know about programming, I learnt it by looking at sample codes and examples, so I thought that I could make some BatLib samples and explain them.

Here's my first code sample, draw a rectangle around the screen and wait for pressing [CLEAR] to close.

Code: [Select]
dim(1,40                 ; Disables Indicator (dim(1)) and Turns Basic ReCode on ((dim(40)). In BatLib, we can have multiple commands in a single dim(
Fill(0                   ; AxesOff, ClrDraw, basically, it cleans the screen in a Recode Block
While getKey≠15          ; waits till a [CLEAR] press
Line(0,96,0,64,6         ; Draws a rectangle, X=0, WIDTH=96,Y=0;HEIGHT=64;TYPE=6. Type 6 is borders only
DispGraph                ; Displays the graphscreen
End                      ; End of loop
Stop                     ; Disables ReCode and goes back to BASIC
AxesOn                   ; Turns Axes on

First of all, I need to clear that you should read the readme and recode readme first.

Then, this code is very simple and here's how it looks when ran:



Hope you enjoyed this example, more to come.

Attached is the 8xp file.

EDIT: fixed up spacing and added ≠ with Scout's approval
« Last Edit: April 08, 2011, 03:42:51 pm by Xeda112358 »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: BatLib/ReCode Examples
« Reply #1 on: April 09, 2011, 03:58:45 am »
Here it comes, the second sample. It's going to do exactly what we did in the first sample, but in a much faster and smaller way!

Code: [Select]
dim(1,40                 ; Disables Indicator (dim(1)) and Turns Basic ReCode on ((dim(40)). In BatLib, we can have multiple commands in a single dim(
Fill(0                   ; AxesOff, ClrDraw, basically, it cleans the screen in a Recode Block
While getKey≠15          ; waits till a [CLEAR] press
Line(0,96,0,64,6         ; Draws a rectangle, X=0, WIDTH=96,Y=0;HEIGHT=64;TYPE=6. Type 6 is borders only
DispGraph                ; Displays the graphscreen
End                      ; End of loop
Stop                     ; Disables ReCode and goes back to BASIC
AxesOn                   ; Turns Axes on

This was last sample, here's the new sample, it doesn't use ReCode, only TI-Basic and BatLib:

Code: [Select]
While getKey!=45  ; "!=" is the difference symbol
dim(1,29,0,96,0,64,6,99
End
ClrDraw:ClrHome

Let's take a look at the meat of the program: dim(1,29,0,96,0,64,6,99, which can be broken to:

Code: [Select]
dim(1 ;Disables Indicator
dim(29,0,96,0,64,6 ;Creates a rectangle of type (6) borders only at (0,0)
dim(99 ;DispGraph

Hope you liked it ;)
« Last Edit: April 09, 2011, 12:46:31 pm by Xeda112358 »

Offline Xeda112358

  • they/them
  • Project Author
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #2 on: April 09, 2011, 12:45:50 pm »
Yes, that is exactly the technique that I love about BatLib, personally. So here is another common example I use...
First, you want to draw a sprite or a rectangle using the XOR (or invert) option and you display the graph. Then, you can use dim(10,x to filter key presses (like waiting for arrows and the column of keys containing enter to be pressed) If there are two or more groups of keys, you can use dim(43 as another getkey. After that, you can use the first thing you did again without updating the screen.

TL;DR
Pretty much, this will display a rectangle, wait for a key press, and restore the screen:
Code: [Select]
dim(29,B,8,C,8,2
dim(99               ;Displays the inverted rectangle
dim(10,3             ;waits until Arrows or the column with enter to be pressed
dim(43→A             ;returns the key in Ans and is stored to A
dim(29,B,8,C,8,2     ;This reinverts the rectangle, but it isn't shown yet. Great for smooth graphics.
However, this can be all put together into one long sequence of commands by combining the inputs:
dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2→A

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #3 on: April 09, 2011, 12:48:17 pm »
I still don't understand how the chaining works; my brain doesn't comprehend the awesome. ;D
What if you leave arguments out of a command?  How does it know to go on to the next command?

Offline Xeda112358

  • they/them
  • Project Author
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #4 on: April 09, 2011, 12:53:58 pm »
That is the downside to chaining commands... you cannot leave out arguments unless they are at the end. However, if you want to you can leave out arguments before a string argument, even if you are chaining.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: BatLib/ReCode Examples
« Reply #5 on: April 10, 2011, 05:11:33 am »
I still don't understand how the chaining works; my brain doesn't comprehend the awesome. ;D
What if you leave arguments out of a command?  How does it know to go on to the next command?

That's true, we can't forget any argument, but it makes code much smaller.

Zeda, does it count number of arguments to get to the next command?

Offline Xeda112358

  • they/them
  • Project Author
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #6 on: April 10, 2011, 11:05:34 am »
No, it takes the first argument to decide which command to execute and then the command will use any arguments necessary. When the command is finished, it reads the next argument to figure out the next command. If BatLib reads a string when it expects a number, though, it reads the number as 0 and waits at the string until it is needed. That is why you can omit arguments before a string, even when stringing commands together :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: BatLib/ReCode Examples
« Reply #7 on: April 10, 2011, 11:30:06 am »
Zeda, since I like to keep it clean, here's my code for your example:

Code: [Select]
:dim(40
:Fill(0
:Stop
:6→D
:DelVar B0→C
:Repeat A=15
:dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2→A
:max(0,min(88,B+Dsum(DeltaList(A={2,3→B
:max(0,min(56,C+Dsum(DeltaList(A={4,1→C
:End

This cleans the screen and we don't have to see the ugly screen of BatLib App.

Offline Xeda112358

  • they/them
  • Project Author
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #8 on: April 10, 2011, 11:33:36 am »
Yeah, I just needed to show that the cursor doesn't change what is underneath it :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: BatLib/ReCode Examples
« Reply #9 on: April 10, 2011, 11:34:50 am »
Yeah, I just needed to show that the cursor doesn't change what is underneath it :)

Can you add a getKey for 2ND that will change it? So, if you press 2ND, it will dim(29,BB,8,C,8, turning to black the place where the cursor is.

So, like a paint program.

Offline Xeda112358

  • they/them
  • Project Author
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #10 on: April 10, 2011, 11:37:08 am »
Yeah, if you check my demos program topic, you will find a better cursor routine that does something similar XD

EDIT: Never mind, it is here: http://ourl.ca/9627/184739

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: BatLib/ReCode Examples
« Reply #11 on: April 10, 2011, 02:48:00 pm »
I just made a BatLib (almost no ReCode) program that draws a cursor and when the user presses [ENTER] it draws a rectangle of 8*8 in the place where the cursor is. A Paint program we might say.

I'll make a ReCode example in a few minutes and post it here, it should be faster and much better (4*4 rectangle cursor).

Code:
Code: [Select]
dim(40
Fill(0                       ;Inside ReCode blocks, this clears the screen, turns axes off
Stop
6->D
DelVar B0->C
Lbl Y
Repeat A=15             ;Will loop until pressing [CLEAR]
If getKey=105           ;GetKey for drawing
Then
Goto Z
End
dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2->A
max(0,min(88,B+Dsum(DeltaList(A={2,3->B
max(0,min(56,C+Dsum(DeltaList(A={4,1->C
End
Return
Lbl Z
dim(29,B,8,C,8,1  ;Draws Rectangle
Goto Y               ;This goes back to beginning of loop

Probably it can be optimized, but I like it this way.

Screenshot:


Enjoy it, if you have any questions, comment.

EDIT:
Here's some updated, optimized code:

Code: [Select]
dim(1,56,4
6->D
DelVar B0->C
Lbl Y
Repeat A=15             ;Will loop until pressing [CLEAR]
If getKey=105           ;GetKey for drawing
Then
Goto Z
End
dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2->A
max(0,min(88,B+Dsum(DeltaList(A={2,3->B
max(0,min(56,C+Dsum(DeltaList(A={4,1->C
End
Return
Lbl Z
dim(29,B,8,C,8,1  ;Draws Rectangle
Goto Y               ;This goes back to beginning of loop
« Last Edit: April 10, 2011, 06:08:06 pm by Scout »

Offline Xeda112358

  • they/them
  • Project Author
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #12 on: April 10, 2011, 03:20:41 pm »
Sweet, nice job, Scout! I actually didn't know it would be that fast! Also, you didn't really need the ReCode block because you could do dim(56,4 instead. Still, it shows how ReCode can be integrated with BASIC nicely! Thanks!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: BatLib/ReCode Examples
« Reply #13 on: April 10, 2011, 03:23:43 pm »
Here's a very similar example, but it uses ReCode and not Basic+BatLib.

Code:
Code: [Select]
dim(40
Fill(0
3->D
->E
0->A->B->C
While A!=15
If A=9
Line(B,D,C,E,1
Line(B,D,C,E,2
DispGraph
getKey->A=0
DS<(1
Line(B,D,C,E,2
B
If A=3
+1
If A=2
-1
->B
C
If A=1
+1
If A=4
-1
->C
If B>=99
0->B
If B=97-D
96-D->B
If C=65-E
64-E->C
If C>=99
0->C
End
Stop

Screenshot:


I have to say I think the samples are getting quite complex, so I'll make a few simpler ones too.

Notes about this example:
Attached is the code in a 8xp file.
This was an example that focuses on ReCode and uses a lot of Zeda-made code.
« Last Edit: April 10, 2011, 03:24:19 pm by Scout »

Offline Xeda112358

  • they/them
  • Project Author
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: BatLib/ReCode Examples
« Reply #14 on: April 10, 2011, 06:12:42 pm »
Again, very nice! And I like the display of speed and smoothness there, too. I keep trying to remake balltrix (made by DJ) into a ReCode game, but I keep forgetting to backup my calc before testing out other code !_!