Summit Middle School classes for Andrew Busch
Andrew Busch - Summit
  • Home
  • Algebra 1
    • Alg 1B - Last Week
    • Alg1B 14 HW - Intro to Functions
    • Alg 1B 11 - Rational Expressions
    • Alg 1B 12 - Radical Expressions
    • Alg 1B 10 v2.0 - Quadratic Functions >
      • 10b Graphing with Pennies - Desmos Tutorial
      • 10i Snowboard Quadratic - Alg1B
      • 10 Quadratics Project
    • Alg 1B 10 Book - Factoring Quadratics
    • Alg 1B 9 - Exponential Functions
    • Alg 1B 8.5 - Representing Data
    • Alg 1B 13 - Inequalities
    • Alg 1B 8 - Best Fit Lines and Linear Regression
    • Alg 1B 7 - Linearity
  • Geometry
    • Geom Last Week
    • Geom 12 - Probability
    • Geom 11 - Circumference, Area, Volume
    • Geom 10-Circles
    • Geom 9 - Right Triangles and Trigonometry
    • Geom 8 - Similarity
    • Geom 7 - Quadrilaterals and Other Polygons
    • Geom 6 - Relationships Within Triangles
    • Geom 5 - Congruent Trianlges
    • Geom 4 - Transformations
    • Geometry 3.5 - Constructions
    • Geom 3 - Parallel and Perpendicular Lines
    • Geom 2 - Reasoning and Proofs
    • Geom 1 - Basics of Geometry
  • Programming
    • Directions for Sharing Programs with Me
    • Hour of Code
    • Intro to Python >
      • Installing and Using Portable Python
      • Introduction to Programming
      • Interactive Storyteller
      • Sophisticated Calculator
      • Getting Started with Games
      • Word Length Frequency
      • Substitution Cipher
      • Simple Game of Paddleball
      • Animating Many Objects
      • Accelerator
      • Applying Trigonometry
      • GIFs
      • Programmatic Art
      • Battleship
      • Pong
      • CodeCademy.com Suggested Work
      • Python Resources
    • Advanced Python >
      • Python Installation
      • Review of Intro to Programming
      • Objects and Classes >
        • More on Classes: Functions, Methods, Inheritance
        • Quadrilaterals
      • tkinter >
        • Paddle Ball
        • Light Bike
        • Frogger
        • Snake Game
        • Breakout
      • Reading and Writing Files
      • Directories and Importing Modules
      • Raspberry Pi
      • API's
      • Python Puzzles
  • Clubs
  • Graphing Calculator
  • PARCC Practice

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.
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.
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.
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.
Extensions
  1. Add a brake button that reduces the velocity by half.
  2. Modify your paddleball game so that the user controls the paddle's velocity.  How does this change affect game play?
  3. 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.
  4. Make the thing bounce off the sides of the window.
  5. 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.

Powered by Create your own unique website with customizable templates.