How I learned to code by creating Pong with C#

Sujay Bhilegaonkar
4 min readAug 15, 2021

I started playing Minecraft when I was 9. Back then, I did not know much about computers, but Minecraft and the community surrounding it helped light a spark inside me to begin modding games and eventually creating them.

In this article, I will outline how I started on my programming journey by learning to code video games. It is something easily accessible and fun to do, and gives a definite product at the end that one can be proud of.

There are many ways to begin programming a game. One is to use an existing game engine that handles most of the work for you, allowing you to focus on things other than graphics and physics. Another, which is more challenging but also rewarding, is to make your own. I created one myself for my games, but I still tried some existing engines out. Here are some free engines I tried out that can help you get started.

  • Unreal Engine is a free to use 3D game engine that is used by professionals regularly. It is a powerful engine that can be used to make complex games, but may have a high learning curve.
  • Unity is an engine that, while slightly less powerful than Unreal Engine, is more than enough to learn how to program games while being easier to learn than Unreal.
  • GameMaker Studio 2 is a 2D game engine that makes creating 2D games very simple. It is not as complex as either Unreal Engine or Unity, but is almost as powerful at creating games.

Creating a game without an engine is a harder task.

Trying to create a game with only the tools provided by built in libraries of a programming language can be a fun challenge that teaches you the basics of graphics and physics, as well as concepts of object oriented programming like inheritance and polymorphism. By changing the scale of the game you are trying to make, you can learn more and become a better coder.

Here are some resources to help you learn:

First, I wanted to try my hand at making an easy game that would run and function. To do this, I needed to learn a few concepts:

  • Rendering/Graphics: the concept of graphics is one of the most important when making a top down game. It is what allows the player to actually see the game and play it. GDI+ has a very simple way of painting graphics to the screen.
  • Inputs: Mouse or keyboard inputs are how the player will control the game. Programming inputs to work seamlessly and easily is a big part of making a good game.
  • Logic: How the game works is arguably the most important part of creating one. Without logic, a game is simply a painting on the screen that does not change. You have to determine a goal that the player has to reach, or a puzzle to solve. It has to make the player entertained for the time they played it.

The first game I created was Pong, which I chose because of its simplicity. The inputs are a simple up and down arrow key, which just stores the location of a paddle. The Graphics are simple, just painting two rectangles on a canvas with a ball. The logic, however, can be a bit complicated.

My first Pong created with C#

The ball is meant to bounce between the two paddles and the two top walls of the game, which means that it must register a collision. This can be accomplished by a simple conditional check, and repeated across each collision surface. This includes:

  • The paddles
  • The top walls
  • The side walls which register a defeat

The ball must move between each tick at a certain rate in the main game loop. Although the position of the ball may change at each reflection point, either the horizontal or vertical vector is just being flipped depending on which wall has a collision.

You can see how I wrote the program here: https://pastebin.com/j3tpAYxW

Creating this physics system for the ball is a stepping stone for more complex particle systems in other games, such as the one I will detail in the next article. Once you have the parts together, you have successfully created your first game with nothing but the built in libraries in C#.

In my next article, I will share the process of making a 2D top down leveled shooter in C#.

--

--

Sujay Bhilegaonkar

Dabbler in all things tech, passionate about developing knowledge and skills in computers, programming, data, AI/ML, and networks. Also high school student.