Decoded: DONKEY.BAS (1981) by Bill Gates and Neil Konzen for IBM Beginner-friendly, line-by-line code walkthrough by MaiZure Original source code: http://www.maizure.org/projects/decoded-donkey-bas/DONKEY.BAS I've kept the original BASIC line numbers for reference Line Comments 940 COMMENT 950 COMMENT 960 COMMENT 975 Set segment to GW-BASIC data and clear keyboard buffer (PC-DOS) 980 Sets string to "No", Only used to trigger a special mode at run time 990 Skip the next line 1000 Sets a string to "Yes" (dead code) TITLE SCREEN 1010 Sets up and clears the screen for use. Prints 'IBM' at the top center 1020 Prints 'Personal Computer' centered below 'IBM' 1030 Changes the color to green and prints the a top box border 1040 Prints 'DONKEY' with the box sides 1050 Prints more box sides and empty space between 1060 Prints more box sides and the version (1.1) 1070 Prints the bottom of the title box 1080 Changes the color to white and prints an IBM copyright notice 1090 Changes the color to yellow and prompts use to press space to start 1100 Keep checking user input until there is something 1110 Get user input 1120 If the user didn't press anything, retry check 1130 If the user pressed ESC, jump to exit procedure on like 1298 1140 If the user pressed space, jump to game start on line 1160 1150 If the user anything else then retry input check HARDWARE CHECKS 1160 Set memory segment to the base 0 1170 Verify CGA by checking bits 4 and 5 at 0x410. Good? Goto 1291 1180 This PC doesn't meet minimum requirements (no color!) Clear screen. 1190 Print first line of error message 1200 Print second line of error message 1210 Print third line of error message 1220 Prompt user to press space bar 1230 Sets address to the BASIC data segment 1240 Check for input repeatedly until there is something 1250 Get user input 1260 If there is no input, try for more (back to line 1250) 1270 If the user pressed ESC, go to the game entry anyway on line 1298 1280 If the user pressed space, prepare to start game on line 1298 1290 All other cases, repeat user input check 1291 Clear the bottom row for display 1292 If something bad has happened, prepare to abandon ship on line 1295 1293 Pause sound 1294 Jump to game start on line 1300 1295 Set blinking white foreground color on black 1296 Print an error message` 1298 Check for errors and restart 1299 Something failed during all of the above init -- exit program GAME INIT 1300 Comment 1410 Set color to black 1420 Assume A through Y are integers 1440 CGA screen mode 1 (320x200) with white color 1450 Declares an array of 500 integers (unused?) 1460 Declares 4 arrays, two for each half of both the donkey and the car 1470 Declare an array for the entire donkey 1480 Call subroutine to initialize donkey graphic 1490 Call subroutine to initialize car graphic 1500 Clear the screen 1510 Create an array for the center line animation pixel pattern 1520 Defines the pixel pattern of the line (11000000 11000000) 1530 Defines the center line array with a width of 2 and height of 193 UPDATE GAMEPLAY SCREEN 1540 Comment 1550 Set the base car x position to 110 and clear the screen 1590 Draw the white border of the game screen 1600 Draw the left cyan box 1610 Draw the right cyan box 1620 Print the donkey score header near the upper left 1630 Print the player score header near the upper right 1631 Print instruction line one in the lower right 1632 Print instruction line two in the lower right 1633 Print instruction line three in the lower right 1635 Print instruction line four in the lower right 1636 Print instruction line five in the lower right 1640 Loop down the center line and draw 10 length bars 1650 Car start position is (105,105) 1660 Draw the left and right road edge lines 1670 Print the player and donkey scores 1680 Move car up and check for victory. If player scores, jump to 2230 1690 Draw the XOR of the car in position (removes the car) 1700 Choose a random lane for the donkey to be in 1710 Loop from donkey start (off screen) to the bottom (Game loop!) 1720 Play some engine sound 1730 Check input, handle ESC, if the car changes lanes, overwrite it black 1740 Draw the donkey in position 1750 If the player has hit the donkey and jump to 2060 1760 For every 3rd redraw, overlay the animated center line 1770 Continue to the next game step. At the end of loop, remove the donkey INITIALIZE CAR GRAPHIC 1780 Clear the screen 1790 Set draw scaling to 8 pixels and color to white. 1800 Move the draw cursor to (12,1) and outline the front and right wheel 1810 Draw the right side rear, right tire, and bottom of rear left tire 1820 Finish the rear left tire and the rear half of the front left tire. 1830 Complete the front left tire and most of the nose 1840 Complete the outline of the car 1850 Draw the window 1860 Draw the front pair of engine ports 1870 Draw the middle pair of engine ports 1880 Draw the rear pair of engine ports 1890 Draw a box around the car 1900 Fill in the car graphic 1910 Create an integer array to hold the car graphic 1920 Save the graphic (29x45) in to the storage array 1930 Return to game init INITIALIZE DONKEY GRAPHIC 1940 Clear the screen 1950 Set draw scaling to 8 1960 Move draw pointer to (14,18) without drawing 1970 Start at the tail and outline the back and to the far ear 1980 Outline the rest of the head to the top of the legs 1990 Outline the front legs, stomach, and the first back leg 2000 Outline the last leg and connect to the start at the top of the tail 2010 Fill in the donkey graphic 2020 Draws (XOR removes) the top pixel of both eyes 2030 Draws (XOR removes) the bottom pixel of both eyes 2040 Save the donkey graphic (32x26) in to the storage array 2050 Return to game init CAR CRASHES IN TO DONKEY 2060 Add to the donkey score and print a losing message 2070 Save the left half of the donkey graphic 2080 Set both halves of the donkey as the current location 2090 Save the right half of the donkey graphic 2100 Save the left half of the car graphic 2110 Save the right half of the car graphic 2120 Set the positions of both halves of the car as the current location 2130 Start a 6-step loop animating the donkey/car parts and offsetting 2140 XOR (remove) both halves of the car using their part positions 2150 XOR (remove) both halves of the donkey using their part positions 2160 Separate the car halves, moving them further apart and down 2170 Separate the donkey halves, moving them further apart and up 2180 XOR (draw) both halves of the car using updated part positions 2190 XOR (draw) both halves of the donkey using updated part positions 2200 Play random sound tones and continue to the next loss animation frame 2210 Busy wait 2220 Clear the screen and jump to background drawing 2230 Player won this round, so update player's score and print a message 2240 Busy wait 2250 Clear the screen and jump to background drawing