Monday, November 25, 2013

Simpleton Geek Teaches Computer Programming Book2 Ch 8C


8.3 CLS,PRINT,LOCATE,CSRX,CSRY+On screen keyboard
“Does everybody have their design on the journal?” asked the teacher. He asked the student to draw some lines and squares on the 32x24 graph paper.
“Yes, teacher.”
“Good. Now, I'm going to show you this program, and you're going to program it. This here is a good test to see if you can convert from English to Computerese. Or, as any professional will tell you, from Pseudocode to Smile Basic (Petit Computer).”
ASCII ART PAINTER
1. Clear top and bottom screen
2. Set array for text graphic characters
3. Main Loop
1. left/right changes character.
2. Print character on top at (0,0)
3. if touch screen then
1. Print character on X,Y.
“And that's it! Pretty simple, eh? Any question?” the teacher asked.
“What kind of array for text graphic characters?” asked a student.
“You look at the design, then find corresponding characters on the character sheet. Note the number, and store that into an array. Don't forget DIM, and choose your own variable name.”
“What do you mean left/right changes character?”
“You need to have a variable for array index. The value of the variable will vary between 0-max array. Remember that the value of max array is one less than the DIM value. You may use modulus arithmetic to do it. I'll show it to you later.”
“Where do we print the character if touch screen?”
“The touch screen condition is activated when the bottom screen is touched with a stylus. You want to print the character that is indicated on the array, as shown on the top screen, on the stylus location. One more thing: Make sure to include the character 0, so you can erase your drawing.”
As there are no more question, the students all started their programming. When the students reached the button reading code, the teacher showed a little piece of code to cycle the array index.
REM ASCII ART PAINTER
CLS:PNLTYPE “OFF”
CLEAR:DIM T[14]
T[0]=0:T[1]=144
T[2]=145:T[3]=146
T[4]=147:T[5]=148
T[6]=149:T[7]=150
T[8]=151:T[9]=152
T[10]=153:T[11]=154
T[12]=155:T[13]=156
U=0:'U=0-13
@LOOP
VSYNC 1:BT=BTRIG()
TX=TCHX:TY=TCHY:TS=TCHST
IF BT AND 4 THEN U=U-1
IF BT AND 8 THEN U=U+1
U=(U+14)%14:'KEEPING IT IN 0-13
LOCATE 0,0:?CHR$(T[U])
IF TS THEN PNLSTR TX/8,TY/8,CHR$(T[U])
GOTO @LOOP
Ten minutes later, most of the students have finished their programming, and started drawing their design on the computer. Some students, who have opted to get standard size, instead of XL version were struggling. They were not amused when their teacher showed a picture of magnifying glass holder. The teacher just laughed.
“Oh, by the way, in case you're having trouble with the small screen size,” the teacher showed his darkside, “you can easily modify the program to use the DPad for a cursor for the bottom screen. A button to print the character. B button to erase the character. X and Y button to cycle through the character, respectively. In fact, that'll be a good homework for you. Not that I'll be grading it! Gya ha ha ha!”
“Don't we need to show cursor for that?” asked Peter.
“Not really. Just erase the character if you draw it wrong! Although, if you really want to, you can use either BG command or Sprite command to show a cursor.”
“BG command?”
“Short for background. I supposed I should teach you about it one of these days, but not anytime soon! Gya ha ha ha!”
The students were feeling rather unappreciated. All these ungraded homework. Yet, they were resigned to do all of them, since failure to do the homework will result in low test scores. Mind you, their quiz scores were rather dismal even after doing all the homeworks, they dared not to imagine what their scores will be if they skipped doing their homework.
“Everybody got it?” asked the teacher.
“Yes, teacher. How can we save it?”
“You can't. There's no way to read the bottom screen. You need to save it in a separate array for it. I suggest a double dimension array to do it, although you certainly can do it using single dimensional array.”
“Double dimension array? What's that means?”
“Are you kidding me? You don't know what 'double dimension' means?” The teacher was looking around the class. Clueless faces abound. “Okay, you know that when you want to have more of the same variables, you use array? DIM and all that? Well, so far, we've been using single dimensional array. So, imagine that all variables are all in a line.”
The teacher drew a line, and put some numbers on it. Just like an axis.
“Now, a double dimensional array, will have another line.” He drew another axis. In fact, it looks suspiciously like the 32x24 grid representing the character screen.
“So, instead of having a single array index, we're using two array indexes. See this thing here? PNLSTR X,Y,CHR$()? If you have a two dimensional array representing a character screen, you can have it like this:
DIM SCR[32,24]
SCR[TX/8,TY/8]=T[U]
“You need to insert them in the appropriate places, but I'm sure you can do that easy!” Of course, the student were still ignorant of the issue, but with some not so gentle guidance, they were able to finally figure out what to do, and be able to modify the program so that the program will output a series of numbers so that they can copy the numbers into DATA commands for future drawing.
“This is actually the hard way to do things. It would be much simpler once you've learned loops.” said the teacher. “But I want you to suffer some more still! Gya ha ha ha!”
“So, how good are you at reading help file? We're going to skip to the end real quick and make some music! Not REAL music. We're going to do with using BEEP. That's right. Old computer 8-bit music. Now, I want you to write this program displaying the set of characters on top screen, and an ASCII picture of the piano keyboard on the bottom screen.” (Appendix E)
“For the main loop,” the teacher continued, “I want you to check whether or not the user touched the bottom screen, and if so, shows the corresponding character on the top screen. Let me know when you're done.”
Some of the students were having a little difficulty in implementing the program. The teacher encourage student discussions and help. “The best way to learn things is to teach things, so why don't you help your fellow students? By the way, here's the pseudocode I'm looking for:”
@LOOP
1. READ TCHX,TCHY,TCHTIME
2. CHKCHR(CURSOR LOCATION)
3. PNLSTR 0,15,CHR$
4. GOTO @LOOP
After some trial and error, all the students finally got it. “Great! Now I want to to open up the help menu and see the help page number 43, specifically the BEEP command. First, you notice that there is waveform number (0-69) and pitch. Scroll down to the bottom of the page and there is an instruction on how to do the pitch. Specifically, how P=4096/12. Also notice that there is a table called 'Changes to Musical Interval'. I think we all agree that it doesn't take a genius to realize that the letters are pretty much self explanatory. Notice that I put in the letter H at the end. That's equivalent to P*12, by the way. So, now, what we need to do is to create some look up table.
C=0 c=1 D=2 d=3
E=4 F=5 f=6 G=7
g=8 A=9 a=10 B=11
H=12
“Fortunately, when we do CLEAR command, all the variables are set to zero. So, when we do DIM notes later on, all the variables are still zero and all we need to do is implement the values above. I suggest that you create an array with 256 elements, and for each corresponding value in the table above, set the values in the character code entry. You know how to determine the character code for a character, right? Use ASC command! Here's the pseudocode that I want you to use:”
DIM N[256]
SET N[] ENTRIES PER ABOVE * P
@LOOP
1. READ TCHX,TCHY,TCHTIME,BTRIG
2. IF TCHTIME==1 THEN
1. A=CHKCHR(CURSOR LOCATION)
2. IF A!=0 THEN BEEP WAVEFORM,N[A]
3. SET WAVEFORM ACCORDING TO L/R DPAD
4. GOTO @LOOP
“Of course, I do not expect you to start from scratch! I expect you to modify the existing program! Now get to it! First find out what the character code values are using ASC command, then fill in the details.”
The students are busy typing '?ASC(“C”)' and other characters. After finishing the table, then they began to modify their program. All in all, it only took them about 10 minutes to do. Soon, the class is filled with electronic music!
“Now, does anybody know how to do karaoke?” asked the teacher. Of course, no one does. The students all stared blankly at the teacher. “Come on, now, show and tell time! What does a karaoke system entails?”
“Well, there's music.” said a student.
“Which is note and tempo. What else?”
“Words?”
“Correct, so we have note, tempo and words. Would this covers it?”
1. Note
2. Wait time (tempo)
3. Text
“So, what I want you to do next is to create DATA statements, consisting of those 3 different elements. We can just use the same program and simply write it after the original data. Press 'A' for automatic music! How's that?”
The students are all getting excited! Soon, they were getting busy. It's rather interesting how little it takes to write a worth while program. Animated text! Who would have thought of it?
IF BT AND 4 THEN DELAY=DELAY-1
IF BT AND 8 THEN DELAY=DELAY+1
DELAY=(DELAY+60)%60

