# Linear Functions and Missile Command: A coding mini-project

Hi everyone – if you’re still hanging in there with me, and I haven’t lost you due to my long absences from engaging with the blogging world, I’d love to share a little mini-project I did with my algebra class. I gotta say, I really enjoy doing coding mini-projects in math class. I like putting the projects together, the kids find them really engaging and fun, the problem-solving is interesting and surprising.

We started out the year as many algebra classes do, studying linear equation-solving and the properties of linear functions. Things have been going reasonably well, but I knew some students were irritated not knowing “when will we ever use this?” – It’s one of my ongoing pet peeves with the high school math curriculum, that the way it is expected to be learned and the way it shows up in standardized tests doesn’t really match how these concepts are used in real life. Including linear functions. These are SUPER useful in real life – in all kinds of situations where you have to make predictions involving a constant rate. But the kind of problem-solving you normally do with linear functions doesn’t always look like what we do in math class, converting functions to slope-intercept form or point-slope form, graphing on a 4-quadrant plane, etc.

I started by giving the students that basic speech. These concepts are incredibly useful, but in the real world they don’t often look like what we see in math class. So today we’ll explore one possible application of linear functions.

And I introduced them to the retro game of Missile Command.

We watched a YouTube video of someone playing the game (the screen captures were fetched from this video too):

The students were in awe. Back in the early 1980’s, we were in the end stages of the Cold War, and the threat of a nuclear apocalypse was a low-level stress always present in our lives. In the game, missiles rain down on your cities and your job is to shoot them down before they destroy your nation. You even get a little 8-bit mushroom cloud when a city is nuked. It really was a dark, terrifying video game and I remember feeling super tense while playing it. There’s no way to win. The missiles just fall down faster and faster until you lose all of your cities and the world ends.

The missiles always start at the top of the screen and rain down in a straight line. In the game, if you could predict where those missiles would land, you could prioritize which ones you needed to shoot down. As the game got faster, for example, it didn’t make sense to shoot down a missile heading for a city that was already destroyed. Focus on the ones heading for your still-standing cities.

We watched for a bit and some students insisted, “Those aren’t straight lines!” But they were, they just didn’t look straight when rendered in 8-bit. For example, these missiles below go down 2, over 1, down 2, over 1, down 2 once more, over 1, then down 1, over 1. Repeat the pattern – 2, 2, 2, 1. 2, 2, 2,1. The slope is a net -7/3. We could identify missiles with a slope of -5/2 (down 3, over 1, then down 2, over 1) and -8/3 (2, 3, 3. 2, 3, 3).

I made the students a little mini-game starter in Khan Academy. In this starter, the missile is at the top of the screen, and a house is at the bottom, but the missile doesn’t move. We did have to start with a little discussion about how the coordinate plane in many programming languages is different than the one we use in math class. Often, a computer game’s coordinate grid has (0,0) in the top left. X increases as you go right, which is what you’d expect. However, Y increases as you go DOWN the screen. It’s intuitive if you think of the way spreadsheet cells work, or the way you read text. Start in the top left, work over, then down.

The key is to change lines 31 and 32, which currently read:

x = x + 0;

y = y + 0;

If you modify the amount added to X and Y, the missile starts moving. The trick is to make the missile make a straight line and hit the house. If you hit the house, the screen turns red. Decimal amounts can be used.

Students found that if they tweaked the velocities such that the Y velocity is a *little* more than the x velocity, they can hit the house. For example, these combinations worked:

x = x + 10;

y = y + 12;

x = x + 0.5;

y = y + 0.6;

A y/x ratio of *about* 6/5 was ideal, and there was some wiggle room since the house has a hitbox about 20 pixels wide and tall. We made some predictions about other combinations before trying them.

Next, I challenged the students to move the starting location of the missile, on lines 2-3, and the starting location of the house, on line 6, and find a new y-x velocity pair that would let the missile hit the house. Some students, of course, thought of putting the house directly below the missile and using a y-velocity only, setting up a “no-slope” situation.

On line 8, there’s a line of code specifying “FrameRate”. This is the number of times the draw() function is called in one second. It’s 60 by default, so the missile will move 60 times each second. For the final challenge, students had to tweak the frameRate() and also the velocity so the missile would hit the house in *exactly* five seconds. Students got timers out and spent quite a while trying to get the missile to hit in exactly five seconds. Many figured out that if the missile moves roughly 80 pixels each second, it takes five seconds to get to the bottom of the screen… so it was a matter of finding combinations of frameRate * y-velocity that would equal about 80.

I just want to finish with a plug for Khan Academy’s computer programming interface, especially when it comes to little math mini-projects like these.

Khan Academy’s Computer Programming courses

Create a new Javascript Program

It’s my go-to resource when I want to create a quick little math activity. I don’t necessarily need my students to create a program from scratch all the time – often I’ll create a little starter program that needs fixed or modified. With this interface, I can save the program and push a link out to Google Classroom. Students click on the link, and then click “Spin-off” to get their own copy to modify. They can also see other students’ spin-offs, so if they get stuck, it’s helpful to see what someone else did. The program runs right away and needs no compiling time, so the students see the impacts of their changes instantly. The number scrubber and color picker make programming changes super easy and fun. The documentation tab is wonderful. If you’ve got students who are interested in taking the task beyond what you set up initially, just refer them to the documentation tab and there will be examples they can copy and paste.

It’s a fantastic coding environment for little activities like these, and I use it in math all the time!

## Recent Comments