Sunday, July 7, 2013

Tutorial #4 - Getting Input

To do anything fun with your program you are going to need to receive input from the user. When your program receives input it can create changes that the user controls and thus makes your program infinitely more useful/fun. Input can be handled a couple of ways in Petit Computer. You can either receive text input from the onscreen keyboard/console or via button pressing. They are both straight forward and fairly easy to understand. Lets take a look at this sample program.

  1. INPUT "What is your name: ";NAME$ ' This asks the user for their name, and stores it in NAME$
  2. PRINT "Hello ";NAME$;"! Nice to meet you."
  3. INPUT "How old are are you?";AGE 'This will only accept a numeric answer, and store it in AGE
  4. IF AGE <= 12 THEN PRINT "Your parents must be proud of you."
  5. 'Okay, this will simply check if the variable AGE is Less Than < or equal to =
  6. 'THEN do a command if it is.
  7. GOTO @ENDPROGRAM 'We can skip ahead now
  8. IF AGE >= 13 OR AGE < 18 THEN PRINT "How are your studies?" ELSE PRINT "What are your plans?"
  9. 'This checks if your age is greater than or equal to 13, OR less than 18 and if its true print one thing, or do something ELSE.
  10. @ENDPROGRAM
  11. PRINT "Press the A key to quit."
  12. @LOOP
  13. IF BUTTON(0) == 16 THEN END
  14. 'This checks if a button (A Key = 16) was pressed. End the program if it was.
  15. GOTO @LOOP 'Keep doing this until it is pressed.
I hope this made some sense to you. If you play around with the code it shouldn't take long to understand.
As always if you have any questions or comments feel free to post them.

New Commands Used

INPUT - Obtain numbers or Strings
This will receive input from the onscreen keyboard and store it in one or multiple variables.
  • INPUT "string";received variable
  • INPUT "string";received variable$
  • INPUT "string";received variable, received variable2$
BUTTON() - This returns data from each button pressed.
This is how we get input from the DPAD, ABXY, LR, and Start buttons.
  • Variable = BUTTON(type)
Please consult the help in Petit Computer or the manual for a table of button types and return values. Under "Console Entry"

IF ~ THEN ~ ELSE - Conditional judgement.
Compare variables or numbers and execute a command based on the result. Must be on one line.
  • IF condition THEN command
  • IF condition THEN @label
  • IF condition THEN command ELSE command
  • IF condition THE @label ELSE @label

QR Code for this Tutorial


No comments:

Post a Comment