@AUTOPLAY
READ NT$,WT,XT$
IF XT$==”END” GOTO @MAIN
BEEP W,N[ASC(NT$)]
PNLSTR 0,17,XT$
VSYNC DELAY*WT
GOTO @AUTOPLAY
Of course, there is more than one way to do it. This particular implementation, Appendix F, relies on the fact that notes are given in string. However, it can easily be done that the notes are given in multiple P blocks.
The data is set in @MUSIC DATA segment, and it does take a long time to do, even with copy and paste. The tempo is rather rough because it's very difficult, unless one is reading musical notation from song book. The text is currently limited to just one line, but there is no reason why the data cannot include text cursor positioning as well.
It does illustrate the point that computer programming, coding the program, doesn't take a lot of time. Once the design is decided, there is nothing to stop the computer programmer to just code the program quickly and easily. It is much more difficult and time consuming to write the data than it is writing the program itself.
The students were really having fun. In fact, some students decided to form an impromptu band. Some playing the beep, others playing drums. Some cat's meow can be heard as well.
The teacher simply leaned back on his chair. He enjoyed the situation. Of course, it would have been better with ear phone, but there's nothing inherently bad about making music with the computer, especially using the computer program the students wrote themselves. It is very encouraging to have the students write the tools as opposed to just give them the completed tools, a mysterious black box if you will, that is all-powerful but with all the features hidden.
“By the way, guys, you know that the sound can have up to 7 sounds? You can have polyphonic. Simply add an extra note, and READ statement, and there you have it!”

No comments:

Post a Comment