Flappy Bit: writing a Tiny Basic game that uses a light sensor and you as the game controller

FlappyBitPhotoHere’s a very rough, very crude game, in HalfByte Tiny Basic, of Flappy Bird. I call it Flappy Bit. You use the Light Sensor to control your bit. Cover the sensor and your bit moves up, shine light on it and you go down. As I have not built much logic into it, weird things happen, like sometimes the pipes blend together and you will hit it, sometimes, you go through the pipe, etc. It only uses about 400 or bytes, so there is plenty of room to play with.

Game Features:

  • Use your hand as the controller!
  • Ultra realistic blocky graphics!
  • Stunning sound!
  • Full Color Black and White!
  • True to physics!

100 CLS
110 X=1:Y=20
120 P=50:Q=0
130 L=RND(20):M=RND(20)
135 W=-1
140 A=0:Z=0
150 S=0
160 D=1
170 C=0
200 LINE P,0,P,L,1
210 LINE P,44,P,44-M,1
220 SET X,Y
225 DELAY 50
230 Z=AREAD(A)
240 IF Z<300 K=-1
250 IF Z>299 K=1
260 IF GET(X,Y+K)=1 GOTO 500
270 RESET X,Y
280 X=X+D
290 IF X>75 X=1:D=1
300 LINE P,44,P,44-M,0
310 LINE P,0,P,L,0
320 P=P+W
330 IF P<1 P=RND(75):M=RND(20):L=RND(20)
340 IF M<15 M=M+10
345 IF L<15 L=L+10
350 Y=Y+K
360 IF Y>40 Y=40
370 IF Y<1 Y=1
380 DELAY 50
385 IF X=P+1 C=C+1:TONE 2000,200
386 CURSOR 0,6:?C;
390 GOTO 200
500 CLS
510 ?”You hit the pipe!”
520 DELAY 5000
590 GOTO 100

Advertisements

Flappy Bit: writing a Tiny Basic game that uses a light sensor and you as the game controller

FlappyBitPhotoHere’s a very rough, very crude game, in HalfByte Tiny Basic, of Flappy Bird. I call it Flappy Bit. You use the Light Sensor to control your bit. Cover the sensor and your bit moves up, shine light on it and you go down. As I have not built much logic into it, weird things happen, like sometimes the pipes blend together and you will hit it, sometimes, you go through the pipe, etc. It only uses about 400 or bytes, so there is plenty of room to play with.

Game Features:

  • Use your hand as the controller!
  • Ultra realistic blocky graphics!
  • Stunning sound!
  • Full Color Black and White!
  • True to physics!

100 CLS
110 X=1:Y=20
120 P=50:Q=0
130 L=RND(20):M=RND(20)
135 W=-1
140 A=0:Z=0
150 S=0
160 D=1
170 C=0
200 LINE P,0,P,L,1
210 LINE P,44,P,44-M,1
220 SET X,Y
225 DELAY 250
230 Z=AREAD(A)
240 IF Z<300 K=-1
250 IF Z>299 K=1
260 IF GET(X,Y+K)=1 GOTO 500
270 RESET X,Y
280 X=X+D
290 IF X>75 X=1:D=1
300 LINE P,44,P,44-M,0
310 LINE P,0,P,L,0
320 P=P+W
330 IF P<1 P=RND(75):M=RND(20):L=RND(20)
340 IF M<15 M=M+10
345 IF L<15 L=L+10
350 Y=Y+K
360 IF Y>40 Y=40
370 IF Y<1 Y=1
380 DELAY 100
385 IF X=P+1 C=C+1:TONE 2000,200
386 CURSOR 0,6:?C;
390 GOTO 200
500 CLS
510 ?”You hit the pipe!”
520 DELAY 5000
590 GOTO 100

Half-Byte Tiny Basic 2: code examples, random dots

Trandomdotsiny 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:

100 CLS
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,43-Y
160 RESET P, 43-Q
170 SET 79-X,Y
180 RESET 79-P, Q
190 SET 79-X,43-Y
200 RESET 79-P,43-Q
210 U=RND(99)
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.