This week’s assignment was to recreate the game pong. We got the assignment right around the time of the second republican primary debate, so I thought it would be fun to make a game that reflected the debate.

Give it a shot here:

I ran into a number of issues. First, I was trying to use this shortcode to declare variables:

var trump = {

However, it wasn’t working for me. It took me a long time to realize that it was because I was later initializing the trump variable itself as the image. In order for it to work, I needed to create a new variable trump.img to take the image file. This was really confusing at first because I thought it was strange that I could use a new variable without having to first declare it with the other variables.

Another major issue was that trump was getting stuck on top of jeb and bouncing back and forth. I was originally just reversing the direction of the ball with trump.speedx = trump.speedx * -1.

What I really needed was for trump’s X value to be positive every time it hit jeb, and to be negative every time it hit rubio. So, I had to use the absolute value function, like so: trump.speedx = Math.abs(trump.speedx).

Also, I originally was using CONTROL and ALT keys for the jeb paddle. But when two players began to play, this would activate Mac shortcuts. For example Option + Right Arrow switches desktops. So, I changed the jeb paddle to A and Z.

Finally, I started getting “false positives” on the winners’ loops. It would appear that someone had won, and the announcement would flash on screen, then trump would bounce back in to play. In order to keep this from happening, I used the noLoop function to freeze play once either of the winner’s loop conditions were met.

There are still a number of things I’d like to work on. Mostly I’d like to introduce radians to the paddles. Right now trump always bounces off at the same angle that he comes in on. It’d be much more difficult if the angles were randomized or varied according to the contact point on the paddle. For now, I’m just speeding up trump and slowing down jeb and rubio to make it a little more interesting.

Play it here. And here’s the code: