Author Topic: Movement Code Explanations  (Read 14362 times)

0 Members and 1 Guest are viewing this topic.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Movement Code Explanations
« on: March 07, 2010, 12:05:00 am »
The other day Ztrumpet gave some TI-BASIC code for moving something on the homescreen over at United-TI. I went and fully explained both these sets of code in great detail (the first was more detailed than the second). I'm not sure how many people will find this usefull but if you're new to programming it might help. :)

I will be working from the top line to the bottom line. Also, please let me know if anything is incorrect, I will change it.

This is the first example he gave:

Code: [Select]
8→A
ClrHome
Repeat K=105
Output(1,A,"*
Repeat Ans
getKey→K
End
Output(1,A,"_
A+(Ans=26 and A<16)-(Ans=24 and A>1→A
End

8→A: This is pretty easy. You are storing a value of 8 to the variable A.
ClrHome: Again, a pretty easy one. This just erases all the data on the homescreen. So say you have a bunch of equations and answers there from math and then you run that all of it will disappear.

Code: [Select]
Before ClrHome:
_____521.3144122
prgmBINTODEC____
?100101_________
______________37
prgmDECTOBIN____
A=?764__________
1011111100______
________________

After ClrHome:
________________
________________
________________
________________
________________
________________
________________
________________

Repeat K=105: Well like ztrumpet said earlier Repeat loops are basically loops ran until the condition is met (post-test apparently). Post-test, from what I understand, basically means that it will check to see if the statement is true at the end of the loop, instead of first.
As for the next part of that, the K=105, I think it was basically covered. But as you saw later in the code getkey is getting stored to the variable K. This means that when [Enter] is pressed (the key code for [Enter] is 105 (without using other things, but that's more advanced stuff I don't even understand currently)) the loop will finish and finish the program (since nothing is after the loop).
Output(1,A,"*: So Output( was also kind of covered. Output( has three basic components to it: the row number, column number, and the thing to be displayed (in that order). For example:

Code: [Select]
The program:
PROGRAM:TEST
ClrHome
Output(4,7,"HELLO

What it displays:
________________
________________
________________
______HELLO_____
________________
________________
________________
________________

The Output( here is basically placing the word "HELLO" the fourth row down and starting the "H" at the seventh column. If you didn't know the homescreen of the calculator is on a axis from 1-16 horizontally and 1-8 vertically (the graph screen is something like 0-94 horizontally and 0-62 vertically, I think).

Code: [Select]
_1234567890123456 //The "0123456" after the "9" stand for 10, 11, 12, 13, 14, 15, and 16.
1________________
2________________
3________________
4________________
5________________
6________________
7________________
8________________

Make sense?
Ok, back to the Output(1,A,"* in the program. This line basically just displays a * into row one and in column A.
Repeat Ans: This basically does what ztrumpet said. It stays in this loop until you press any key. The reason for this, though, is that a key press is a form of Ans. An example to show this would be:

Code: [Select]
PROGRAM:TEST
Repeat 0 //Starts an endless loop.
Repeat Ans //Starts another loop that waits till something is stored to "Ans."
getKey //Gets the key code of the key that was pressed.
End //Ends the "Repeat Ans" loop.
Disp Ans //Displays the value stored to "Ans." In this case it is the key code value.
End //Ends the "Repeat 0" loop.

getKey→K: This is what triggers the loop it is nested in. When you press a key the getKey command will capture this code (hence it being called getKey haha) and store it into Ans AND the variable K. This is because it is the answer from getKey but getKey is storing it's information to the variable K. You technically don't need the ...→K part of this, but you will run slower if you don't.
End: This basically just lets the program know that is the end of the loop. End commands are needed at the end of each type of loop (whether it is While, For(, or Repeat). You need it at the end of other things but I'm not covering that now. Sorry.
Output(1,A,"_: This is basically like the other Output( command, just instead of displaying a * it displays space. The point of this one though is that it erases the trail that would normally be there. Quick recap: 1 is the row number, A is the column number (this changes with the next line), and a space is what is being displayed.
A+(Ans=26 and A<16)-(Ans=24 and A>1→A: Well this one is going to be a doozy haha. I'm going to try to split this up.
-(Ans=26 and A<16) and (Ans=24 and A>1: So this is definitely better explained here (Boolean Logic) and here (Piecewise Expressions). But I'll try to explain this some. Basically when you have a function that uses =, , and anything else in the [2nd][MATH] menu (in both TEST and LOGIC list) you will return with either a 1 (meaning the statement is true) or 0 (meaning the statement is false). A quick program to show this is:

Code: [Select]
PROGRAM:TEST
Repeat 0 //Enters endless loop.
Prompt A //Prompts for the value of A.
A≤50 //If A is less than or equal to A then it returns "1" as an answer. If A is greater than 50 it returns a "0."
Disp Ans //Displays the answer. Will be either "1" or "0."
End //Ends "Repeat 0" loop.

Essentially this means that the (Ans=26 and A<16) and (Ans=24 and A>1 can be replaced with "1's" and "0's" depending on which is true when. That covers that part.
-The next part I'm going to explain is the individual parts (not the "1's" and "0's" aspect though). First, the Ans=24 and Ans=26 pieces. Basically Ans right here is the value of getKey or K because that was the last thing to be stored to it. Easy enough, right? The A>1 and A<16 are easy to understand too, I hope. A is the column number, as I have said, so this is making sure it remains above 1 but lower than 16. otherwise you will get a ERR:DOMAIN error (I'm pretty sure that is the correct error). The only part left of those commands is the and part. This is pretty self-explanitory. Basically you have to have both the conditions met (to obtain a 1 for an output) otherwise the whole condition will be a 0 output.
-Finally, to put that all together. Depending which condition is made true ((Ans=26 and A<16) or (Ans=24 and A>1) you are adding or subtracting 1 (or 0 if neither are met).
I am hoping I explained that line of code well enough.
End: Finally, the last line of code haha. This one is simply ending the Repeat K=105 loop.

I believe I covered all of the lines of code, also I hope I explained what they do and how they work thouroughly and correctly. Please let me know if I have made a mistake somewhere, I will fix it.

As a final little thing, I will explain what each line of code does in the actual program just briefly. As a final recap.

Code: [Select]
8→A //Stores a value of 8 to the variable "A."
ClrHome //This clears the homescreen completly.
Repeat K=105 //This loop is repeated until the [ENTER] button is pressed.
Output(1,A,"_ //Displays a space in row one, into column "A."
Repeat Ans //Repeats this loop until any button is pressed.
getKey→K //Stores the key code value to the variable "K."
End //Ends the "Repeat Ans" loop.
Output(1,A,"* //Displays a "*" in row one, into column "A" (replacing the space).
A+(Ans=26 and A<16)-(Ans=24 and A>1→A //Adds or subtracts either "1" or "0" to "A" depending which condition is true, if any.
End //Ends the "Repeat K=105" loop

Here was the second example that I explained:

Code: [Select]
8→A
ClrHome
Repeat Ans=105
A+(Ans=26)-(Ans=24
Ans-16((Ans=17)-not(Ans→A
Output(1,Ans,"*
Repeat Ans
getKey
End
Output(1,A,"_
End

Ok, well like last time i will go line by line, but probably not into nearly as much detail. Lets start:
8→A: Pretty simple. You're storing a value of 8 to the variable A.
ClrHome: This clears the homescreen of everything that is currently on it. For more detail look at my last explanation.
Repeat Ans=105: This is the beginning of the a Repeat loop that is waiting for you to hit [ENTER] (since it's key code is 105).
A+(Ans=26)-(Ans=24: Little trickier. A currently has a value of 8 (when the program is run) so when you hit the left or right arrow key (which will be stored in Ans because getKey is the last line of the loop) you will either add 1, -1, or 0 to A. This answer will now be stored to Ans which sets up the next line.
Ans-16((Ans=17)-not(Ans→A: Even more tricky. This is the line that lets the * wrap around. Like I had said the new value of A has been stored to Ans in the last line. You are essentially adding either 16, -16, or 0 and then storing it to A. How this works is that it takes the value in Ans and if this value is equal to either 17 or 0 you are multiplying 16 by 1 or -1 (thanks to the Boolean Logic I explained earlier with conditions). The reason this works with zero though is because of the not( command. This essentially reverses the Boolean Logic and returns the opossite (true becomes false and false becomes true). Now, to be honest this doesn't make the most ammount of sense to me but I do know why it is used here. Using the not( command in this way lets you replace the statement Ans=0 (because they return the same answer). It's used as an optimization right here.
Output(1,Ans,"*: This outputs a * into row one, Ans number of columns out. You can use Ans here because of the last line. You are storing the value of A to Ans because it is a math operation basically. Hope that makes sense.
Repeat Ans: This starts a loop that waits for you to push a button. It does this because the value from getKey gets stored to Ans.
End: Ends the Repeat Ans loop. As specified earlier (in the previous explanation) End's specify the end of code inside loops (or a few other things that require them).
Output(1,A,"_: This displays a space into row one and in column A. The reason for this Output( is to erase the * that would normally trail behind.
End: This ends the Repeat Ans=105 loop. This completes the code so if you hit [ENTER] it will end the program (since nothing is beyond the End).

So, quick recap:

Code: [Select]
8→A //Stores a value of 8 to the variable "A."
ClrHome //Clears the homescreen of anything that is on it.
Repeat Ans=105 //Begins a loop that waits for [ENTER] to be pressed to end the program.
A+(Ans=26)-(Ans=24 //Adds 1 or -1 to A depending if [◄] or [►] is pressed. "A" gets stored to "Ans."
Ans-16((Ans=17)-not(Ans→A //This adds or subtracts 16 from "Ans" if "Ans" is equal to 17 or 0. Adds nothing if it doesn't equal either one. Stores the answer to "A."
Output(1,Ans,"* //Displays a "*" in row one and "Ans" columns out.
Repeat Ans //Starts a loop that waits for you to press a button.
getKey //Stores the value of the key pressed to "Ans."
End //Ends the "Repeat Ans" loop.
Output(1,A,"_ //Displays a space in row one and in column "A." This erases the "*" left behind.
End //Ends the "Repeat Ans=105" loop.

I hope this helps anyone who had stuff to take away from this. Again, just thought some might like this. Enjoy :)
« Last Edit: March 10, 2010, 08:28:57 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Geekboy1011

  • The Oneironaut
  • Donator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2031
  • Rating: +119/-2
  • Dream that Awakening dream
    • View Profile
Re: Movement Code Explanations
« Reply #1 on: March 07, 2010, 12:06:20 am »
wow wall of post X.x

any way nice i think it will help a lot of people

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: Movement Code Explanations
« Reply #2 on: March 07, 2010, 12:09:21 am »
Code: [Select]
8→A
ClrHome
Repeat K=105
Output(1,A,"_ //This should be *
Repeat Ans
getKey→K
End
Output(1,A,"* //This should be a space
A+(Ans=26 and A<16)-(Ans=24 and A>1→A
End
Nice job! :)

Could you fix my typo and flip the * and _?  Thanks!

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Movement Code Explanations
« Reply #3 on: March 07, 2010, 12:12:48 am »
@ztrumpet
I changed it. But I'm gonna leave it on UTI because I'm just to lazy to go back and do it again :P Plus we have mentioned it there. O0

@Geekboy1011
Thanks, i really hope so.
« Last Edit: March 07, 2010, 12:26:05 am by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Movement Code Explanations
« Reply #4 on: March 07, 2010, 01:10:22 am »
Nice. I will move this to TI-BASIC routines, though, so it's easier to find for BASIC coders ^^

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Movement Code Explanations
« Reply #5 on: March 07, 2010, 01:49:05 am »
Ok, sounds good. I wasn't sure where to post this. I just posted it where it says you can "get or give calculator help..." Works either way though, thank you O0
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Movement Code Explanations
« Reply #6 on: March 07, 2010, 02:04:25 am »
no problem :)

Help and support is ok too, but I like to put the BASIC routines separate so they're easier to find :)

And I see you,re familiar with secret easter egg SMF forum emoticons :P

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Movement Code Explanations
« Reply #7 on: March 07, 2010, 02:13:27 am »
I understand, I figured either would work but I get why this is better. And ya, thanks to ztrumpet I am lol. How many are there?
« Last Edit: March 07, 2010, 02:19:25 am by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Movement Code Explanations
« Reply #8 on: March 07, 2010, 02:20:35 am »
I don't exactly remember, but in Google if you search for SMF easter egg, you might find some. I would add custom emoticons too if I knew how the emoticon system worked x.x

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Movement Code Explanations
« Reply #9 on: March 07, 2010, 02:28:46 am »
Haha I already tried. Where I saw it made it seem you (as in you) had to add them all personally and that is what limited them. I shall try again!
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline tpt1234567890

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: Movement Code Explanations
« Reply #10 on: October 28, 2013, 11:38:04 pm »
Very nicely explained!