game-developmentdosbasicretro-computing

What is the use of the variable `B%` in DONKEY.BAS written by Bill Gates?


DONKEY.BAS, a video game written in 1981, was developed by Microsoft co-founder Bill Gates and early employee Neil Konzen.

We can experience the game here and view the source code of the game (only 130+ lines).

I can guess most of the code means, except for an integer array variable called B%,

...
1510 DIM B%(300)
1520 FOR I=2 TO 300:B%(I)=-16384+192:NEXT
1530 B%(0)=2:B%(1)=193
...
1760 IF Y AND 3 THEN PUT (140,6),B%
...

It looks like Bill Gates and Neil Konzen filled this array with some magic numbers and drew it as an image at coordinates (140, 6), when the two lowest bits of variable Y (donkey's y-coordinate) are not both 0. 140 is the x-position of dotted line in the middle of the road, I think.

I'm curious about the code for the B% variable, what do those magic numbers mean, and why the binary pattern of the first 2 elements (0 and 193) is different from the next 298 elements?

IF Y AND 3 THEN PUT (140,6),B% seems to make some kind of transformation on the dotted line in the middle of the road, generating animation and making the player feel that the car is moving forward.


Solution

  • I believe I got it but I'm not sure. According to https://qb64.com/wiki/PUT-(graphics-statement),

    The first two indices of the arrays or array offset will hold the width and height of the stored image area.

    This would mean that the stored image area is 2 pixels wide and 193 pixels tall. The height makes sense because it is put at location (140, 6), therefore it covers Y coordinates 6 to 198 (the whole screen is 320 x 200).

    In base-2 notation, 16384 is 0b100000000000000 = 2^14 = 16K.

    192 is 0b11000000.

    I guess -16384+192 was a shortcut to switching on and off some bits they knew caused the right bitmap to be stored in the array. If the negatives worked in a typical overflowing notation on a 64K number space, you get this:

    65536 - 16384 + 192 = 0b1100000011000000

    The trick is due to 16384 being 2^14 and 65536, the integer boundary, eing 2^14, which means that -16384 is like switching on the first two (16-14) bits.

    0b1100000011000000 (0xC0C0) is two 0b11000000 (0xC0) bytes. I guess those 192 (0xC0) bytes represent the solid line in the format used by GET and PUT.