I know that Bruce already gave you a program to use. However, I thought I would add some additional suggestions.
I teach a physics course for non-science majors called Physics for Video Games. This semester, I am converting my course materials from VPython Classic to GlowScript VPython.
If you are using VPython Classic, see Chapter 4 of the pdf at:
If you are using GlowScript VPython, see the updated version of this chapter at:
This chapter/activity in my course teaches students to add keyboard interactions to a simple program where balls move back and forth in the scene and a box (the shooter) shoots “bullets” at the balls. The following chapter, in each case, adds collision detection in order to complete this simple game.
In VPython Classic, there is no event for lifting a key. However, in GlowScript VPython, there is an event called ‘keyup’. To see two different versions of this simple game, go to:
The program "shoot-the-ball" uses the keyup event. Hold down the left/right arrow keys to move the shooter left and right. When you stop pressing the key, the shooter stops. Use the spacebar to shoot a bullet.
The program "shoot-the-ball-v2" is like the VPython Classic version of the game. In this case, the shooter travels left and right. Pressing an arrow key changes the direction of the velocity and pressing any other key sets the velocity to zero. Again, the spacebar shoots a bullet.
When I first created the course about 5 years ago, GlowScript was in its infancy so I used Classic. Now, in 2016 GlowScript VPython is the preferred choice. It has numerous practical advantages for students writing and submitting their programs. However, the keyboard event ‘keyup’ is a big advantage for games.
Aaron