Tetris for Applesoft BASIC

Program Listing by Mark Stock, Analysis by April Ayres-Griffiths

What do Steve Wozniak and George H.W. Bush have in common? They’ve both been seriously into Tetris! But who can blame them? The object of the game (as if you didn’t know) is to complete horizontal lines using falling shapes of various configurations. When you finish a line, it disappears, causing the rest of the blocks to fall down a line. However, if you stack up shapes to the point they overflow the top of the playfield: Game Over. When you finish a certain number of lines, the level ends… and in the next, the shapes fall faster, and you need to complete more lines! The insanity never ends.

In an era where games were becoming increasingly more complex, the simplicity of Tetris was seen as a breath of fresh air. Tetris would inspire a number of other “falling block puzzle games” such as Sega’s colour-matching Columns, and the three-dimensional Welltris. But Tetris would always remain king (tsar?) of the arcade puzzle game world, with sequels, clones and variations being released for virtually every console, computer, and operating system worldwide.

A little history

Alexey Pajitnov’s Tetris – early unauthorised versions of which, such as Spectrum Holobyte’s rendition described above, began appearing on home computers in 1987 – spawned a whole new generation of shape-based puzzle games.

Holobyte’s version took next year’s CES by a storm, and garnered the attention of the Soviet government, who held the rights to the game. They sold the arcade rights to Atari and the console rights to Nintendo.

Nintendo released Tetris on both its Nintendo Entertainment System and on the Game Boy, the latter as a pack-in with the portable console. The inclusion of Tetris arguably made the Game Boy a success –the game was perfect for smaller screen sizes, and was very addictive, spawning a whole generation of Tetris ‘junkies’. 

Nintendo’s version of Tetris for the NES was criticised for not having a two-player mode; however, Atari Games, perhaps mistakenly believing their arcade licensing gave them the right to release a console version, came out with their own Nintendo Tetris game through their Tengen subsidiary (created after the consumer rights to the Atari name were sold to Jack Tramiel, see Point & Click) which featured head-to-head play.

Nintendo sued, and Tengen was eventually forced to withdraw its cartridge from sale, after selling around 100,000 copies. They are collectibles.

About the listing

This Tetris listing for the Apple II has a number of interesting features that suggest the author has taken great pains to make the listing as efficient as possible. 

Constants vs. Variables

For one thing, rather than using constant values such as “1” or “2” in arithmetic expressions, they have opted instead to use variables.  This is a lot more efficient in Applesoft, as using constants requires the parser to convert a string in the listing into a number each time that it is encountered, rather than using a variable which stores the values in a format that is ready to use.  You can also see this trick being used with memory addresses, for example the keyboard address is stored in a variable called “KB”.

Finding the right line for all occasions

Additionally, subroutines which need to be called frequently (for example the block drawing routines), are towards the top of the program, and less frequently called subroutines (for example game over) are towards the end of the listing.  Although this might at first glance seem rather arbitrary, there is a reason for structuring the program this way. 

Applesoft programs are stored in memory as a series of statements with a known starting address (typically 2049 in memory).  In order to locate a given line of the program (for example if using a GOTO or a GOSUB) then we need to find the right line. 

In order to do this, the Applesoft interpreter will start at the beginning, and check each line number.  In memory, each line begins with a two byte line number, followed by two bytes which point to the next line numbers address in memory. So to find line 1000, we need to look at each line and if it isn’t the correct one, then move to the address of the next line and check that. 

This seems like a lot of work, right? The more of the program it has to search through for the right line, the longer each GOTO or GOSUB will take to perform.  So we can speed things up a lot by putting things that need to happen frequently, (and quickly) at the start of the program.   This makes the game faster as it spends more time doing what we want it to do, and less time trying to find the next line.

Why use code when data will do

The final thing that is interesting about this listing, is that rather than trying to define the various shapes in the game using code, which can become inefficient rather quickly, the author has opted to define the shapes themselves as data statements, which are read into variables. 

This makes the program easier to amend in future, should they wish to change the shapes, but also means much more simplified logic can be used in the game.  Where possible, let data define the game behaviour rather than hard-coded logic.  This is especially true when your program involves multiple different things that follow the exact same rules.

The listing (finally!)

This Applesoft BASIC program can be typed into a real Apple II (the preferred experience, although the Apple II’s keyboard could quickly become tiresome), or an Apple II emulator such as microM8 (Mac / Windows) or AppleWin (Windows)

In microM8 you can choose ‘A’ from the boot menu to go into Applesoft BASIC. If you use AppleWin you will need to boot from a DOS disk image.

Type in the numbered lines from the left-hand colum of each page. The notes in the right-hand column describe what the numbered lines do. Don’t forget to save often!

Aww typing… what is this, 1985? Yes! Typing in a program is not without value. (Click on each page for full-sized image)



Be the first to comment

Leave a Reply