Accelerator
The resources on this page were originally created by Dr. Aaron Bradley of Summit Middle School. I've done some reformatting to add clarity but that is about it. Enjoy!
|
In the game developed in A Simple Game of Paddleball, the user directly controls the paddle's position: hitting the 'j' button moves the paddle left a fixed distance; hitting the 'l' button moves it right a fixed distance. But in the real world, we usually control a vehicle's velocity, which in turn controls its position. In a car, for example, the user pushes the accelerator, which changes the car's speed; and a non-zero speed causes the car to change position. Similarly, on a bicycle, a strong input into the pedals translates to increased speed, which in turn causes a greater distance to be covered per unit time. In this exploration, we will program this form of control: the user's input will control a sprite's velocity rather than its position directly.
Let's get started.
Let's get started.
Set Up
Using your knowledge from previous modules, create a window and a circle called "thing" that is initially in the center of the window.
Using your knowledge from previous modules, create a window and a circle called "thing" that is initially in the center of the window.
Controlling Velocity
The 'j' button should cause an acceleration to the left. First, we create global variables to store the thing's velocity:
vx = 0
vy = 0
We make the 'j' button decrease the horizontal velocity by 1 unit:
def left():
global vx
vx += -1
w.on('j', left)
At this point, hitting 'j' when the program is running will not have any visible effect. We need to animate the thing in a fashion similar to that used to animate the ball in Exploration: A Simple Game of Paddleball:
def animate():
w.move(thing, vx, vy)
w.animate(animate)
That is, at a rate of 20 times per second, the thing is moved horizontally by vx units and vertically by vy units.
Run the program and observe the effect.
Now implement similar functionality to allow the user to move the object in all directions.
The 'j' button should cause an acceleration to the left. First, we create global variables to store the thing's velocity:
vx = 0
vy = 0
We make the 'j' button decrease the horizontal velocity by 1 unit:
def left():
global vx
vx += -1
w.on('j', left)
At this point, hitting 'j' when the program is running will not have any visible effect. We need to animate the thing in a fashion similar to that used to animate the ball in Exploration: A Simple Game of Paddleball:
def animate():
w.move(thing, vx, vy)
w.animate(animate)
That is, at a rate of 20 times per second, the thing is moved horizontally by vx units and vertically by vy units.
Run the program and observe the effect.
Now implement similar functionality to allow the user to move the object in all directions.
Energy Loss
Energy dissipates. On a bicycle, a powerful pedal stroke accelerates the bicycle, but in time the bicycle slows down. Let's modify the program to have that effect. The basic idea is to reduce vx and vy by some factor on each call to animate:
def animate():
global vx, vy
vx *= 0.95
vy *= 0.95
w.move(thing, vx, vy)
vx *= 0.95 is shorthand for vx = vx * 0.95.
Run the program and observe the effect. Adjust the factor so that the thing does not slow down as quickly; adjust the factor so that it slows down more quickly.
Energy dissipates. On a bicycle, a powerful pedal stroke accelerates the bicycle, but in time the bicycle slows down. Let's modify the program to have that effect. The basic idea is to reduce vx and vy by some factor on each call to animate:
def animate():
global vx, vy
vx *= 0.95
vy *= 0.95
w.move(thing, vx, vy)
vx *= 0.95 is shorthand for vx = vx * 0.95.
Run the program and observe the effect. Adjust the factor so that the thing does not slow down as quickly; adjust the factor so that it slows down more quickly.
Extensions
- Add a brake button that reduces the velocity by half.
- Modify your paddleball game so that the user controls the paddle's velocity. How does this change affect game play?
- If you haven't done so already, add drag to the paddleball game so that the paddle slows down when the user does not provide input.
- Make the thing bounce off the sides of the window.
- Make two buttons change the radius of the thing as well as the drag: when the radius is bigger, there's more drag; when it's smaller, there's less drag.