How I made my first 2D game in C#

Sujay Bhilegaonkar
3 min readNov 15, 2021

Making a 2D game without an established engine is a massive challenge. I wrote all of the graphical code from scratch and attempt to make the game run well since I did it without using a hardware accelerated library like OpenGL.

Hardware accelerated libraries use a computer’s GPU to make graphics run smoothly, but some of the higher end libraries like Vulkan or DirectX take a lot more time to learn.

First, I created my storyboard.

I had to create a setting, a player character, and a goal for the game.

  • Setting- I called my game Vesuvius, after the volcano that erupted and buried Pompeii during ancient Rome. I was inspired by a band piece I played of the same name by Frank Tichelli. You can listen to it on youtube.
  • Player- The player is a Roman legionary in 79 CE.
  • Goal- The legionary fights anthropomorphized lava monsters in Pompeii through several levels, eventually defeating the biggest one as a final boss.

Then, I Started Programming.

For my main methods, I used the same concepts I developed from Pong. However, the foundation had to be constructed stronger in order to support more complexity.

In pong, my rendering engine only had to handle a few objects on the screen at once — a ball and two paddles. In this new game, it had to manage many projectiles on the screen in addition to obstacles and enemies.

In order to improve my code, I structured it to use the four object oriented concepts:

  • Encapsulation- each object has a set of its own methods and objects that only it can access, essentially making it a self contained entity.
  • Abstraction- similar to encapsulation, it is the process of isolating objects and connecting them using interfaces with each other.
  • Inheritance- allows the creation of new types of objects by building on top of old ones, and can streamline flow significantly by transferring relevant information.
  • Polymorphism- an object can be used just like its parent object.

In simple terms, this means that every part of my game was created as an object, which had certain properties. For example, every single projectile on the screen was built on an object called a “particle.” Even though I made different types of particles (bullets, trails, and explosions), they could all be processed the same way. I applied this to every part of my game: all the characters on the screen, including the player and enemies, were a version of a “character” object, and every weapon each held was built on a “weapon” object.

Using object oriented programming helped ease my workflow considerably. I could add levels by adding a line in a text file and could debug my code much easier by creating distinct handlers for each part of my game.

It took me around two weeks to complete this game and, in the process, taught me a lot about how to structure large projects. It also gave me greater insights into programming itself and how to make complex events happen smoothly in real time. I encourage everyone who wishes to learn the basics of game development or object oriented programming to take it upon themselves to make a game like this.

Here are some resources to help create similar projects:

Learn the basics of classes and OOP by looking through this website: https://www.w3schools.com/cs/cs_oop.php. You can apply this to the game you make.

Basic GDI+ in C#: https://www.c-sharpcorner.com/article/gdi-tutorial-for-beginners/.

Learn double buffering and how to make a renderer (this is an old tutorial but explains the concept very well): https://www.youtube.com/watch?v=E2UAWki76cQ

Learn how to create and structure games in C#: https://scottlilly.com/learn-c-by-building-a-simple-rpg-index/

I will write more articles about creating games in the future. Feel free to comment and share your own thoughts and experiences!

--

--

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.