Tiny Basic can be a great way to learn how to program. It is primitive enough to be easy to learn, yet powerful enough to complex things in a small amount of memory. For today’s example, I’m going to show you how to create the random dot pattern I love to show off.
As the resolution on the Half-Byte Computer Console is really low, heck, it is barely there, it does not take much to make a compelling demo. We have 80 by 48 dots to play with. Not much, but more than some very early home computers had. But, today, we are only going to use 80 by 46. (Only because I typed the wrong value into the Y=RND statement and was too lazy to fix it.)
Our demo creates four random numbers, one set to turn on the dots and one set to turn them off. We will use the x, y, p and q variables for the pixels. A fifth number will be generated, using the variable u, and we will discuss that later.
Our demo will create a four quadrant screen to light up the pixels. This will give you a kaleidoscope effect. So, that means we have to restrict our work to just one quarter of the screen and then mirror it to the other three. Sounds complicated, but it isn’t.
Have a look at the code below:
110 X=RND(40): Y=RND(23)
120 P=RND(40): Q=RND(23)
130 SET X,Y
140 RESET P,Q
150 SET X,23-Y
160 RESET P, 23-Q
170 SET 40-X,Y
180 RESET 40-P, Q
190 SET 40-X,23-Y
200 RESET 40-P,23-Q
290 GOTO 110
Lines 110 and 120 generate our random numbers, one pixel, to light up and one pixel to turn off. Lines 130 through 200 calculate the four quadrant locations to turn on or off. SET will turn the pixel on, RESET will turn them off. We figure out where to turn them on or off by using offsets. In our case, the offsets are the width (0 to 40) and height (0 to 23). Study what these lines are doing and you can figure out where on the screen each dot will go. Take a sheet of graph paper and make an 80 by 46 grid. Use RANDOM.ORG to generate 2 random numbers, one will be from 0 to 40 and the other will be from 0 to 23. Use the SET statements and figure out which cells on your graph paper to color in and then do so. After you color in the four squares, you can see just how the code works. Do that a few more times and you get an interesting design. Now, key the code into Tiny Basic 2 and run it. Let it go for a while and you’ll see very interesting patterns emerge. After a while, though, the screen gets busy and ceases to be interesting. So, we need to do something about it.
We need to clear the screen every now and then and let the patterns regenerate.
So, how do we do this? Well, you can it do it a number of ways…poll the keyboard for a keypress, read the Wii Nunchuck, etc. The easy way, though, is to just do it randomly. Good thing we have line 210. Line 210 has already created a random number, we just have not done anything with the number. So…lets do something.
220 IF U=93 GOTO 100
That’s all we need to do. Evaluate the value of U and, if it matches our magic number (which can be anything from 0 to 99 as dictated by line 210. You can put 10000 in there if you like. The random number limit and the number after the equal sign are entirely arbitrary. But, the higher the numbers, the longer it could take to hit that random number. It doesn’t matter. For our demo, I chose 99 and 93. Go ahead, break the program if it is still running and then type in line 220 above. Re-run the program. You will see it switch patterns frequently.
Pretty cool, huh?
Play with the numbers a little. You can limit the pattern to just one quadrant. Change the x and y values in 110 to 20 and 12 (you will need to change line 120 as well.) Then, in the code, everywhere you see 40, change it 20 and change all of the 23 to 12. Run the demo. You should see the same thing as before, only smaller and in one corner of the screen. Experiment with this, what do you have to do to put it in, say, the lower right of the screen? Hint: you will need to offset your set and reset locations. Look at lines 190 and 200 for a clue. Post your solution in the comments.
I have posted a video here.
In an upcoming example, we will use the Wii Nunchuck to control the drawing.