Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: meishe91 on August 07, 2010, 01:23:04 am

Title: Jump Code Explanation
Post by: meishe91 on August 07, 2010, 01:23:04 am
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.

Code: [Select]
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:

Code: [Select]
"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:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
"*
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:

Code: [Select]
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 :)
Title: Re: Jump Code Explanation
Post by: Raylin on August 07, 2010, 09:32:00 am
I like.

++
Title: Re: Jump Code Explanation
Post by: FinaleTI on August 07, 2010, 10:23:50 am
I like it too.
One thing you could do to speed it up is like what was done for Metroid Pure. You have 2 sets of variables for the placement of the character.
Code: [Select]
Output(C,D,"_
Output(X,Y,"π
X→C:Y→D
A modified version of the code above would be:
Code: [Select]
ClrHome
8→X:Ans→C
1→Y:Ans→D
DelVar ARepeat 0
getKey→K
Output(C,D,"_
Output(X,Y,"π
X→C:Y→D
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
A+(K=25 and X=8→A
End
Title: Re: Jump Code Explanation
Post by: DJ Omnimaga on August 07, 2010, 03:06:52 pm
I saw this on United-TI earlier. Awesome job :)
Title: Re: Jump Code Explanation
Post by: ztrumpet on August 07, 2010, 06:34:44 pm
Awesome job!  I just have a couple of things to nitpick at. :)

"DelVar ARepeat 0"
I recommend staying away from putting DelVar on the same line as control structures.  This works fine now, but if you try this code you can see what can happen:
For(A,0,1
If not(A
Then
Delvar BIf 1
Then
Disp "HI
End
End
End
Make sense. :D

"There is For(, While, and Repeat that all do differnt things (well sorta, they more just act differently)."
Sorry to nitpick, but I thought it looked too weird to not be a grammar Nazi on. :)  (different)

"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."
Not quite.  It's only more memory efficient if you (ab)use the "extra line" feature of DelVar.  Even then, it only saves you one byte.

==================

Regardless of this constructive criticism, I think this is a wonderful tutorial!  Excellent job!  Thanks Meishe. ;D
Title: Re: Jump Code Explanation
Post by: meishe91 on August 07, 2010, 09:10:51 pm
I saw this on United-TI earlier. Awesome job :)

Ya, I uploaded it there first because since I use Weregoose's type face it's easier to type [goose][/goose] instead of everything you have to here :P

Awesome job!  I just have a couple of things to nitpick at. :)

"DelVar ARepeat 0"
I recommend staying away from putting DelVar on the same line as control structures.  This works fine now, but if you try this code you can see what can happen:
For(A,0,1
If not(A
Then
Delvar BIf 1
Then
Disp "HI
End
End
End
Make sense. :D

"There is For(, While, and Repeat that all do differnt things (well sorta, they more just act differently)."
Sorry to nitpick, but I thought it looked too weird to not be a grammar Nazi on. :)  (different)

"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."
Not quite.  It's only more memory efficient if you (ab)use the "extra line" feature of DelVar.  Even then, it only saves you one byte.

==================

Regardless of this constructive criticism, I think this is a wonderful tutorial!  Excellent job!  Thanks Meishe. ;D

Ya, looping commands are really the only thing I ever put them on because I know it won't change them in cases like this.

The typo was just because I typed it all in Notepad first. I thought I got everything but I guess I didn't. Thanks.

Ya, I know it isn't "much" more efficient but it can save space depending on the situation, especially when you need every byte to come off a program.
Title: Re: Jump Code Explanation
Post by: patriotsfan on August 07, 2010, 09:31:39 pm
Nice explanation. I like the way you go about the code step by step which makes it much easier to understand (at least for me). I will certainly read all off it and learn. Thanks. :)
Title: Re: Jump Code Explanation
Post by: meishe91 on August 07, 2010, 09:45:09 pm
Thanks. I have two other explanations like this somewhere in this sub-forum but they are a bit hidden now :P My tutorial thread, located here (http://ourl.ca/4541), has them though if you're interested. It also has links to the other tutorials and guides made by Omnimaga's very own.
Title: Re: Jump Code Explanation
Post by: patriotsfan on August 07, 2010, 09:49:04 pm
Yeah, I've read your other explanations before and they were pretty good. :) Definitely helps people who are trying to master TI-BASIC. :D
Title: Re: Jump Code Explanation
Post by: meishe91 on August 07, 2010, 09:49:45 pm
Thanks :)
Title: Re: Jump Code Explanation
Post by: parserp on October 15, 2011, 12:36:49 am
The tutorial is for TI-Basic, but very applicable to axe. Thanx! ;D
Title: Re: Jump Code Explanation
Post by: meishe91 on October 15, 2011, 02:05:06 pm
No problem.
Title: Re: Jump Code Explanation
Post by: ztrumpet on October 15, 2011, 06:04:33 pm
Meishe, I think you need to revise this slightly.  Here's why:
If you have a DelVar followed by an If/While/Repeat/For command, those commands will glitch if you use additional loops around them.  I think this need to be slightly revised to reflect this.
Title: Re: Jump Code Explanation
Post by: meishe91 on October 16, 2011, 01:54:49 am
Well this is just a example of a concept and shouldn't probably used in an actual program. Since it works in here I'm just gonna keep it but if you wanna change it go for it.
Title: Re: Jump Code Explanation
Post by: stevon8ter on March 23, 2012, 09:29:15 am
Nice explanation.

I'm trying to make a little stupid game.

But I'm just not getting how to do jump+collision detection so I can jump on platforms.
Anybody ideas? PS: It's in the graph screen
Title: Re: Jump Code Explanation
Post by: parserp on March 23, 2012, 04:48:18 pm
Basic? or Axe? do you want realistic gravity or no?
Title: Re: Jump Code Explanation
Post by: meishe91 on April 18, 2012, 02:21:19 am
This is an incredibly late reply, and probably has been answered by now. But there are a couple ways that I can think of off the top of my head. Depending on how you are storing your map data you can check the spots above and below in a check before falling or going up another spot. You could also have your sprites have a constant spot that is either taken or not, depending if it is a "solid" block or not that you check. Same concept though. Anyways...ya.
Title: Re: Jump Code Explanation
Post by: dl on May 03, 2012, 05:51:46 pm
i will try this when i get home
Title: Re: Jump Code Explanation
Post by: tpt1234567890 on October 28, 2013, 11:37:16 pm
should work, meishe91...