Making the game more realistic!

Timing is important to any game. If we are simulating an environment, having it slow down or speed up ruins the simulation if it isn't actually part of the game.

In this lesson students learn how to time code to make the simulation more realistic.

Fixing the Speed

For asteroids to be realistic, the speed should now slow down when we shoot at asteroids and speed up again when the torpedoes die. However, the fact is that when we fire a torpedo there is more work to do. We have to check each time the torpedo moves if it has hit an asteroid. This will slow down the game unless we make sure that when there are no torpedoes that it takes the same amount of time for the play function to execute as when there are torpedoes. That is the key to our animation running at one speed. The play function must be called on regular intervals. Let's say every 10 milliseconds we should call play.

To guarantee that play does not take more than 10 milliseconds to execute, we'll limit the number of torpedoes to 20. To do this, you must modify some existing code when Photon Torpedoes are fired.

    def fire():
if len(bullets) < 20:
bullet = PhotonTorpedo(cv,ship.xcor(),ship.ycor(),\ ship.heading(),ship.getDX(),ship.getDY())

screen.onkeypress(fire," ")

This will guarantee that there are no more than 20 torpedoes (i.e. bullets) in the list of bullets at any one time.

Next, we'll turn off screen updates and control that by ourselves. We'll do this because if screen updates are done automatically, then we can't tell exactly when they are done. That could mess up our timing of the play function calls. Since play will be called on regular intervals, we'll just update the screen ourselves.

Find the line in the code that contains screen.tracer(10). Change this line to:


For the screen to update, we must manually call update now. At the beginning of the play function, call the update method on the screen.

    def play():

Now, to time the play function we need the help of some other code. We need to import the datetime module to be able to get the time when we need it. Import statements go at the top of your module or program.

import datetime

Now we want to measure how many milliseconds it takes to execute the play function. We can find this out by getting the current time when the play function starts and the current time just before the play function ends. Then we can subtract the start time from the end time and that will tell us how long it took for the play function to execute.

So at the beginning of the play function we need to record the current time.

        start =

Then, at the end of the play function, just before the call to update the screen, we'll get the time again. At the same time we'll calculate the amount of time it took to run the play function and set the timer to call play again precisely 10 milliseconds from when it was last called.

        end =
        duration = end - start
        millis = duration.microseconds / 1000.0
        # Set the timer to go off again

The duration doesn't give us milliseconds, but we can get the microseconds. A microsecond is a millionth of a second. A millisecond is a thousandth. So if we divide by 1000 then we can convert microseconds to milliseconds. The new call to ontimer guarantees we'll call play again precisely 10 milliseconds after it was last called.

Have More Time?

This game is a complete game, but Atrari's asteroids did contain more elements. There were flying saucers that could shoot at the spaceship. If the spaceship shot down a flying saucer then the player scored 200 points. Small flying saucers were worth 1000 points.

There are also more levels to the Atari version. More levels could be added by making a level variable and increasing it for each level.

What's Next?

Computer programming is a fun, creative discipline. It has consistently been rated as one of the top jobs in America in terms of job satisfaction, pay, and benefits. The decision of which career you will pursue may seem like it's a long way off, but it'll come sooner than you might expect.

In the meantime, you can learn more about computer programming to see if it is something you might enjoy doing for a lifetime. To learn more, get yourself an introductory text like Python Programming Fundamentals by Kent D. Lee or Python Programming in Context by Brad Miller and David Ranum. These texts will introduce the concepts you need to know and skills you need to develop to become a computer programming. One text will not make you an expert, but it will get you started. Either of these texts is what college students use when majoring in Computer Science at Luther College.

Most importantly, have fun! Learning to program can be a lot of fun. You can program all kinds of games and useful applications with Python.