1. Computing & Technology

Discuss in my forum

Programming Games in C - Tutorial 1 Star Empires

By , About.com Guide

1 of 5

Introduction to the Games Programming Tutorials
Programming Games in C - Tutorial 1 Star Empires

This is the first of several games programming Tutorials in C for complete beginners. Instead of concentrating on teaching C then showing example programs they teach C by providing you with complete programs (ie games) in C

Keeping It Simple

The first game in the series is a console (i.e. text based game called Star Empires). Star Empires is a simple game where you have to capture all 10 systems in the Galaxy while stopping your AI opponent doing the same.

You start owning System 0, while your enemy own system 9. The remaining eight systems (1-8) all start neutral. All systems start within a 5 parsec x 5 parsec square so no system is more than 6 parsecs apart. The furthest two points are (0,0) and (4,4). By Pythagoras theorem, the furthest distance apart of any two systems is the square root ((4)2 + (4)2) which is the square root of 32 which is about 5.657.

Please note, this is not the final version and will be amended. Last change: August 21, 2011.

Turn Based & Real-Time

The game is turn based and each turn you give orders to move any number of fleets from any system you own to any other system. If you own more than one system you can order fleets to move from all your systems to the target system. This is done pro rata rounded up so if you own three systems (1,2,3) with 20, 10 and 5 fleets present and you order 10 Fleets to go to system 4 then 6 will go from system 1, 3 from system 2 and 1 from system 3. Each fleet moves 1 parsec per turn.

Each turn lasts 5 seconds though you can alter the speed to speed it up or slow it down by changing the 5 in this line of code to 3 or 7 or whatever you choose. Look for this line of code:

onesec = clock()+(5*CLOCKS_PER_SEC);

C Programming Tutorial

This game has been programmed and assumes that you don't know any C programming. I'll introduce C programming features in this and the next two or three tutorials as they progress. First though you'll need a compiler for Windows. Here are two free ones:

The CC386 article walks you through creating a project. If you install that compiler then all you have to do is load the Hello World program as described, copy and paste the source code over the example, save it and then hit F7 to compile it and run it. Likewise the Visual C++ 2010 article creates a hello world program. Overwrite it and press F7 to build Star Empires., F5 to run it.


On the next page - Making Star Empires Work

©2012 About.com. All rights reserved.

A part of The New York Times Company.