The Commodore 64
From Maze
(Difference between revisions)
(Initial creation copy/pasting from the talk page for diff convenience later) | (modified RUN to serve as an intro for the chapter) | ||
| Line 1: | Line 1: | ||
| + | == == | ||
Imagine it's 1982 and you've just sat down in front of your Commodore 64. As you stare at the television set attached to this small computer-in-a-keyboard object, you see the word "READY" and a blinking cursor. Earlier in the day a "neat little program"[1] in the Random Graphics section of the manual caught your eye: | Imagine it's 1982 and you've just sat down in front of your Commodore 64. As you stare at the television set attached to this small computer-in-a-keyboard object, you see the word "READY" and a blinking cursor. Earlier in the day a "neat little program"[1] in the Random Graphics section of the manual caught your eye: | ||
| Line 9: | Line 10: | ||
[INSERT TWO FIGURES - Preferably photos of the program running on an actual C64, since we have screenshots of it all over the place before now] | [INSERT TWO FIGURES - Preferably photos of the program running on an actual C64, since we have screenshots of it all over the place before now] | ||
| − | The three lines of code that create this continually generating maze do so because of the specific design of the C64, paired with the design of the BASIC language in which the program is written. The manual explains the code element by element, first pointing out the importance of the number 205.5. When this number is summed with RND(1), the result is a value between 205.5 and 206.5 because RND(1) calculates a value between 0 and 1. By design, the CHR$ function ignores fractional values, so numbers from 205.5 to 205.999 become 205 and numbers between 206 and 206.5 become 206. The program draws the maze because 205 is the C64 code for “\”, 206 is the code for “/” and because CHR$ draws those graphics to the screen. | + | The three lines of code that create this continually generating maze do so because of the specific design of the C64, paired with the design of the BASIC language in which the program is written. The manual explains the code element by element, first introducing the idea of a character code and then pointing out the importance of the number 205.5 specifically. The explanation of how the code works is followed by something even more valuable: an invitation to modify the code, change it, and make it your own based on what you've read. |
| − | We can test this by checking Appendix F in the manual (“ASCII and CHR$ Codes”) or inputting a short one-line program to print the character corresponding to 205 to the console: | + | ==Learning Through Play== |
| − | PRINT CHR$(205) | + | The manual not only gets you started with the basics, it begins to point out specific ways that you can tweak the program to change how it functions. It begins with the value 205.5. When this number is summed with RND(1), which chooses a decimal between 0 and 1, the result is a value between 205.5 and 206.5. By design, the CHR$ function ignores fractional values, so numbers from 205.5 to 205.999 become 205 and numbers between 206 and 206.5 become 206. The program draws the maze because 205 is the C64 code for “\”, 206 is the code for “/” and because CHR$ draws those graphics to the screen. |
| − | This places “\” on screen. We can test the other value writes “/”by inputting this: | + | Since the chance of having a value between 205.5 and 206 is roughly equal to the chance of having a value between 206 and 206.5 the maze contains roughly an even distribution of these two graphics, giving it a balanced look. The manual suggests, “If you'd like to experiment with this program, try changing 205.5 by adding or subtracting a couple tenths from it. This will give either character a greater chance of being selected.” This suggestion increases the chances for one character to be selected over the other. Changing 205.5 to 205.1 gives the value 205 a 90% chance of drawing, which strongly affects the visual quality of the maze: |
| − | |||
| − | PRINT CHR$(206) | ||
| − | |||
| − | Now that we've established the transcoded identity of 205 and 206, we can focus on the fifty-fifty chance that each has to draw to the screen. Over a long period of time, the maze has roughly an even amount of these two graphics to give it balance. The manual suggests, “If you'd like to experiment with this program, try changing 205.5 by adding or subtracting a couple tenths from it. This will give either character a greater chance of being selected.” This suggestion increases the chances for one character to be selected over the other. Changing 205.5 to 205.1 gives the value 205 a 90% chance of drawing, which strongly affects the visual quality of the maze: | ||
[INSERT FIGURE] | [INSERT FIGURE] | ||
| Line 27: | Line 24: | ||
[INSERT FIGURE] | [INSERT FIGURE] | ||
| − | The code continually draws characters to the screen until the program is stopped because of line 40. Each time that line of code runs, the program jumps back to line 20 as defined by the GOTO command and makes the calculation to draw either the “\” or “/” graphic. This continuous loop defines the generative nature of the maze. | + | Great, you have not only learned to program a maze, but you have also learned how to modify it by controlling the ratio between the "/" and "\" characters that form it. But what exactly did you change? There is no code that explicitly sets the probability of either character appearing on the screen. You never even tell the C64 that it should round off the fractional 205.x and 206.x values to whole numbers. You achieved the effect of changing the character's frequency indirectly by taking advantage of the programming environment of BASIC and the inherent properties of the Commodore 64 platform. |
| − | The dimensions and density of the maze are defined by the C64 specifications and the attached television. The screen is divided into 40 units wide and 25 high; each unit is a 8 x 8 pixel grid. The large border that surrounds the maze is required by the wide variation within cathode ray tube (CRT) televisions of the era. The CRT screen of different televisions framed the pixels differently, making only a subset of pixels in the center reliable for display. The 32 pixels borders on the left and right and 35 pixel borders on the top and bottom reduce the usable display to 320 wide by 200 pixels high. | + | ==Hidden Code== |
| − | |||
| − | The three-line program introduced here is nearly equivalent to the one-line namesake of this book: | ||
| − | |||
| − | 10 PRINT CHR$(205.5 + RND(1)); : GOTO 10 | ||
| − | |||
| − | The only difference is the condensed version doesn't clear the screen as in the first line of the expanded code. The first line of the three-line program clears the screen and places the first character in the upper left. The one-line program starts placing characters at the current location on screen and gradually pushes the other characters up as the maze draws: | ||
| − | |||
| − | [INSERT TWO FIGURES] | ||
| − | |||
| − | '''Hidden Code''' | ||
(Note: This section does not get into areas that I believe are going to be covered in greater depth elsewhere in the book. The biggest glaring omission is in character sets/graphics. It is intended as a high-level gloss of some of the features of the environment that impact 10-Print's output; depending on the trajectory of the book, it might serve as an introduction to other sections or be fleshed out more itself. I'd like to take it in the direction of how other environments would change the output/require different code, but I'll see how it fits with other sections.) | (Note: This section does not get into areas that I believe are going to be covered in greater depth elsewhere in the book. The biggest glaring omission is in character sets/graphics. It is intended as a high-level gloss of some of the features of the environment that impact 10-Print's output; depending on the trajectory of the book, it might serve as an introduction to other sections or be fleshed out more itself. I'd like to take it in the direction of how other environments would change the output/require different code, but I'll see how it fits with other sections.) | ||
