Re-coding Pong in 30 minutes! (Unity/C#)

Let’s remake our own version of the famous Pong game… but in 30 minutes!

This article is also available on Medium.

To celebrate the new year, I’m starting a new series of gamedev videos: the gamedev programming challenges!

Today, for the first episode, I’m going to re-code my own Pong game… but in less than 30 minutes 🙂

For this challenge, I had 3 constraints:

  • I had to use Unity and C#
  • I only had 30 minutes starting from a blank Unity project
  • I couldn’t use any exterior resource or previously written code

What is Pong? (history & rules)

Just so we’re all on the same page, here’s a little bit of context on the game.

Pong is one of these games from the beginning of video games that everybody has heard of once in their life; it’s basically one of the first sport video games ever. Originally released as an arcade game in 1972, the game quickly became famous and got ported to living rooms.

The concept is pretty simple.

Image from: https://variety.com/2018/tv/news/pong-game-show-atari-1202681958/

You have two players that each control a paddle, and each have one halve of the screen. These paddles can only move up or down. The goal, just like in real table tennis, is to hit the ball that’s in the field so it doesn’t get behind you and instead exits by the screen edge opposite your space. If you manage to get the ball out behind your opponent, then you get one point and the ball gets reset in the middle.

Note: here, I’ve made a single-keyboard 2-players game where I gave the left player the W and S keys and the right player the up and down arrow keys to move their paddle 😉

Because of the time-constraint, I decided to adapt the rules a bit.

First, I didn’t actually implement a win/game over: instead of stopping at 11 points like in the original Pong game, my version will keep on running forever.

Also, I’ve put the ball spawning point in the middle of the screen rather than having it go up and down the screen depending on the turn.

Finally, even though it’s not very realistic but because I think it makes it more challenging, I gave the ball a random angle whenever it’s reset… so when it’s re-initialised, it gets a random velocity and you don’t know which way it’s gonna go!

About the implementation

The various elements we see in the scene are created using simple Unity 2D sprites (either squares or circles).

But in addition to the objects that are visible and directly used by the player in the Unity scene (like the paddles, the ball or the net in the middle), I also needed to have some invisible util borders to constrain the ball to the camera view at the top and the bottom, and to easily know if it has exited left or right. To do this, I’ve added some sprites with some specific 2D physics components to turn them into either walls (that the ball bounces off of) or trigger areas (that react to objects entering the zone by executing some custom code to increase the score and respawn the ball).

Because I actually a bit extra-time on my hands at the end of the challenge, I decided to add a basic 3-2-1 countdown at the very beginning so that the game doesn’t start too abruptly. I did this using Unity coroutines to update a UI element – if you’re curious, I’ve discussed this technique of animating Unity UI via C# scripting in another tutorial 😉

To conclude…

This first gamedev challenge was really interesting because it forced me to focus on a little set of features and it allowed me to re-discover some basic Unity concepts like Sprites, 2D Physics with colliders and triggers, a bit of UI and even some coroutines!

I really hope you’ll enjoy this new format – feel free to tell me what you think in the comments down below and share your ideas of gamedev challenges for the next episodes!

Leave a Reply

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