Coding a Retro Game: Learn C Programming by Developing Games (2024)

Learn to code in C and create your own retro games with this free multi-part C programming tutorial series.

But first, why learn to program in C?

Want more context about this series?

Check out the

TL;DR C is one of the most sought-after languages in its own right, it has spawned or inspired lots of other super popular languages, and learning C is a great foundation for programming well in any language.

That’s a lot! Let’s break it down.

Programming languages tend to be grouped in a few ways, but a major way that matters to us in retro programming is where the language sits on a continuum from low to high level.

Coding a Retro Game: Learn C Programming by Developing Games (1)

A low-level language is as close to machine language as possible while still being human readable. Machine language is the zeros and ones that the chips in the computer understand as operations and data. That makes it super fast and efficient – very little waste. In fact, when people say they code in machine language today they often conflate machine language with Assembly language.

Assembly language is the lowest level language you will find in common use today. While back in decades past you would find people punching in hex code into the front panel of a computer, that is mostly an academic or museum activity outside of masoch*stic hobbyists!

Assembly, Compiled, and Interpreted Languages

Coding a Retro Game: Learn C Programming by Developing Games (2)

With Assembly, your text file of mnemonics is converted by the assembler program into machine code, which is then run on the computer. It can also be disassembled back into human readable form (usually with the loss of labels and variable names).

Most arcade-style games in the 1980s were coded at least in part in 6502 or Z80 assembler. There was really no other option if you wanted to get fast, smooth, twitchy action, and have it fit onto a tape or floppy disk.

Compiled languages, such as C and Pascal, are first converted into assembly before then being turned into machine code. This compilation of code from human-readable to machine code means, while not as close to machine code as assembly, is still super efficient and much faster than the easier to learn high level languages.

Obviously these additional steps introduce some friction to the programming process. You have to go through the compilation to know if your changes have the desired result or if you have introduced a bug. This meant that a more interactive and direct approach was desired, leading to interpreted high level languages, such as BASIC and Python.

In the late 1970s and the first half of the 1980s, home computers would usually boot into the BASIC (Beginners’ All-purpose Symbolic Instruction Code) programming environment. BASIC, usually from Microsoft, or heavily influenced-by) was essentially the command-line interface of the operating system. Seconds after switching on you could enter:

print "Hello World"

and the computer would interpret that line of BASIC programming code, and out would be displayed Hello World. Even compiled versions of BASIC still usually maintain a direct command interpreter, for example AMOS for the Commodore Amiga.

Contrast that simplicity with the 6502 assembly version earlier and you will understand why higher level languages are better suited for initial learning and educational environments.

Instant feedback makes for a great learning experience, and BASIC is highly human readable, but all of this convenience comes at the expense of performance and memory utilization, which on those old computers was a real challenge. Today, with super-computers in our pockets, not so much.

Some BASIC implementations, such as the sophisticated BBC BASIC, were very performant, and were capable of running semi-decent action games, but mostly BASIC was either used to learn with, before moving to a lower-level language, or as things progressed, was turned into a compiled language.

Compiling a higher level language adds an enormous performance boost, that can mitigate the slowness, while not eliminating it entirely. The popular Atari ST STOS and Amiga Programming systems AMOS and Blitz Basic used this approach with great results. Check out my Amiga Programming Tutorial series.

Also, high level languages can be augmented by C or even Assembly routines where raw performance is required.

History of the C Programming Language

Coding a Retro Game: Learn C Programming by Developing Games (4)

The history of the DEC PDP-11, C programming language, and the UNIX operating system are intertwined. As Dennis Ritchie explains:

C came into being in the years 1969-1973, in parallel with the early development of the Unix operating system; the most creative period occurred during 1972. Another spate of changes peaked between 1977 and 1979, when portability of the Unix system was being demonstrated. In the middle of this second period, the first widely available description of the language appeared: The C Programming Language, often called the ‘white book’ or ‘K&R’

The Development of the C Language
Dennis M. Ritchie

Brian Kernighan and Dennis Ritchie wrote the book that launched the language, but it was only after it was standardized as the ANSI C that we could say the C programming language. C was heavily inspired by B, created by Ken Thompson, which was itself inspired by BCPL, and ALGOL. Without those languages, the restrictions and abilities of the PDP series, and the computing culture of the time, maybe we wouldn’t have C now.

What about Pascal?

Of course, Pascal was also created in parallel to the development of C, and is also a descendent of ALGOL. While not quite as important today as C is, Pascal is equally proficient and in many ways very similar. Look out for future tutorials covering this awesome language.

Programming Languages Today

C programming language is still used today where performance and efficiency is required. That means you will find C developed code in your operating system, embedded systems (“Arduino” is C programming), and games.

Modern descendants of C, such as C# and Swift, take the best of C and mix in some “quality of life” additions to make it more desirable to use. PHP, Java, JavaScript are obviously heavily inspired by C, even down to using ; to mark the end of a statement.

This means knowing C makes it easier to learn more modern languages, plus surprisingly can make it easier to learn assembly, because to be a fully-formed C game programmer means getting down and dirty all up in the target machine’s memory and registers.

Coding Hello World in C Programming Language

We have seen Hello World in 6502 Assembly and BASIC. What does it take to output Hello World in C?

#include <stdio.h> int main() {printf("Hello C World!\n"); }

While there is definitely more to it, and there seems more decoration with all the brackets, it’s still more readable than the Assembly mnemonics. Don’t worry, we will break down how it works in the next entry of this series.

If you pasted the above code into a text file and compiled it with a C compiler, you could pretty much count on it working. That is because the C language is very cross-platform. While you can directly address the target hardware, you can also code in a way that works across many different computer systems.

Coding a Retro Game: Learn C Programming by Developing Games (5)

That same code, with one slight difference, also works on the Atari 800 and even the PDP-11!

Coding a Retro Game: Learn C Programming by Developing Games (6)
Coding a Retro Game: Learn C Programming by Developing Games (7)
Coding a Retro Game: Learn C Programming by Developing Games (8)

Obviously at this point we are just outputting text; we can mostly pretend graphics hardware and memory locations are not a concern. Thing is, for the longest time text was all we had, but still some great game genres came out of that era that are still played today!

In the next tutorial we will start getting interactive, getting input from the user and processing that input to change our output.

If you can’t wait for the next entries in the series, a great interactive service to practice your C programming is Repl.it plus also check out my Arduino Programming Tutorials to see examples of C programming in action.

Retro Programming in C Series

Part 1: Introduction to C/C++

Part 2: Installing Compilers, and Hello World

Part 3: Keyboard control, conio.h, and Character Movement

See Also: Programming the C64 with Visual Studio Code

See Also: Compiling C programs for the Atari ST

Coding a Retro Game: Learn C Programming by Developing Games (2024)

References

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5732

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.