Newtona 500 -- space race in zero gravity

Newtona pixThe annual Daytona 500 automobile race is a classic test of driver's skill. In our Newtona 500 version, your car is weightless because we have removed gravity from the equation. The skills you will need for best lap times in our race are better suited to a graduate of Space Dock than to Driver's Ed.


Newtona 500 -- instructions for running the program

Key diagram

Start! Press any key to begin. Press the left arrow key to start your race.

Key diagramGo! Trying to move around the track in a circle without gravity or friction is a lot harder than it looks. It's even more difficult when you are in a hurry. When you move this dot by pressing the arrow keys, you can apply forces in all eight directions, including the diagonals. Applying force accelerates the car and quickly builds up speed. You will have to apply a force in the opposite direction to slow down again.

Hint: Using the diagonal keys 7, 9, 1, and 3 is a lot faster than the arrow keys.

Here are some things you may wish to try when you run Newtona 500:

If you have trouble keeping the car on the track, you may want to return to the Intro to Isaac or Space Dock programs for a little practice. After you've finished running 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 Newtona 500 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

The feedback loop shown here is similar to the previous programs in the seminar. The computer repeats the same instructions, causing the dot on the screen to continue moving if it has any momentum. The keyboard is checked inside the program loop so that pressing a key at any time applies a new force to the dot.

There are three major decision points in this program, shown by the diamonds in this flowchart. Deciding that the car hit something is easy. The program checks the background color of the screen before drawing the car. If the screen is black at the car's new location, the car is now off course. The Crash! section takes care of the details.

The other two decisions work together to control the flow of the game. The Lap Counter detects when the car crosses the finish line, and updates the number of laps shown on the screen. If there are more laps to go, the program continues. After ten laps, a different decision is reached and the race is over.

If you would like to see an even more detailed explanation of flowcharting, check out Introduction to Flowcharts. And if you are interested in flight simulators, airplanes, and other computer simulations, be sure to follow this Scenic Route:

Computer simulations and digital realities


Inside look -- counting laps

Adding Total Time and Lap Time counters to this program, and ending the race after ten laps, make this game complete. The internal software that causes all the action is basically the same as the two previous programs.

You may have discovered a bug (Bug goes here)in this program if you tried to run the racecourse backward (clockwise). The Lap Counter handles things well enough -- it just counts backward. But the Lap Times get very confused. As soon as one of my software classes got this program, people discovered that by going back and forth over the start/finish line, they could trick the program and get really fast lap times. It's a good idea to make programs "bullet proof" so that a user can do anything at all without getting weird results. Even weird users like Chris, Mark, and Tom.

Winning times (legally) will probably require you to race in a set pattern of moves. I found that using the diagonal keys only and pressing for a certain number of clicks at each corner works well.


More ideas

The easiest way to improve this program would be to add a more interesting race course layout. You could draw any one of several sports car circuits on the screen, or make up your own. The program won't register a crash unless the dot hits the background color, so you can draw the colored track in any shape you want and the program will know if you are on course or not. The start/stop line should probably be left where it is, because I use these coordinates for counting laps.

This program doesn't have to just stop when the dot hits a wall. Another graphics trick you can add is a better ending sequence. For example, you could quickly print several pre-drawn images at the final destination to create an explosion effect.

I thought of racing two cars at once in this program, but many computers won't be able to handle all the processing necessary to run two versions of the arithmetic at the same time without slowing down. There is a 1/10-second delay built into the main program loop. You could remove this and get more speed, but the program would then run very differently on computers with faster or slower processors.


Programmer's toolkit

Most of the subs used in this program are from the toolkit and have already been used in the first two examples. In writing Newtona 500 I developed three special subs to draw the course on the screen, count laps as the car crosses the start/finish line, and show the current velocities.

DrawCourse

The drawing instructions for creating and coloring the racetrack are saved here. If you wish to change the track layout, all you have to do is change these instructions. If you wanted to let the user select the course, you could have several layouts, each with a different sub.

LapCount (X1, Y1, X2, Y2, LC, Time!) STATIC

This sub compares the old position of the car (X1, Y1) with the next position (X2, Y2) to see if the start/finish line has been crossed. If so, it updates the lap counter. If the lap time is faster than before, the sub also updates and prints BestTime! The sub is STATIC because it must remember the value of BestTime! from one lap to the next.

ShowStats (Vx, X, Vy, Y)

The race statistics shown in the top-left corner are calculated here. The important calculation is the Total Velocity, Vt!. This number relates to what a speedometer in the car would show. To get this number, the computer calculates the vector sum of the velocities in the X and Y directions. Here's how the velocity calculation might look as you are entering the first turn:

The car in our example is going 4 units in the X direction (to your left), and 3 units in the Y direction (down). The car is actually moving in the direction shown in red. How fast? The answer is the square root of the sum of X squared and Y squared. The computer instruction used in this program for calculating the Total Velocity (Vt!) to four decimal places is:

Vt! = INT(1000 * SQR(Vx * Vx) + (Vy * Vy)) / 1000

Statements like this that contain several instructions inside other instructions can be difficult to read. Here is an inside look. This first part of the instruction calculates the square root of Vx squared plus Vy squared. This is the length of the hypotenuse of the triangle or the red line in the diagram:

SQR (Vx * Vx) + (Vy * Vy)

The second part of the instruction changes the answer by rounding it off to four decimal places. The first calculation is nested or surrounded by another calculation that multiplies the result by 1000, rounds off the answer to the nearest number (INT), and then divides the result by 1000.

INT(1000 * SQR(Vx * Vx) + (Vy * Vy)) / 1000

This last step sets the variable Vt! equal to the above result. We use an exclamation point after the variable because that allows us to set this variable equal to numbers with decimals. Here is the final result.

Vt! = INT(1000 * SQR(Vx * Vx) + (Vy * Vy)) / 1000

More information about Pythagoras, the first mathematician


Seminar homewww.qwerty.com