Space Dock -- your first astronaut training flight

Space DockPretend that this program is a computer simulator that trains you to dock a space ship. All you have to do is maneuver a green dot (your space ship) to a red dot (the space station). The images won't win any special effects awards, but the physical laws that move the dot are correct.


Space Dock -- instructions for running the program

Space DockMoving around in space without gravity or friction is a lot harder than it looks. It's even more difficult when you have things like asteroids and other space junk in the way. When you move this dot by pressing the arrow keys, you can apply forces in all eight directions, including the diagonals.

Here are some things you may wish to try when you run Space Dock.

After you've finished playing with the program, press the Esc key to stop. Then open the browser and return to this page to learn how the program was created and how it works.


Run the Space Dock program.

After you've finished running the program, press the Esc key to stop and return to this page.


FlowchartSee how it runs -- the flowchart

If you looked at the flowchart for previous program, Intro to Isaac, you will see that the feedback loop shown here is similar. In both programs, the computer loops back to continue the same instructions over and over, causing the dot on the screen to continue moving if it has any momentum. These programs also check the keyboard continuously to see if you have pressed a key, applying a new force to the dot.

Important additions to Space Dock include the Start instructions, which create an asteroid field on the screen, and a Timer that displays the elapsed time. Two decisions, shown by the diamonds in the flowchart, cause this program to respond differently if you are docked correctly at the red dot or if you hit an asteroid.

Decisions like this are shown in diamonds because it is easy to see what the program will do in each case. If a condition such as hitting something is met, the computer branches to the side. Otherwise the program flow continues downward in the diagram.

There is no special way to draw a flowchart. Use whatever method helps you understand what is happening when the program runs. In designing larger programs, I often begin by drawing a flowchart that shows the complete system. I then draw additional flowcharts to expand on each section. A program as large as the Keyboard Trainer could easily involve over a hundred drawings.

If you would like to see an even more detailed explanation of flowcharting, check out Introduction to Flowcharts.


Computer game design

While Intro to Isaac is an interesting program, Space Dock qualifies as a game. The differences are slight, but important. A game, whether it is a video game, a board game, a sports event, or just an interesting personal challenge must have these four components:

Notice how few elements are actually necessary to create the Space Dock game. A green dot, some white dots, a clock, and a few sound effects do the trick because they create the illusion of the four essentials: goal, obstacle, suspense, and doom.

Programs for sale -- 25 cents

Creating a game that others will enjoy is both exciting and rewarding. Selling anything you create, whether it's a program or a poem, is a significant accomplishment. In spite of this, some "serious" programmers I know feel that game and arcade programming is kid stuff and not very important. I disagree. To illustrate how difficult it is to write a successful arcade game, consider asking any young person to simply give you a quarter. Some programmers can create games that kids are willing pay for again and again. That's programming mastery.


Inside look -- making time...

Nothing adds suspense as much as a ticking clock. It's a simple gimmick, but it almost always works. In this case, adding a timer to the program also makes it competitive because you can compare your speed with your friends. I could use methods other than elapsed time for determining success, such as calculating the amount of "fuel" you used or how efficiently you maneuvered the rocket, but a timer seemed like the best choice.

TimerThe most impressive docking maneuver I saw was done by a student who decided that finesse, rather than sheer speed, was the goal. He maneuvered his space ship to the left and off the computer screen completely. Then, navigating by the coordinate numbers only, he circled around and came back on screen from the top without seeing his dot at all. When his ship coasted in to view from the top of the screen and then made a perfect landing with no corrections at all, I was impressed.

Adding a timer or a clock to your programs is easy because I have created a short subroutine or SUB that does all the calculations. All you have to do is add this instruction and the program will print the time on the screen:

The first instruction sets the variable Time! equal to the elapsed time. The second instruction prints this number on the screen. The exclamation point after Time! tells the computer to calculate the time in fractions of a second.

TimeClock is one of more than a dozen special tools I have added to the programmer's Toolkit. This tool is described in more detail below.


More ideas... Kaboom! Pow! Zap!

It isn't hard to think of more things to add to this program, and my first choice would be better sounds. The tunes I use in this program are easy to do in BASIC, but they don't have the impact of real sound effects. The Crash! when you hit an asteroid would be so much better with the sounds of crunching metal, smashing rocks, body parts being sucked out the broken air lock with a big "Sluuuurp!" some screaming, and stuff like that. Of course, if you were really watching a ship hit an asteroid in space, you wouldn't hear anything because space is a vacuum and sounds don't travel at all. Dead silence.

