Staredit Network > Forums > Games > Topic: Windows Console: Code a Game!
Windows Console: Code a Game!
Nov 29 2011, 3:36 am
By: CecilSunkure  

Nov 29 2011, 3:36 am CecilSunkure Post #1



Quote
Cross post from teamliquid.net blogs...

Have you ever wanted to program video games, either as a hobby or profession? I can remember back in High School I'd ponder about the people who would actually develop things like animations, computer games, console games, all sorts of things. The specific section that interested me the most, obviously, was video game development.

For the longest time I always considered people who develop video games far and out of reach. I just sort of assumed they were people who were really rich and had family that got them into that field of profession. I just had no clue how anyone would actually go about developing video games, or how they'd get there in the first place.

So I began to research. What I found is there are three main types of developers (and I included a couple more for accuracy):
  • Programmers: they actually write code that is a translation of the software (game), which computers decode and then execute.
  • Artists: these people create all the art for video games. There are two main sections of art: 2D and 3D.
  • Designers: Game designers are the people that choose what goes where, and design how the actual game should be played and play out.
  • Writers/Sound Engineers - As development teams become larger, roles required to develop a game become more and more specialized. This opens up positions dedicated to things like sound engineering, and writers!
I figured that programming games is what interests me most in life, and so I pursued it and am now studying at DigiPen, a highly specialized school aimed at getting people into video game development. Basically I know of two ways to get into game programming: learning on your own, or with a college degree. Here is a podcast talking about both options, featuring a very experienced developer Ben Ellinger: podcast. This podcast contains a wealth of information that is vital to anyone new to the idea of becoming a game programmer, I highly recommend listening to it!

Once I found out I was accepted to my school of choice I decided to document the steps and progress one undergoes from having no programming experience (almost), to becoming a video game programmer. And so I started my blog: CecilSunkure's Game Programming Blog. I don't believe an in-depth documentation of the process has ever been written before, and as such I hope to have a popular and helpful blog in the future! What better a chance to help people achieve something they'd love to learn than to have access to a wealth of knowledge from a top computer science school?

On various occasions I've tried to search for online sources and articles aimed at beginning programmers who don't know how to take basic knowledge and apply it to actually construct a game. There is tons and tons of information out there on advanced topics in game programming, and many articles on the beginning steps of learning to code, but hardly anything out there is aimed at the middle stage. Currently I feel I'm in that middle stage, which gives me an ideal opportunity to create content aimed at remedying such a deficit!

I've been ravaged with an epic case of busy this last couple months due to transitioning from SC2 to college, but I'm finally getting the hang of managing my time. As such I'm getting up and running with getting time into blog posts again. This is an awesome time for anyone interested in programming to get started, and they can even learn a lot about how to become a professional through the resources my blog provides!

I've started my first post in a series explaining how to construct, in C, a game from scratch using the Windows Console (command prompt) as the platform. The great thing about coding a game in the windows console is that it requires basically no overhead, nothing to download or install (assuming you use windows), and the art requirements are hugely decreased because the only things you can display in a windows console are ASCII characters!


Screenshot of a game I'm developing right now! You can also create something like this!


The reason I've chosen to focus on C is twofold: in the game industry 90% of video games are coded in C/C++. This is because the way C was written gets you as close as one can comfortably be (within reason) to the hardware. This means you have full control over everything, which is absolutely necessary for a real-time software application requiring high optimization. If you're serious about programming video games, I highly suggest learning C. Although I do love other languages like Flash and Python, C needs to be the primary focus for most any professional.


Game concept image by Markham



Concept mockup by Xion


In C the character datatype is a whole number in the range of 0 to 255 (if unsigned). This means that there are in total 256 different ASCII characters to use at your disposal when creating your own game. There's a lot of different tables for displaying all the ASCII characters, but I find this one the best - note that the indices are in hex not decimal:


ASCII table


Constructing the actual game requires a bit of setup in order to get a good looking window up and going. I've written some example code showing how to set up a console window with C (should be compatible all the way back to C89), in which I set the window's title, size, and screen buffer size. This is the template in which you can get started creating your own game. Here is the finalized example code from my blog post:

Code
#include <windows.h> /* for HANDLE type, and console functions */
#include <stdio.h> /* standard input/output */

HANDLE wHnd; /* write (output) handle */
HANDLE rHnd; /* read (input handle */

int main(void)
{
 /* Window size coordinates, be sure to start index at zero! */
 SMALL_RECT windowSize = {0, 0, 69, 34};

 /* A COORD struct for specificying the console's screen buffer dimensions */
 COORD bufferSize = {70, 35};

 /* initialize handles */
 wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
 rHnd = GetStdHandle(STD_INPUT_HANDLE);

 /* Set the console's title */
 SetConsoleTitle("Our shiny new title!");

 /* Set the window size */
 SetConsoleWindowInfo(wHnd, TRUE, &windowSize);

 /* Set the screen's buffer size */
 SetConsoleScreenBufferSize(wHnd, bufferSize);

 getchar();
}


Just copy/paste this code and compile it to an exe, and you'll have a nice executable that creates a window, resizes it, resizes the window's screen buffer, and sets the title! I recommend using a simple compiler like Dev-C++, though I myself am using GCC within Cygwin (which is a giant pain to install for your first time).


The window you constructed!


Feel like you want to actually try writing something to the Windows Console now? Well I've written the second post in this series all about this! Here's the link to the post.


Finished example program of writing chars of random color to a console window.


In this post you can learn all about how to re-create the above image! Here's a code snippet showing the logic behind assigning random values to an off-screen buffer:

Code
for (y = 0; y < HEIGHT; ++y)
 {
   for (x = 0; x < WIDTH; ++x)
   {
     consoleBuffer[x + WIDTH * y].Char.AsciiChar = (unsigned char)219;
     consoleBuffer[x + WIDTH * y].Attributes = rand() % 256;
   }
 }
 
 /* Write our character buffer (a single character currently) to the console buffer */
 WriteConsoleOutputA(wHnd, &consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);


This code is actually pretty interesting if you're new to arrays. If you don't know what an array is, go google it real fast and come back. This code here sets up an array called consoleBuffer, which is single dimensional array. The array has elements WIDTH * HEIGHT, and in order to index the array during a loop as if it were a two dimensional array, you need to use a formula. The idea behind the formula is to figure out what row you want to access, by taking the number of elements in a row and multiplying it by a value. To access the first row, you multiply the WIDTH value by 0. To access the fifth row, you'd multiply WIDTH by five. This works since as the single dimensional array is written to the screen with WriteConsoleOutput, it wraps around the screen buffer once ever WIDTH elements. Then, access a specific element within that row you add in your x value.



I'm extremely excited to get the rest of the posts in the series finished and provide some awesome content for beginning programmers to actually develop their own games! The other posts cover topics such as:Hopefully this blog post will get some of you excited and interested in learning to program, especially learning to program a game. I feel learning some sort of programming language is extremely beneficial in developing as a person in general; after I learned how to think like a programmer it was honestly as if I found a new way of thinking entirely. I encourage anyone interested to get started immediately!

However, what if you're completely new to programming and know really just about nothing? Well, I've written a nice post about getting started with the C language, and I don't assume you know anything! In about an hour you can be well on your way to writing programs that deal with simple mathematical statements, and even print output of those statements onto the screen! Here's an excerpt from the blog post I wrote called I Want to Learn Programming, but I Know Nothing!:

Blog Excerpt



Interesting links to get started:
http://cecilsunkure.blogspot.com/ - Good wealth of resources to start programming, especially games
http://forums.tigsource.com/index.php?topic=14588.0 - Awesome thread on ascii art, with tools to create
http://labs.bp.io/2011/ascii-paint-bpio-fork.zip - My favorite tool to create ascii art
http://www.bloodshed.net/devcpp.html - The compiler I first started using when messing with C
http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/cygwin/part1/ - How to install Cygwin to use the GCC compiler
http://www.crimsoneditor.com/ - Favorite text editor for writing code



None.

Nov 29 2011, 3:48 am ubermctastic Post #2



I'm clueless when it comes to any kind of C coding etc. so I'm going to read that noobies link you got there when I get the time.

Hey, so your first tutorial was awesome, but after that you completely lost me!
I'm reading up on some other sites though, I'm looking forward to getting into arrays, buffers, and all that fun stuff later :]

Post has been edited 1 time(s), last time on Nov 30 2011, 4:11 am by K_A.



None.

Nov 30 2011, 4:45 pm CecilSunkure Post #3



Quote from name:K_A
I'm clueless when it comes to any kind of C coding etc. so I'm going to read that noobies link you got there when I get the time.

Hey, so your first tutorial was awesome, but after that you completely lost me!
I'm reading up on some other sites though, I'm looking forward to getting into arrays, buffers, and all that fun stuff later :]
Wow very cool! Please do request me to cover other topics you'd like, and I'll write one most asaply :)



None.

Nov 30 2011, 9:46 pm lil-Inferno Post #4

Just here for the pie

http://www.gamefromscratch.com/post/2011/08/04/I-want-to-be-a-game-developer.aspx
Well, there ya go for start-up, although the guide really only covers C++, C#, and Java in depth and enumerates resources both free and not free. It mentions Python but not to the extent that it should.




Dec 1 2011, 6:18 pm Riney Post #5

Thigh high affectionado

Game developing does not only included programming the game. Most higher up companies will have a team build an engine and ways to add things to it by including editors and such. Honestly, programming is probably one of the hardest things do, apart from actually writing a good story.

If you want to try developing a game, try using Eclipse, and try playing around with things already made for you, to see if you even CAN making a game.



Riney#6948 on Discord.
Riney on Steam (Steam)
@RineyCat on Twitter

-- Updated as of December 2021 --

Dec 1 2011, 6:27 pm CecilSunkure Post #6



Quote from Riney
Game developing does not only included programming the game. Most higher up companies will have a team build an engine and ways to add things to it by including editors and such. Honestly, programming is probably one of the hardest things do, apart from actually writing a good story.

If you want to try developing a game, try using Eclipse, and try playing around with things already made for you, to see if you even CAN making a game.
No way, do it from scratch ^^



None.

Jan 25 2012, 1:56 am ImagoDeo Post #7



Needz karma.

I've been looking to go into programming once I finish high school and get into college, but I haven't spent the time to go in-depth yet. I'd love to have some experience before I get to college, though. Thanks for the starting point. ^^



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[07:46 am]
RIVE -- :wob:
[2024-4-22. : 6:48 pm]
Ultraviolet -- :wob:
[2024-4-21. : 1:32 pm]
Oh_Man -- I will
[2024-4-20. : 11:29 pm]
Zoan -- Oh_Man
Oh_Man shouted: yeah i'm tryin to go through all the greatest hits and get the runs up on youtube so my senile ass can appreciate them more readily
You should do my Delirus map too; it's a little cocky to say but I still think it's actually just a good game lol
[2024-4-20. : 8:20 pm]
Ultraviolet -- Goons were functioning like stalkers, I think a valk was made into a banshee, all sorts of cool shit
[2024-4-20. : 8:20 pm]
Ultraviolet -- Oh wait, no I saw something else. It was more melee style, and guys were doing warpgate shit and morphing lings into banelings (Infested terran graphics)
[2024-4-20. : 8:18 pm]
Ultraviolet -- Oh_Man
Oh_Man shouted: lol SC2 in SC1: https://youtu.be/pChWu_eRQZI
oh ya I saw that when Armo posted it on Discord, pretty crazy
[2024-4-20. : 8:09 pm]
Vrael -- thats less than half of what I thought I'd need, better figure out how to open SCMDraft on windows 11
[2024-4-20. : 8:09 pm]
Vrael -- woo baby talk about a time crunch
[2024-4-20. : 8:08 pm]
Vrael -- Oh_Man
Oh_Man shouted: yeah i'm tryin to go through all the greatest hits and get the runs up on youtube so my senile ass can appreciate them more readily
so that gives me approximately 27 more years to finish tenebrous before you get to it?
Please log in to shout.


Members Online: jun3hong, Wing Zero