![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Home | ||||||||
Pascal Home | ||||||||
Pascal Tutorial Part II - Codes | ||||||||
If you do not understand or did not go through Tutorial I, you need to click here. ASCII For those of you who are advanced programmers, go ahead and scroll on down to where it says "Using ASCII in Pascal" ASCII are character codes, 0 to 255, or in other words 256 characters, including every key on your keyboard, that includes SHIFT, ENTER, TAB, and the others, plus special characters - line codes, greek, latin and icelandic letters, and others. These are all accessed by most programming languages with a number called the ASCII Code. So in order to use these characters, you have to know all these numbers. To find out about some of these, you can use the function ord. We can use a byte value, which holds up to 256, so it can hold the ASCII code of any character. Here's out program, where b is a byte: uses crt; var b : byte c : char; begin c:=readkey; b:=ord(c); writeln('ASCII Value: ', b); end. Remember to go to the user screen when you're done. You know, of course, that you cannot detect if the user pressed ENTER this way: if c = ' ' then That won't do at all! You need to check the ASCII code of ENTER, which is 13. if c = #13 then... so here's how you display any character: with its number. Using ASCII in Pascal To use ASCII in Pascal, you simply put an octothorp, or "#" in front of it. So a character with ASCII code 33 would be displayed without quotes with a "#33." Here: writeln(#33); ENTER is #13, Backspace is #8, Tab is #9, ESC is #27, Space is #32, and the Up arrow is #72 and the Down arrow is #80 and the right arrow is #77 and a left arrow is #75. These are the same: if ord(c) = 10; and: if c = #10; That's ASCII! Colors - even more codes?! Well, yes, even more codes! How would one make colors? Pascal has colors? Yes. Many of you probably know that DOS has 16 colors - Pro's might even have them memorized! Here's a list: 00 - Dark Black 01 - Dark Blue 02 - Dark Green 03 - Dark Cyan 04 - Dark Red 05 - Dark Purple 06 - Dark Brown 07 - Grey-white(Light Grey) 08 - Dark Grey 09 - Light Blue 10 - Light Green 11 - Light Cyan 12 - Light Red 13 - Light Magenta 14 - Light Yellow 15 - Bright White Notice that 00 and 08, 01 and 09, 02 and 10, 03 and 11, all the way to 07 and 15, are related in that the higher value is the "Lighter" shade of the same color that the lower values are the darker versions of. Now how would we use these in Pascal? Well, let's review a bit. The Background is always one of the first 00-08 colors. Setting it to a 09 or more just sets it to its darker counterpart - the lower of that color, or that color value minus eight. Foreground - Text Color, etc, can be set to any of the 16 colors. Here's how: Textcolor(fgcolor); TextBackgroung(bgcolor); Or you can put them together in a special variable: TextAttr:=(fgcolor * 16) + bgcolor; My favorite color combination is Blue BackGround - BG - and Green Foreground - FG. So here's how: TextAttr:=(10 * 16) + 1; {Which is, in other words, 160 * 1 or 160.} And that's how you do color in QBASIC, Pascal, and most DOS languages! Of course, there's 256 colors, which you'll work with later, but for now, that's your colors in Pascal! But how do you do something like this?: h e l l o wo r l d How do you put text anywhere on the screen? If you're a QB Person, you use LOCATE. Well, in Pascal, you use the GotoXY statement (which is in crt!): GotoXY(10, 25); Well, now that you understand ASCII and GotoXY, you can learn a new statement. The Write statement is the same as Writeln, except Writeln inserts an ASCII character 13 (ENTER) at the end of the line so the next write/writeln operation is on the next line. If you use "write" though, you'll be able to do it again and it'll be on the same line, unless you use a GotoXY statement, although who knows why you would want to instead of just using the "Writeln" statement. So: Write('Hello, '); Writeln('World!'); Writeln('Hello, '); Writeln('World!'); Produces an output of: Hello, World! Hello, World! understand? You can also change the text size with the "TextMode" statement if you're using crt. Here's how. Your screen is divided into columns. So if you change the ammount of columns, you have to make the text bigger or smaller: uses crt; begin TextAttr:=26; clrscr; write('Normal'); TextMode(CO40); writeln('BIG'); Lastmode; writeln('Normal'); end. Normally, your Textmode is 80 Columns. This program reduces it to 40, making the characters twice as big. Lastmode returns it to its previous mode. You could, however, just do "TextMode(CO80);" and get the same result. But you know what? All this text stuff is driving me nuts. I'm bored insane. Let's do something new. Sound! Octave 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Note | | G# 52 | 104| 208| 415 | 831| 1661|3322| | G 49 | 98 | 196| 392| 784| 1568|3136| | F# 46| 92 | 185| 370| 740| 1480|2960| | F 44| 87 | 175| 349| 698| 1397|2794| | E 41| 82| 165| 330| 659| 1349|2637| | D# 39| 78| 156| 311| 622| 1245|2484| | D 37| 73| 147| 294| 587| 1175|2341| | C# 35| 69| 139| 277| 554| 1109|2217| | C 33| 65| 131| 262| 523| 1017|2093|4186| B 31| 62| 123| 247| 494| 988 |1976|3951 | A# 29| 58| 117| 233| 466| 982 |1865|3729 | A 28| 55| 110| 220| 440| 980 |1760|3320 | This is our table of Approximate Hz values for piano notes. But how would one play them in Pascal. QB People know we can play Hz values with the "SOUND" statement. Here's how: Sound(44);Delay(100);NoSound; That's an A octave 5 for 100 Milliseconds. That's it for sound. This tutorial is not finished yet. Click here to see the finished Tutorial #1 if you have not yet. To send suggestions in, click here to e-mail qbspot@zwallet.com |
||||||||
Home | ||||||||
Pascal Home |