Python Math

Diagram of the Pong table showing how the game works.

Diagram showing how the Pong game works

I have blogged before about my experience with MOOCs (Massive Open Online Courses) and, in that post, mentioned that I was learning the programming language Python.  Well, I successfully completed the course, earning a Statement of Accomplishment (go me!), and, perhaps more importantly, added to my ever-growing list of reasons why math is so important, useful, and necessary for people to learn, understand, and appreciate.

Before I talk about the math of programming, let me mention a few things about Python itself.  It's a fairly new programming language (released in the early 90's) and it's named for "Monty Python's Flying Circus" (not the snake).  Here's a little more about it: (from this Python page)

The Rice programming course has two main features that I believe make it one of the best MOOCs I've experienced so far:  

(1) A course structure and set of online tools that used technology to its best advantage and did the most to minimize the disadvantages of "distance" and lack of personal interaction.  Specifically, the web-based tool, "CodeSkulptor", allowed all of us novice programmers to write and test our Python programs without having to get hung up on downloading the language.  For me, this was HUGE since I experienced A LOT of frustration during my Computational Photography class in trying to get the correct version of Python loaded and working on my Mac (FYI, I never did, and, after giving up, enrolled in this course instead).  

(2)  A game-based approach to learning the program that involved mini-projects that asked us (the students) to create programs that either had a game 'feel' to them or were actual games.  I got to create the following Python programs:

  • "Hello World" (classic first program for a new language)
  • Rock-Paper-Scissors-Lizard-Spock (more complicated version of the typical RPS)
  • Guess the Number
  • Stopwatch Game
  • Pong (classic arcade game)
  • Memory (classic card game)
  • Blackjack (classic card game)
  • Spaceship & Rice Rocks (versions of classic arcade game, "Asteroids")


In this blog, I'm going to discuss Pong and some of the mathematics needed to make the game work.  I plan to use my experience and new-found knowledge of Python programming in my future algebra & geometry classes because here is just a partial list of the mathematics concepts / formulas that were utilized when I programmed the list of games shown above:

  • Variables
  • Plotting points, coordinate pairs
  • Distance formula, Pythagorean theorem
  • Equations for lines
  • Properties & equations for circles
  • Iteration
  • Translations
  • Vectors and applications to geometry
  • Slope, rate


Honestly, I think Python may be a GREAT way to teach an entire practical mathematics course within a context that students may LOVE. If any of you have done this, I would love to hear about it: contact me.

Before I discuss the programming problems that math allowed me to solve, let me describe the way Python plots points, because the coordinate system used is slightly different from the typical "school" version (i.e. Cartesian coordinate system).

Description of the coordinate system used to plot points in Python.

As you can see above, a point is given by two coordinates:  the horizontal component (aka "x") and the vertical component (aka "y").  However, in Python the starting point (origin) is the upper left corner of the screen (or "canvas") rather than the intersection of the two axes that is typical in school graphing.  Also, the location of a point is given by a positive horizontal value (which determines how many units you travel toward the right side of the screen) and a positive vertical value (which determines how many units you travel down toward the bottom of the screen). In the example point plotted above, the Python coordinates would be given (3, 3) [meaning, start at the upper left corner of the screen and travel 3 units to the right and 3 units down, then mark that point].  If we were graphing this same point in school on the Cartesian coordinate plane, it would have coordinates (3, -3) because, to indicate 3 units down on the vertical axis requires a negative value.  As a mathematician, this change was strange for me; however, it also helped me see how useful it would be to have students who could fluidly and competently graph according to whatever "rules" are given (rather than the mistaken belief that it's always done the same way).

Plotting points was required to make the Pong game work because I had to find out when the ball touched the "gutter" (see diagram at the beginning of the blog post) in order to award a point, and also when the ball touched a paddle in order to alter the ball's direction and velocity to allow play to continue.  Achieving these relatively simple goals required significant understanding and application of mathematics.

First, in order to determine "touching" I had to know the distance between two points.  Why?  If the distance between two points is equal to zero, then they are touching; if it's greater than zero, then they are not touching. In Python (just like in school math), the easiest way to calculate distance is by using the distance formula derived from the Pythagorean theorem.  This screenshot shows the "math" and the accompanying Python code.

The math of programming:  distance formula & the Pythagorean theorem are necessary to make Pong work.

Next, in order to determine how the ball will bounce when struck by a paddle, I had to use vectors to dictate motion.  This involved breaking the coordinates into their horizontal and vertical components and incorporating some physics to handle the change in velocity.  This screenshot shows a bit of the math as well as the related Python code: 

The math of python:  understanding of vectors and triangle properties are needed to make Pong work.

Of course you can see that using vectors requires both a speed (acceleration) as well as a direction.  To determine the direction the ball will bounce requires more mathematics, this time in the form of geometry and reflections.  [Note: reflections are one of the rigid transformations, or isometries, that I blogged about before - HERE.]  Here is a screenshot that shows part of those calculations: (have you ever seen a more perfect example of reference angles in your life?! :-) #MyMathGeekIsShowing)

Python math reflections.tiff

As I hope you can tell, this entry just scratches the surface in describing the wonderful way math is needed to make this program work.  I believe students will really "see" how math is needed in computer programming and will be tremendously motivated to create a working game -- I know I was!  What do you think? Tweet or email or connect with me on LinkedIn to let me know.

Finally, here's a screenshot of the game itself (this is from my personal program).

Screenshot of my actual Pong game, created in python.

Of course it's WAY more fun to actually PLAY it: here is the link to do so.

References:

Small History of Python

Python official page: http://www.python.org/

Pong arcade game page: http://www.ponggame.org/

Introduction to Python Programming MOOC course

Web-based Python interface: CodeSkulptor

My Pong game! (Enjoy!)


RSSRSS

The Solver Blog

Author:  Dr. Diana S. Perdue

© Rimwe Educational Resources LLC 2019