Bouncing Off the Walls
They knew from working on mazes that they could make the turtle bounce off a wall. They started by programming the walls to cause the turtles to turn right 180, but this didn't look right; not the way the puck would really move.
They had a pretty good idea that there was a connection between the angle at which the puck struck the wall and the angle that it bounced off; that if it hit at a shallow angle it would bounce off at a shallow angle; in at a steep angle and out at a steep angle.
We worked with them to make a list of cases that they were sure about. If the turtle went straight into a wall it would come straight out. To test how this looked and behaved in practice they set the turtle's heading to 0 with the command seth 0. The turtle was programmed to go forward 1 many times and the wall color contained the turtle instruction seth 180. A click on the turtle sent it into the wall and it bounced off convincingly. We suggested that they seth 45 and send the turtle into the wall while trying different turtle instructions for the wall color until the bounce looked right. They were happy with a reflection angle of 135.
With a few more cases the chart looked like this:
The boys also realized that they could switch any pair of headings. If the heading going in were 135, it would bounce out at a heading of 45; in at 120, out at 60, etc.
What's the pattern? This question was heard frequently in math class where students had become comfortable with the idea of looking at a collection of data and trying to find rules and relationships.1
In short order Brandon and Jerrit decided that the heading on the way out was equal to 180 minus the heading going in. The horizontal wall was programmed with the turtle instruction seth 180 - heading. This was then tested with many different turtle headings and appeared to work properly in all cases.
But, it didn't work for bouncing off the vertical walls. The boys were left to puzzle this one out with the suggestion that there would probably need to be a different color for vertical walls that would be programmed with a somewhat different instruction. After some trial and error they came up with seth 360 - heading as the formula for reflection off vertical walls. We don't know the process by which they arrived at this. They did it on their own and their explanation was that they just tried different things until something worked. (Instead of seth 360 - heading it could have been seth 0 - heading or simply seth minus heading, but we left it alone at that point.)
Having gotten the puck to bounce off the walls the next step was to get the paddles to work. Since the paddles are horizontal surfaces they are programmed like the horizontal light blue walls, to cause the puck to seth 180- heading. But the paddles are moving turtles, not part of the background. Instead of programming a color to cause a turtle to bounce, the bounce would occur when the puck turtle touched a paddle turtle. The instructions look like this
when [touching? "t1 "t2] [t2, seth 180 - heading]
when [touching? "t3 "t2] [t2, seth 180 - heading]
T1 and t3 are the paddles. T2 is the puck.
The next step was to get the paddles to move left and right under player control. The original idea was to have each paddle controlled by a different pair of key presses, but instead they boys decided to use the mouse to control one paddle and the left and right arrows for the other. They knew about controlling a turtle with arrow keys. They just removed the code for the up and down arrows from the direct procedure that had been widely used in maze games and came up with
to direct :key
They needed some technical information to implement mouse control of the other paddle: Mousepos reports the position of the mouse pointer as a list of two numbers. The coordinate system is the same as for turtles. Programming a turtle to setpos mousepos many times, and then clicking on the turtle will cause the turtle to follow the mouse pointer. But Brandon and Jerrit wanted the paddle to only move left and right, not all over the screen. Setx first mousepos causes the turtle to follow the horizontal position of the mouse pointer while ignoring the vertical position.
That's pretty much it. The orange and red goal areas are each programmed to increment one of the score sliders. The orange instruction is setscore score + 1. Red is programmed to setscored scored + 1. After hitting a goal area the turtle continues through it to the light blue area beyond and bounces back.
There is a bug here that was never fixed. You may notice that the score goes up in increments of two, not one. If you watch closely, you will see that the score goes up by one as the puck enters the goal color and by one again as it leaves it. Since bouncing is occurring when the puck touches light blue behind the red or orange goal, it actually crosses the goal color twice, once on the way in and once on the way out.
One fix would be to include the code for bouncing in the orange and red instructions. The orange instruction would then be
setscore score + 1 seth 180 - heading
Putting It All Together
There is a startup procedure that runs automatically when the game is loaded:
Presentationmode hides the MicroWorlds tool bars and centers the page on a blank background. The scores are set to 0 and the instructions are displayed. Go starts play.
The first instruction in go activates the paddles and the puck. The command clickon has the same effect as clicking on a turtle with the mouse. The two when commands set up the processes that continually check for collision between the puck, which is t2, and each of the paddles. When contact occurs the instruction t2, seth 180 - heading is run.
1 The math textbook used at Computer School II was Mathematics: A Human Endeavor by Harold Jacobs, W.H Freeman &Co., New York, Third edition, 1994