BangThe fact that sound doesn't travel in a vacuum hasn't stopped most movie and TV science fiction writers from having things go "Kaboom!" in space, as well as letting us hear ships fly by with a satisfying "Swoosh!" with phasers zapping away. And it wouldn't stop me from adding really satisfying sounds to my science fiction either. As soon as I figure out a simple way to add real sounds to QuickBASIC, I'll let you know. While I like the simulations to be as real as possible, a little theater doesn't hurt. So if you get any ideas for good sound effects, or if you get a really low time in the game, drop me a note at ainsworth@qwerty.com. Before you send me any code or specific suggestions, however, please also read my copyright info.


ToolkitProgrammer's toolkit

These three subs are included in TOOLKIT1.BAS. If you want more information on using the complete toolkit, try Using the Toolkit for details on how you can easily add these features to any program you write.

Tunes (N)

While waiting around for the inspiration to create really good sound effects, I decided to do things the easy way, with a collection of tunes for every occasion. This sub uses both the PLAY and the SOUND commands in BASIC to create some standard sounds I use often in this and other programs. You could copy the PLAY statements below and just add them to your programs, but it's easier to load this sub and then call the tune you want with Tunes N, where N is a number from 1 to 4. Tunes 1 and Tunes 2 are used in this program and again in Newtona 500 (coming up next). Tunes 3 and Tunes 4 show up in later in Hangperson. You can hear all four tunes if you run the TOOLKIT1.EXE or TOOLKIT1.BAS program.

If you get inspired, you can add your compositions to this sub as Tunes 5, 6, 7, etc.

SUB Tunes (WhichOne)
SELECT CASE (WhichOne)
CASE 1 'victory tune
PLAY "L15 O1 C E G O2 C L8 N0 L15 o1 G O2 L5C"
CASE 2 'dribble
FOR n = 200 TO 32 STEP -10
SOUND n, 1
NEXT n
CASE 3 'funeral march
PLAY "<<<<l2d L3D L7D L2D L3F L7E L7E L7N0 L7D L7D L7N0 L7C+ L2D"
END SELECT
END SUB

TimeClock (Time!)

This sub can be used to add a running timer anywhere in your programs. When you wish to start the clock at zero, put this instruction in your program:

TimeClock 0

To print the elapsed time since the clock was set; you will need two instructions. The first will set the variable Time! to the elapsed time, and the second instruction will print the time on the screen. The numbers following LOCATE will be adjusted as required to place the readout wherever you want it on the screen. If you want the clock to run continuously, you will have to put these instructions in a program loop so that they repeat.

The sub that does all the work is included in your toolkit. There are two important things to notice about this sub. First, the STATIC statement is required. This tells the sub to remember and keep track of any variables used. In this case, the variable ClockStart! is first set to zero when you start the clock. Each time you request the elapsed time, the value of ClockStart! is subtracted, from TIMER, giving you the number of seconds since the clock was started.

CrashDetect (x, y, c, Result)

There are two ways you can tell whether an object on the screen has hit something or not. If you keep track of the location of everything on the screen, you can check with this information to see if the object is in a location that's already occupied by something else. This works, but it's complicated.

An easier way that works well in BASIC is to use the colors on the screen to tell you when an object you are moving is about to hit something. Before you move an object, check to see if the location you are moving to is colored or not. If it is, you are about to bump into something.

In this program, I color all the asteroids white. Before I move the green dot, I check the screen to see if its next location is colored white or not. If it is, I know that you have hit an asteroid. I use this same trick later in Newtona 500 to know when a car has skidded off the track or driven into the infield.

This sub is a general formula that sets Result = 1 if an object at location x, y hits anything in color c. In Space Dock I check for collisions with white objects (asteroids), and in Newtona 500 I check for a black background.

WhatKey(K$)

When you get a key from the keyboard, your program will have to figure out what to do with it. This is a lot easier if you use this sub to tell you what key has been pressed. There's no problem if the user types a letter or a number. But if someone presses a different key, such as one of the direction arrows or the Esc key, it is nice to be able to figure this out easily. You also want to react to weird keys such as Alt-F4, the standard escape from Windows. Here is how I make all these decisions easily in a section of code that uses both Gobble and WhatKey(K$):

The complete code for the WhatKey(K$) sub takes care of all the possible inputs to your program. Notice that I only use F1 and F2 in most programs. If you want to use any other special keys or key combinations, you can add them to this sub and then use them in all the programs you write.


Seminar homewww.qwerty.com