18

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 (2 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.

4
  • 1
    Not an answer, just writing down the bit patterns: [0] = 0000 0000 0000 0010 [1] = 0000 0000 1100 0001 [2] = 1100 0000 1100 0000
    – dave
    Commented Oct 18 at 3:36
  • 3
    I hadn't realized Donkey was written by Bill Gates. My first exposure to that game was in FuturePod at Ontario Place in Canada; the machine running it didn't have a publicly-accessible keyboard, but instead discrete buttons (which may have been wired to keyboard keys, or joystick inputs; I think the game was somewhat modified from the original in any case to interact with other menus better).
    – supercat
    Commented Oct 18 at 16:35
  • 6
    @supercat And to think that if he had followed up with it he could have made a fortune. I bet he's kicking himself now at the way his luck went. Commented Oct 18 at 19:54
  • The performance of GET/PUT in BASICA may not be quite as good as what could be achieved with hand-optimized machine code, but it's good enough to make a game like Donkey work without needing any assembly language "helper" routines.
    – supercat
    Commented Oct 18 at 20:36

2 Answers 2

18

If you look up the PUT command for BASIC, the format is explained in the GET command:

  • Byte 0,1: Number of x pixels, unsigned int. In SCREEN 1, this value is doubled.
  • Byte 2,3: Number of y pixels, unsigned int.
  • Byte 4 onwards: Pixel data. Data is arranged in 2-byte words. The first 16-bit word holds the bit 0 of the first 16 pixels on the top row. The second word holds the second bit, etc. Data is word-aligned at the end of each row. Thus, in a screen mode with 4 bits per pixel, the first row takes at least 8 bytes (4 words), even if it consists of only one pixel. The number of bits per pixel depends on the SCREEN mode.

So the first two elements are different because they contain the size of the rectangle that is displayed.

The rest is constant, so it's a repeating pattern (stripes) where some of the 16 bits are turned on, and some are turned off.

2
  • 3
    Yeah, note that the constant -16384+192 is 1100000011000000 in binary.
    – dan04
    Commented Oct 18 at 16:12
  • But if the dimensions are 2 by 193, and the data also includes those two values, shouldn't there be 388 2-byte values, not just 300? Commented Oct 19 at 20:38
3

B% is a solid light grey line. This line

IF Y AND 3 THEN PUT (140,6),B%

uses XOR copy to invert the current dashed line repeatedly which gives the illusion of movement. I assume that XOR is the default behaviour, as

https://robhagemans.github.io/pcbasic/doc/2.0/#PUT-graphics

doesn't say.

The reason it works is more complicated. The screen mode is set here:

1440 SCREEN 1,0:COLOR 8,1

Screen mode 1 (https://robhagemans.github.io/pcbasic/doc/2.0/#SCREEN-statement) has the following description:

SCREEN 1 CGA colour
320x200 pixels
40x25 characters of 8x8 pixels
4 attributes picked from 16 colours; 2 bits per pixel

An 'attribute' refers to the chosen palette. This is done with the COLOR 8,1 command. The 8 sets the background colour to dark grey:

https://robhagemans.github.io/pcbasic/doc/2.0/#COLOR

scroll down to 'EGA default palette' and look up colour 8.

The 1 sets the palette to palette 1, see 'CGA palette' in the same section.

So, in these lines

1600 LINE (6,6)-(97,195),1,BF
1610 LINE (183,6)-(305,195),1,BF

the 1 sets the colour to attribute 1 of palette 1 (cyan).

https://robhagemans.github.io/pcbasic/doc/2.0/#LINE

B% is an array of words, but a PUT array uses bytes:

The image stored in the array can then be put on the screen using PUT. For the purposes of GET, any array is considered a string of bytes.

So B% looks like

11000000
11000000
11000000
...

with 600 entries.

The width of each entry is 2 as we are working in SCREEN 1, which means we need 2 bits to specify an attribute, in this case 11 which is LO-WHITE in palette 1.

This is the first pixel, and the only one on the row. Word-alignment then truncates the rest of the line.

Data is word-aligned at the end of each row

so we need only to have assigned 97 entries of B% in the first place.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .