How does source code turn into PC games?

2.15K views

I understand that game developers type thousands of lines of code for a single game such as any version of a game anyone of us plays every day. How could their source code stored in many files of the game can be executed through a single application and I can play the game itself? What is their code’s journey to our PCs?

In: Technology

9 Answers

Anonymous 0 Comments

Explaining this fully in an easy to understand way is tricky, as games are fairly complex as far as going from 1’s and 0’s inside the computer to the game you see on screen. I’ll try instead to describe the sort of layered complexity (called ‘abstraction’) used when developing a game or any other program, as I think it helps develop an intuition for how things are put together.

So at the bottom level you have the machine hardware and the code that runs directly on it. This is in binary and how it works is hard-wired into the computer. The general idea is that you have numbers that represent some value (like memory addresses or literal numbers or whatever) and instructions. For example, in the old 8085 processors there’s an instruction called ADD C, which takes two numbers that are in the processor already and adds them together. It’s hard-wired into the processor so it happens when it sees the code 10000001. This is the first layer of abstraction. We can write down in human-readable language (called assembly code) the instruction ADD C. We can also write a program that converts that string of text into the computer code 10000001. For assembly language this is called an “assembler”, though for higher level languages we call such a thing a “compiler” or “interpreter” depending on exactly how it works.

Writing a game in assembly is tough (though it can and is done) because you’re dealing with a lot of very basic level stuff like manually moving around stuff in memory. So we can instead think of a language that deals with actual logic you might want describe. These are the high level languages like C++, Python, etc. This is the second layer of abstraction. We can do what we did before and devise a way of naming variables like “player_health” and then a compiler that turns that variable into a memory address in assembly code. So you write a line like “player_health = 100”, which makes a lot of sense to a human and the compiler then goes something like “LDL 100, STA (some memory address)” and that assembly gets turned into binary.

Often for making games we add another layer of abstraction: the engine. An engine is itself a program that has tons of built in code to help developers easily create the kinds of logic and objects used in games. It often has toolbars and graphical objects built into a window that allow the user to quickly create a bunch of reusable code. This code is generally in some higher level language like the ones I talked about in the above paragraph, which get turned into assembly, and hopefully you get the idea of what’s going on here. At the level of the engine this stuff gets pretty intuitive for people. Some engines you can just drag an object onto a screen and fill out a name for it and some basic properties you want it to have and the engine does a huge chunk of the coding for you. When coding it’s similar. You’ll often see code like “new player node_3D {a list of properties here}” which is code that the engine uses to create a new player object with the list of properties inside of the {}. If you’d like to see how people do this, you can look up a tutorial video on Youtube for popular engines like Unity, Unreal, or Godot and just watch someone create a basic game. Even if you don’t fully understand what’s going on you can get the gist of it.

If you have a lot of time to kill and want to learn about the basics of how a computer functions at the hardware/assembly level there’s [this playlist](https://www.youtube.com/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU) by the awesome dude Ben Eater demonstrating how to build a basic 8-bit computer from scratch.

EDIT: I just remembered a cool video you can [find here](https://www.youtube.com/watch?v=Kalmryn9_sE). It demonstrates a few basic ideas for writing a game directly in assembly code.

You are viewing 1 out of 9 answers, click here to view all answers.