So I realized that it has been quite a while sine I've done one of these. So, for this one I decided to choose a topic that I really haven't ever seen discussed in tutorials or guides or anything. So here you have it, a code explanation about jumping
Without further adue, here is the code I will be working with.
ClrHome
8→X
1→Y
DelVar ARepeat 0
getKey→K
Output(X,Y,"_
Y+(K=26)-(K=24
Ans-16((Ans=17)-not(Ans→Y
X-A→X
A-2(X=5→A
If X=8
DelVar A
Output(X,Y,"π
A+(K=25 and X=8→A
rand(11
End
Note:
This is NOT the best example for jumping. It is more based for concept. If someone would provide me code that does the same thing as this program (in terms of looking the same) then I will happily disect that code too for everyone.
Ok, so I will just go line by line and do this and explain as I go. I will then create a commented set of code at the bottom that covers everything too.
Let's get started.
ClrHome: I'm assuming most of you know what this does. But for those who are very beginners this command clears the homescreen of everything that is on it. This is also the operating systems way of doing this. There are multiple ways of clearing the screen with code but this will be your fastest and most memory efficient way.
8→X and
1→Y: Again this is a fairly simple thing to know. But basically you are storing the integer (in this case anyways) into the variable. The
→ command is used to store what ever is on the left side into what is on the right. So in these examples we are storing eight into the variable
X and one into
Y.
But the
→ command isn't just used for integers and variables. You can store things such as strings (a set of characters) into a string (represented on the calculator as
Str1,
Str2, etc.). An example of this would be storing "
HELLO WORLD!" into
Str1. The code would look like:
"HELLO_WORLD!→Str1
But this still isn't all. You can store all sorts of things such as matricies, lists, dimetions, plus some other stuff. But that is all for a different guide
DelVar ARepeat 0: This is actually two lines of code in one. It is split up into
DelVar A and
Repeat 0. First, the
DelVar A part. What
DelVar does is clear out a variable and stores zero into it or if it is used with a list or matrix or something it completely resets it. Now for the
Repeat 0 part.
Repeat is one of the three main looping commands. There is
For(,
While, and
Repeat that all do different things (well sorta, they more just act differently). I will only explain
Repeat for the sake of this break-down. What
Repeat does is repeat a block of code until a certain condition is true. This means that the way this works is that you put a Boolean conditional as the argument. A Boolean conditional is statement that will either return true or false (one or zero, one being true and zero being false). This are often recognized by their use of
=,
≠,
≥,
>,
≤, and
<. Not always, but usually. So since
Repeat repeats until true, and zero means false, this loop will loop indefinetly.
Tip:
If you ever use
0→A, or zero stored into any variable, then replace it with
DelVar A, or what ever variable is used. It is much more memory efficient. Also, I forgot to mention that you can stack multiple
DelVars in a row and before any other command or length of code. An example is:
1→A
Ans→B
Ans→C
Ans→D
Repeat 0
getKey
If Ans
DelVar ADelVar BC+D→A
End
When you break the code and see what
A is it will equal two.
getKey→K: So the
getKey command is a standard in games and is fairly simple. It is the command that detects key presses. Each key is assigned a different number. For the most part each row starts at a multiple of ten plus one number, so the first row starts with eleven. Then you just count outwards to the right until the end of the row then, instead of wrapping around, the next row starts with that first buttons key number on the line before plus ten. So row two, the row with [2ND], would start with twenty-one. A good way to find out which keys are which just use this program:
Repeat 0
getKey
If Ans
Disp Ans
End
Note:
All the keys have a number assigned EXCEPT for the [ON] button. In TI-BASIC that key will always break the code execution.
Output(X,Y,"_:
Output( is yet another simple command. It simply outputs what ever is in the third argument on to the homescreen at point
(Y,X). The y-coordinate, which is the first argument, can go from one to eight while the x-coordinate can be from one to sixteen. If you go beyond this you will get an
ERR:DOMAIN error. However this does not restrict you to using only sixteen characters per line. When the output goes beyond column sixteen it will wrap down to the next line. Here is an example of this:
"*
For(A,1,7
Ans+Ans
End
Output(1,1,Ans
So the point of this line in particular is that it is puting a single space at where the future pi guy was, clearing that spot and erasing a trail. If you didn't have this you would leave a trail of pi characters each time you moved or jumped.
Y+(K=26)-(K=24: This is the start of where that Boolean logic I talked about comes into play. What we have here is the
Y variable, which holds our x-coordinate of our character, being affected by which key is pressed (remember, we stored the value from
getKey into
K). If the the key pressed was the left arrow then
K will be equal to twenty-four then the Boolean conditional
(K=24) will equal one. This in turn subtracts one from
Y. If you push the right arrow button then
K will equal twenty-six and then add one to
Y.
Ans-16((Ans=17)-not(Ans→Y: This line is basically a run on of the last line. It take the answer from it and will either subtract or add sixteen to that number if the number is seventeen (subtracts from) or zero (adds to) because both of these will result in a
ERR:DOMAIN. If it doesn't equal either of those then it will just store what ever number was the answer to
Y. So, basically, this line is the line that makes the pi character wrap around the screen if he goes off one end.
X-A→X: As talked about when explaining the
Output( command,
X is our y-coordinate. So since this is about jumping we need some type of variable that changes that affects when our guy jumps, that is the
A variable. Basically though this is subtracting what ever value is in
A from
X to give the appearance of jumping.
A-2(X=5→A: Hey, look, another Boolean statement. What is happening here is that we are constantly storing
A into
A but if
X equals five then it will subtract two from
A and store negative one to
A (the only way for
X to equal five is if the guy is jumping and
A will equal one at that point).
If X=8: our first
If statement. It's pretty simple. If the statement is true, it will execute THE NEXT LINE, not just the next command. This is why on the next line that
DelVar A is on a line of it's own instead of being
DelVar AOutput(X,Y,"π.
DelVar A: As explained before
DelVar just deletes what ever is in it's argument. In this case it is a variable so it sets the value to zero. This only happens though once
X becomese eight again after a jump.
Output(X,Y,"π: This is pretty obvious now. It will output the Greek letter pi at the cooridnates
(Y,X). This is what will be jumping too.
A+(K=25 and X=8→A: This is where our jumping initially starts.
A will always be equal to zero unless the character is in the middle of a jump (then it will be equal to either one or negative one). If you push the up arrow key it will store a value of twenty-five to
K which meets the first part of this conditional. Then we need
X to be equal to eight, which is when the character is on the ground. As long as both of those conditions are true then you will initiate a jump by storing one into
A.
rand(11: This is basically here to slow this loop down. Without it here the loop will run to quickly for you to see what is happening during a jump. You can always fine tune this to what you like by messing with the number. But basically how this works is that the
rand command creates a "random" number (I say "random" because technically a computer can't produce a truly random number). When you add the second argument of
(11 then you are telling it to create an abitrary list of random numbers which takes a bit to create because calculating these random numbers takes time.
End: Woo, finally to the end. This just marks the end of our
Repeat loop.
Commented code time:
ClrHome \\Initially clears the homescreen of any junk previously on it.
8→X \\Stores the integer eight into the variable "X."
1→Y \\Stores the integer one into the variable "Y."
DelVar ARepeat 0 \\Our double line. First it deletes the integer previously stored in variable "A" and replaces it with zero. Then it starts an indefinite loop.
getKey→K \\When you press a button it stores what ever value from the key you pressed into the variable "K."
Output(X,Y,"_ \\Outputs a single space at the coordinates (Y,X) to erase the trail that would be left behind.
Y+(K=26)-(K=24 \\This controls the movement left and right. If the left arrow is pressed then twenty-four is stored to "K" and subtracts one from "Y."
Ans-16((Ans=17)-not(Ans→Y \\Takes the answer from the previos line and either stores it as is, subtracts sixteen if the answer was seventeen or adds sixteen if the answer was zero.
X-A→X \\Applies our "jumping" variable to the variable "X."
A-2(X=5→A \\When our character reaches three spaces up in the jump it will subtract two from "A" to make the guy go back down.
If X=8 \\If the value of "X" equals eight then it will execute the next line.
DelVar A \\Deletes the integer stored to "A" and replaces it with zero if the value in "X" equals eight.
Output(X,Y,"π \\Outputs our character, the Greek letter pi, at the coordinates (Y,X).
A+(K=25 and X=8→A \\Adds one to the "A" variable if the up button is pressed the guy is on the floor. This initiates the jump.
rand(11 \\Effectively slows down our loop enough to let us see what is happening with the jump. Creates an abitrary list of eleven random numbers.
End \\Marks the end of our Repeat loop.
Ok, we have reached the end. Hopefully you have learned something. Again, this is a poor example of jumping code and there are much better examples out there. If someone provides me with a set of code that does this same thing but is much better I will happily explain it and do a code breakdown for it.
Thanks:
ztrumpet and nemo: For helping me with the concepts of creating simple jumping code. Thanks guys
I really hope this has been helpful and enjoy