Monday, November 25, 2013
Simpleton Geek Teaches Computer Programming Book 2 Ch 7C
7.3 Dpad Menu + Button Presses
“Today,” the teacher said gravely, “We are going to learn what many other people have failed to learn properly. A lot of people have tried to do what we will do today, and failed utterly, never to return. In fact, it is no exaggeration that this is The River Test. Beyond the river lies richness beyond your dreams. All you have to do is cross the river. And yet, the river is strong and deadly. Can you cross the river successfully where many have tried and failed? Will you try?”
The students gulped. This is going to be very challenging, indeed. And from the sound of it, extremely dangerous. “What happens if we failed?” asked Nancy.
“The usual fate of failure is death. Many who have failed went on to other ventures, never to return.”
“Then maybe we should skip this and learn something else? Something easier?”
The teacher whacked Nancy's head. “Of course, we'll learn it. What are you talking about? Gya ha ha ha!”
All the other student wisely kept silent. The teacher surveyed the class. They are properly impressed.
Peter raised his hand. “What are we supposed to learn, anyway?”
The teacher smiled. “You're going to learn what to do when the user presses the 'A' button.”
“Whaaat? Is that all? How can something so easy be so difficult?” the students were shocked!
“Simple. Not easy. Two different things.”
“What can be so difficult?”
“If it's that easy to explain, then it wouldn't be difficult, now, is it?” The teacher smiled mysteriously.
The teacher went to the board and wrote this:
@INIT
@MAIN
@END
“Learn this carefully! This is the template in which all your programs will follow from now on. Deviate from this template at your own risk!”
“What happens if we do?” asked Nancy the clueless.
“You'll probably end up with spaghetti code and fail!” came the easy answer.
The teacher points to the board. “There are four main stages. Start of the program, Initialization phase, the Main Loop, and the Ending.
“Start of the program begins from the first line to @INIT. It shows the name of the program, name of the programmer, and date that it is written. Also, variable declarations are there.
“Initialization is where you initialize the variables, read in static data, load graphics, fonts, if need be.
“Main loop is where the main process begins. You will learn to separate different processes and mode. There are many methods possible here, and you will learn them later. We'll take it slow. The most important thing here is to understand how to read inputs. That means, button presses, and Touch Screen.
“Ending segment is easy because you really don't need to do anything here. Exception if you have some background music you need to stop, or clear the graphics you loaded before hand.
“We will learn all of these later. For now, we will learn how input works. That means keyboard, buttons, and touch screen. You have all learned how do read data using INPUT and DATA. The problem is INPUT stops the execution. You need real time input!”
REM INPUT DEMO
@INIT
'READING KEYBOARD
K$=””:KEYBOARD READ
S=0:KEYBOARD SCAN CODE
'READING BUTTONS
BT=0:'BUTTON READ
U=FALSE:'UP DPAD
D=FALSE:'DOWN DPAD
L=FALSE:'LEFT DPAD
R=FALSE:'RIGHT DPAD
A=FALSE:'A BUTTON
B=FALSE:'B BUTTON
X=FALSE:'X BUTTON
Y=FALSE:'Y BUTTON
'READING TOUCHSCREEN
TX=0:'TOUCH SCREEN X
TY=0:'TOUCH SCREEN Y
TS=0:'TOUCH SCREEN STATUS
TT=0:'TOUCH SCREEN TIME
@MAIN
VSYNC 1
K$=INKEY$:S=KEYBOARD
BT=BTRIG()
TX=TCHX:TY=TCHY:TS=TCHST:TT=TCHTIME
IF BT AND 1 THEN U=TRUE ELSE U=FALSE
IF BT AND 2 THEN D=TRUE ELSE D=FALSE
IF BT AND 4 THEN L=TRUE ELSE L=FALSE
IF BT AND 8 THEN R=TRUE ELSE R=FALSE
IF BT AND 16 THEN A=TRUE ELSE A=FALSE
IF BT AND 32 THEN B=TRUE ELSE B=FALSE
IF BT AND 64 THEN X=TRUE ELSE X=FALSE
IF BT AND 128 THEN Y=TRUE ELSE Y=FALSE
CLS
?”INKEY: “;K$
?”SCANCODE: “;S
?
?”U=”;U
?”D=”;D
?”L=”;L
?”R=”;R
?”A=”;A
?”B=”;B
?”X=”;X
?”Y=”;Y
?
?”TX=”;TX
?”TY=”;TY
?”TS=”;TS
?”TT=”;TT
IF X==FALSE GOTO @MAIN
@END
END
“Okay. Type that program and run it. What do you see?” asked the teacher.
After the students run the program, several of them got confused. “Teacher, it doesn't seem to be working!”
“Oh, really? Is there an error somewhere or you're just not getting any response?”
“No response.”
“No response at all or is there something on the screen?”
“TX and TY has some numbers on them.”
“Then the program works as expected. Try moving your stylus around and see what happens to the Touchscreen variables.” said the teacher.
The students tried it, and it works fine. They can see the numbers working as they should. “Now try holding down the key on the keyboard.”
“Hey! The inkey and scancode are flashing! It's showing some number.”
“The inkey should show the character pressed. The scancode is showing the code associated with that keypress. Useful if you want to capture special keys such as backspace, shift, and tab.”
“But teacher! The buttons don't work!”
“The buttons works fine. Push X button and see if the program ends.” Sure enough, the program ends when the student pressed the X button.
The teacher pointed to the screen. “See this X variable turned to 1? This proves that the program works just fine. Now run the program again and while you're pressing this button or that button, press X. Also try it using the DPAD and see if all eight direction is accounted for.”
The students tried it. “No, teacher. It doesn't work!”
The teacher laughed. “See what I mean about how difficult it is? Now do you understand the problem? Even when the program works just fine, it still doesn't show that it works!”
“But it doesn't work!” said Nancy.
The teacher stamped her forehead with so much force, her head flipped backward quite a ways. “Owwww!” said Nancy. “Why did you hit me for?”
“I just said it works, how come you contradict me so easily?” said the teacher.
“But I don't see it!”
“And that's the trouble with you. You see that the X button work. Logically, there's nothing to prevent other buttons from working. So, they work. You just don't see it!”
“Then how can we tell it works?”
“The usual advice is to replace BT=BTRIG() with BT=BUTTON(). In a moment, I will let you do that, but first tell me. What will make you believe that it works without seeing it? “
Nobody make any suggestion. Then Peter said, “How about doing some animation or text?”
“In other words, some long delayed process. You may add the code to see that if BT is not zero, then wait a while. Add this code just before GOTO.”
IF BT>0 THEN WAIT 60
The students tried it. “Oh, we see it now! It works after all!”
“See what I mean about how this simple process is very difficult to get right? You were misled into the wrong thing. Now, delete that WAIT line, and replace BT=BTRIG() with BT=BUTTON().”
The student dutifully tried it. “It works, teacher! BT=BUTTON() really works!”
“Fail! You've just did the wrong thing! See how difficult it is to get it right?”
“But, teacher! It really works! I can see it working!”
“First of all BUTTON() is the wrong way to do it. The correct syntax is BUTTON(0). But that's not the point. The point is, BTRIG() is not the same as BUTTON(0)! BTRIG is the same as BUTTON(1). Therefore, the alternative way to do it, isn't BUTTON(), but BUTTON(1)!”
“But what's the difference?”
“BUTTON(0) will keep set the button states for as long as the button is held down. Button(1) will set the states only for 1/60th of a second. That's why you cannot see it! It's too fast for your eyes to follow!
“Not too fast,” the teacher smiled, “for the computer to follow. So, set a counter at the @INIT, and add this line on the main just below the Touchscreen variables printing.
@INIT
C=0
'PRINT COUNTER
IF A THEN C=C+1
?”COUNTER=”;C
“Now run the program. It will count the number of button presses every time you press A button. See what happens.”
“Teacher! The program counts my presses multiple times! I only press it 3 times, and it counted 26 already!”
“Good. Now try it again with BT=BUTTON(1) and see what happens.”
After the students made the changes, the difference is clear. The program counted the number of presses correctly. A wave of understanding went over the students.
“And that is why reading button presses is so difficult to understand. Not because it is complex, but because it is so easy to go the wrong way. And remember, unless you have the ability to unlearn what you've learned incorrectly, there's no way back. Once you think that BUTTON() holds all the key to understanding how button works, it is very hard to unlearn that knowledge and accept that you misunderstood the process.
“The end result is a lot of frustration and unnecessary grief. This causes the mythical 'Programming is incredible difficult' train of thought that is very hard to get rid of.
“I say this before, and I say this again and again: Computer Programming is very easy to do IF you know what you're doing. So many people don't know what they're doing. So, they give up and never return. Instead of admitting that they're doing it wrong, they rather blame the tools.
“If computer programming can be equated to making cabinets, then these bad workmen blame their tools. That's just silly! If you don't know how a hammer work, it's stupid to blame the hammer. Then why would we accept that the computer is hard to program? It's not hard to program. It's your knowledge that is faulty. BASIC does not cause brain damage. The damage is already there!
“Now that you understand how this work, will you use BUTTON(0) in your program?” asked the teacher.
“No, teacher, we won't” said the students.
“That's too bad, because there are instances where BUTTON(0) will work better. When VSYNC is greater than 1 for example. In fact, why don't you try it now and see if the program still works fine when you do VSYNC 15?”
The students were stunned! The teacher got them yet again!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment