Page 11 of 12
Posted: Mon 28 Nov, 2005 9:39 pm
by DarkerLine
pacHa wrote:
This might work, if my basic memories are good (assuming Ans != 21 before this)
Since it's a repeat loop, it doesn't matter if Ans is 21 before it. Not that it matters *points to kalan's post*
If you want to wait for more than one key,
or if you want to know which key was pressed afterwards,
and the keypress will be stored in Ans afterwards.
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 1:10 am
by waeV
I don't know if something like this has been posted, but I used this in my Tron Basic program and it sped up quite a lot:
instead of
Code: Select all
getkey -> g
if g = 24
then
...
end
if g = 25
then
...
end
I did this
Code: Select all
getkey -> g
if g != 0
if g = 24
then
...
end
if g = 25
then
...
end
end
The only problem is that the game slows down when you hold a key, as it goes through all the if statements.
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 3:47 am
by tr1p1ea
Some people optimise their keypress blocks depending on what you need to do. If you only need to increase/decrease an x/y you can just factor in key checks.
Code: Select all
getKey->K
X+(K=26)-(K=24)->X
Y+(K=34)-(K=25)->Y
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 4:55 am
by waeV
Huh, I didn't know you could do that. How would you get it to continue in the direction of the keypress without needing to be held down?
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 5:08 am
by tr1p1ea
Guess you could:
Code: Select all
getKey
If Ans:Ans->K
X+(K=26)-(K=24)->X
Y+(K=34)-(K=25)->Y
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 6:26 am
by waeV
Awesome, that works and keeps the speed fast even when a key is held.
However, I had no idea something like this could be done and still barely understand : does. Where can I read up on this sort of stuff?
I also wanted a variable F that switches between -1 and 1 when 2nd is pressed, an I came up with the following code:
Oh wait, when I do that it keeps putting 21 in as the last pressed key instead of the direction. Hmm...
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 6:39 am
by tr1p1ea
You could try:
Code: Select all
getKey
If Ans and Ans!=21:Ans->K
F*(-2*(Ans=21)+1)->F
X+(K=26)-(K=24)->X
Y+(K=34)-(K=25)->Y
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 6:47 am
by waeV
Well, now it won't accept 2nd as input.
Does the ":" equate to a "then"?
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 8:00 am
by tr1p1ea
: is the character ':', accessed via Alpha+'.' also the " and " is accessed via 2nd+MATH>LOGIC.
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 8:51 am
by kalan_vod
The ":" is just as if you make a new line, but this just helps when you want to group routines in one area. It is not any faster/slower than creating a new line, it really is just a matter of opinion..I love to use it, as it makes things easier to read.
If you are wanting to know more about basic, I would suggest going to
TI|BD.
Re: [TI BASIC] Optimizing
Posted: Sun 14 Dec, 2008 11:35 pm
by waeV
Thanks for the link.
I am confused, though, I am used to the following syntax:
Code: Select all
if ans != 0 and ans !=21
then
ans -> k
end
If : is just the same as a new line, then what how come the following doesn't need a then, an end, or a logic operator for the first comparison?
Re: [TI BASIC] Optimizing
Posted: Mon 15 Dec, 2008 1:15 am
by calc84maniac
"If" only needs a "Then" and "End" if it needs more than one line of code to run if true (colon counts as a newline).
Re: [TI BASIC] Optimizing
Posted: Mon 15 Dec, 2008 1:59 am
by waeV
Well that make for much cleaner code, thanks. I remember you could do something like that in QBASIC but not in Ti-Basic.
So are the following equivalent?
Re: [TI BASIC] Optimizing
Posted: Mon 15 Dec, 2008 2:24 am
by benryves
Non-zero is implicitly true.
Re: [TI BASIC] Optimizing
Posted: Mon 15 Dec, 2008 3:51 am
by waeV
Ah, I get it.
Sweet, this will help a ton.