pong

[Tutorial] 30 Minute Pong

Pong is one of the oldest arcade games there is. Pong was released in 1972 by Atari Games. It’s almost old enough to be my dad. I first played Pong when I was about 8 years old. At that point I was very familiar with NES and SNES games, so I was not very impressed with Pong. I had no historical context and didn’t realize that had it not been for Pong, more or less launching the videogame industry, that my precious NES and SNES might not even exist!

Pong was written by one man named Allan Alcorn, as a training exercise to learn how to make games. I’ve recreated it in GameMaker Studio as a lesson in simple GML programming. I use no drag and drop techniques at all in the recreation. Pong is so simple, that this tutorial can easily be followed in 30 minutes.

You can get the source here, and play a live demo here.

All of the scripting is well commented and should be pretty easy to follow. You’ll notice that each room only has one object. I make an invisible master object and have it initialize everything else where I want it. I do that, because it’s more precise than trying to drag objects around in the room GUI (though that GUI is pretty great if you’re trying to make a platform game).

This is the most complicated piece of code in the entire project. It’s located in the ballControlScript script.

//if the ball hits a paddle, have it bounce off.
//Determine the angle by where the ball hit the paddle.

if(place_meeting(x,y,playerPaddleObject))
{
direction = radtodeg(arctan2(playerPaddleObject.y – playerPaddleObject.y, playerPaddleObject.x – (playerPaddleObject.x – 50)) – arctan2(y – playerPaddleObject.y, x – (playerPaddleObject.x – 50)));
speed++;
audio_play_sound(beep, 10, false);

} else if(place_meeting(x,y,enemyPaddleObject))
{
direction = radtodeg(arctan2(enemyPaddleObject.y – enemyPaddleObject.y, enemyPaddleObject.x – (enemyPaddleObject.x + 50)) – arctan2(y – enemyPaddleObject.y, x – (enemyPaddleObject.x + 50))) + 180;
speed++;
audio_play_sound(beep, 10, false);
}

In the original Pong, the paddle was broken up into 8 sections. Which section the ball hit on the paddle determined what angle it would bounce off at. I was capable of a smoother system. The script above uses some trigonometry tricks to make a triangle between three points. The ball. the center of the paddle and a point 50 units horizontally behind the paddle. I calculated the angle based on that. You can adjust how sharply you can return the ball by making that 50 a smaller number.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *