KermMartian wrote:
The correct and most compact form of that incorrect code you posted would look like this (your code is not the right way to use Then and Else:
Code:
Code:

randInt(0,1)->A
If A=0
"You will eat potatoes"->Str1
If A=1
"You will code a basic program"->Str1
Disp Str1



Wink Sorry, just wanted to make it even smaller... Smile


Code:
"You will eat potatoes
If randInt(0,1:"You will code a basic program
Disp Ans
MateoConLechuga wrote:
Wink Sorry, just wanted to make it even smaller... Smile


Code:
"You will eat potatoes
If randInt(0,1:"You will code a basic program
Disp Ans
Premature optimization can be very confusing for beginners; I try not to get carried away until the student has the fundamentals down. The format I provided will allow him to easily extend it for more than 2 fortunes, then he can learn to optimize it later. Smile
How do I get user input? I mean how do I sort of make a menu with something other than the menu command. Or maybe how do I make it so if you press a key something else is displayed. I want the calculator to look for if someone presses a certain key and if they do press the calculator displays something else.
DWMelon wrote:
How do I get user input? I mean how do I sort of make a menu with something other than the menu command. Or maybe how do I make it so if you press a key something else is displayed. I want the calculator to look for if someone presses a certain key and if they do press the calculator displays something else.
The command getKey (PRGM > I/O > 7) will return a number corresponding to the currently pressed key, or 0 if no key is pressed. There's a lot of neat stuff you can do with it, but just like almost everything in TI-OS, it can't detect more than one key at once. Some common usages:

Check if CLEAR is pressed:

Code:
getKey→K
If K=45
// Do stuff here

Wait until ENTER is pressed:

Code:
Repeat K=105
getKey→K
End

These examples could be optimized a bit further by using the value of getKey directly instead of storing it to K. However, it's usually useful to store it somewhere when writing a proper program since you may want to check the value several times for different keys.
Thanks! I have another question, this kind of relates back to when I was making a movement game. I'm trying to make a sprite move by the arrow keys. The code looks a little bit like what someone else showed me before, but here it is:
ClrHome
5->A
5->B
Lbl 1
Output(A,B,"0")
getKey->K
If K=26
B+1->B
If K=24
B-1->B
If K=25
A-1->A
If K=34
A+1->A
Goto 1


This code sort of works except 0 sprite stays behind after I move it so soon there will be a pile of 0's on the screen when I'm only controlling one. I tried putting a ClrHome in but that made it blink, which is something I wish it wouldn't do. What can I do so that my "0" sprite doesn't blink, and it doesn't leave behind a trail of other 0's?
Let's see. Here's something else you can do, but keep in mind there's a speed limit to the calc and you can't make it perfect.

ClrHome
5->A
5->B
Lbl 1
Output(A,B,"0")
getKey->K
If K=26
Output(A,B," "
B+1->B
If K=24
Output(A,B," "
B-1->B
If K=25
Output(A,B," "
A-1->A
If K=34
Output(A,B," "
A+1->A
Goto 1

Also, two things to optimise your program:
You don't need to close parenthesis in most occasions, and if you want the loop to run forever you can simply make a "While 0=0" loop.

No clue if this will stop the blinking, though, but it's worth a try!
Thanks for trying to help, but all it did was make the program unresponsive ("0" didn't move at all). I'll try the while loop and will get rid of parenthesis though. I know you can eliminate blinking though or at least so much I can't notice, as I've seen it before in other games.
OOOH! I've got an idea, but it might only work if you're moving it to a horizontal direction.
This will require a bit more space, but if it moves left, make it output "0 ", and if it moves right, make it output " 0" the space to the left of it. As far as vertical goes, I'm not sure.
What do you mean by that? I'm confused... could you show me in code?
Asian wrote:
if you want the loop to run forever you can simply make a "While 0=0" loop.

"While 1" works as well.
ClrHome
5->A
5->B
While 1
If K=26
Output(A,B," 0")
If K=24
Output(A,B,"0 ")
If K=25 or K=34
Output(A,B,"0")
K=0
getKey->K
If K=26
B+1->B
If K=24
B-1->B
If K=25
A-1->A
If K=34
A+1->A
End

Also added a line of code to reduce blinking
Okay, time for some code:


Code:
ClrHome
4->A
5->B
34->K
While K!=45
If K:Then
 Output(A,B," "
 B+(K=26)-(K=24->B
 A+(K=34)-(K=25->A
 Output(A,B,"O
End
getKey->K
End


A bit of advice: Try to never use a Goto-Lbl loop unless absolutely necessary. Now, how does this code work? Take a look at (K=34) for example. As 34 is the Key code for UP, this will return 1 if up is pressed, and 0 if not. This is then added to the offset. Notice that it will also only update if a key is pressed. Test out this code and see what you can learn from it. Smile
MateoConLechuga wrote:
Okay, time for some code:


Code:
ClrHome
4->A
5->B
34->K
While K!=45
If K:Then
 Output(A,B," "
 B+(K=26)-(K=24->B
 A+(K=34)-(K-25->A
 Output(A,B,"O
End
getKey->K
End


A bit of advice: Try to never use a Goto-Lbl loop unless absolutely necessary. Now, how does this code work? Take a look at (K=34) for example. As 34 is the Key code for UP, this will return 1 if up is pressed, and 0 if not. This is then added to the offeset. Notice that it will also only update if a key is pressed. Test out this code and see what you can learn from it. Smile

Mateo... maybe wait a bit until he gets how to do it the basic way first -_-
Of course; that is why that code is there in case there is an opportunity to take it a step further. Smile
So... would my code be "the basic way"?
I found Asian's version of code left a trail just like before and Mateo's gave me a domain error. When I went to the goto option it said the error was on the line 4 from the bottom. I had this problem before with something. Does anyone maybe know the fix? BTW thanks so much everyone for helping me.
Asian, there is an error in your code:
Asian wrote:

Code:

K=0
getKey->K
I believe you meant 0->K there, but even so, that's unnecessary, as getKey will store 0 to K if no key is being pressed.
DWMelon wrote:
I found Asian's version of code left a trail just like before and Mateo's gave me a domain error. When I went to the goto option it said the error was on the line 4 from the bottom. I had this problem before with something. Does anyone maybe know the fix? BTW thanks so much everyone for helping me.


Yes, sorry about that. It is because that minus sign should be an "=" That's what I get for not using sourcecoder. Smile
It still leaves a trail behind. Maybe I do just have to deal with the blinking... I know I've seen it not blink before on basic games before.
The way to reduce blinking (as 123outerme puts it) is to erase the previous thingy right after drawing the new thingy.
lemme see...

Code:

:While 1
:Output(X,Y,"O
:Output(Q,W," "
:Repeat K=24 or K=25 or K=26 or K=34
:getKey->K
:End
:If K=24
:Then
:X->Q
:X-1->X
:End
:If K=25
:Then
:Y->W
:Y+1->Y
:End
:If K=26
:Then
:X->Q
:X+1->X
:End
:If K=34
:Then
:Y->W
:Y-1->Y
:End
:End

I don't know the dimensions of the screen because I forgot Razz But cursor restriction should be just a matter of an "if"
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement