Classic Computer Magazine Archive ANTIC VOL. 2, NO. 9 / DECEMBER 1983

IN THE PUBLIC DOMAIN

WORM SQUIRM

by JOHN GUNTHER

Today, you're going to take your worm out for a squirm around the block.

Your hungry little worm has a lot of tempting goodies to strive for in this game of skill. Your mission is to direct it to the tasty bugs. Don't let it run into the walls or into its own tail, or turn back on its own neck; if you do, its eating days are over! Survive this stage of the game, and you will find more challenges ahead of you. Small castles appear on the screen for your worm to attack. However, if you don't get to them fast enough, they turn into impervious stone obstacles.

There is more to Worm Squirm than meets the eye. Not only is it fun, but it's also a tutorial in writing a fast program in BASIC. After you've played with it for a while, I think you'll be amazed to discover that there is not a single line of machine language in the program. BASIC is not necessarily a slow language.

One simple technique I used in the program is a test for the absence of obstacles in the worm's path (i.e, "if there is nothing in the way, then continue"). See line 115 in the program, I used the statement "IF NOT PEEK(PPOS) THEN 150," where PPOS is the next location in the worm's way. Many programs check to see if the next location involves any one of a number of possible obstacles, but this often requires checking for three or four different items, which takes time.

IF NOT PEEK, on the other hand, is one of the fastest tests there is in BASIC. It jumps over the rest of the tests if the path is clear, and uses a PEEK, which is also very fast. PEEKs and POKEs to the screen were also used for fast movement.

I put the main loop and the most-used subroutines toward the beginning of the program. When BASIC looks for lines and subroutines, it starts at the beginning; therefore, it's to your advantage if BASIC can find them as soon as possible.

The use of variable GOSUB's (see line 150) allows the program to be more flexible, especially when different levels of play are used (since they use different numbers of variables).

The real heart of the program, however, is the dual character set. What you see as animation, when the worm's body changes and the bugs quiver, is actually two different character sets being alternated on the screen. Some of this switching is done in lines 115 and 170. But while the worm is eating, the switch is performed in lines 2010 and 2055. Location 756 (in memory) holds the "page number" of the character set. This location is POKEd with either SET or SET+2. New character sets are defined in lines 5020 through 5054.

The game displays the highest score achieved in a session of play, and also shows the names and scores of the top five players at the end of each game. This routine would probabiy be useful in a number of games. You can find it in lines 8050 through 8610, if you'd like to add it to one of your own games. To do this, you'll also have to define the strings, as was done in lines 4000 through 4004.

There are ten skill levels to choose from in Worm Squirm: Level 1 is the most difficult. If you have small children who need an easier game (or if your reflexes aren't what they used to be), you can input any higher number you like. The higher the number, the easier the game.

When your conquering worm is young, it is fairly easy to maneuver. As it gets older, though, having eaten more bugs, it gets much longer and the chance of it running into its own tail (and thus being terminated) increases dramatically. As a result, it's necessary to develop a strategy to keep the tail out of your way if you want to survive and prosper.

At the start of the game, you are provided with nineteen red bugs; these are worth ten points each. A green bug appears at random intervals, but disappears if not eaten quickly. This one is worth fifty points.

If you live to be a big worm, you are rewarded with a higher level of existence to explore. Here, you find castles waiting to be terrorized. If you don't get to them fast enough, however, they turn into solid fortresses that block your path. As a warning signal, the fortresses grow darker as they solidify. So be forewarned.

PROGRAM DESCRIPTION

LINE NUMBERS DESCRIPTION
20 - 30 Green bug subroutine.
100 Wait for player to start
105 - 107 Main loop: Read stick, move WORM, check for edibles
200 - 430 Variable intialization and screen drawing: fill with red bugs
1000 - 1010 WORM's demise
2000 - 2080 Eat bugs
3000 - 3060 Castle subroutine
4000 - 4008 One-time variable initialization
4010 - 4027 Instructions
5000 - 5200 Alternative two character sets; redifine characters
8050 - 8610 Names and scores input
9000 Screen location subroutine
10000 Difficulty level input
10100 - 10105 Level completion subroutine

CHANGEABLE PARAMETERS
EXT: Length of time the green bug is on the screen; it's related to the length of the WORM (EXT = LT + SZ where LT is the current position and SZ is the WORM length)
L(500): In line 4006, this array stores the locations of the WORM's body. Increase the array length and you will increase the time at a level. I use TRAPs to abort each level and reset L(LT).
SZ: In line 2070, SZ=SZ+1 everytime the WORM eats, it gains one body segment. Change it to SZ=SZ+2 and the WORM will grow faster.

Listing: WORMSQRM.BAS Download