Sign in to follow this  

Game from Scratch ? C++

This topic is 1855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guys, I am back and now I have started learning C++ and know to create codes( But still on basics [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] ) . Can any one of you give me a code for making a game ( like tic tac toe) from scratch in C++. Please also explain it so i could understand it.

Share this post


Link to post
Share on other sites
Just start small. Take ultramailman's post and go from there. Store the state of the tic-tac-toe game in an array, 9 separate variables, or a string you constantly parse. You can output a grid easily using the pipe | and the dash - characters. Input can be as simple as entering a number from 1 to 9 (or 0 to 8) that represents the space you want to take your turn in. Save that and move on to the next turn. Don't let someone select a space that already has an X or O.

Maybe when that is working, create a little "AI" that you can play against. Get creative, make it a 4x4 grid instead of a 3x3. Just start small, work piece by piece, and you will start to see your game.

Share this post


Link to post
Share on other sites
[quote name='Sugavanas' timestamp='1355624564' post='5011136']
But still on basics
[/quote]

If you are still on basics, you can't make a game.
Learn the language first.

Share this post


Link to post
Share on other sites
That depends. A [url="http://www.cplusplus.com/forum/beginner/58540/"]random number guessing game is just a few lines of code[/url] and doesn't require intricate knowledge of a programming language.

Share this post


Link to post
Share on other sites
[quote name='GameCreator' timestamp='1355636004' post='5011178']
That depends. A [url="http://www.cplusplus.com/forum/beginner/58540/"]random number guessing game is just a few lines of code[/url] and doesn't require intricate knowledge of a programming language.
[/quote]

GameCreator has a valid point. My suggestion is to start small. Take GameCreator's advice and create a random number game. After you finish that, take a stab at another little game. You need to build up your skills (which is often overlooked by new programmers).

[quote name='lride' timestamp='1355631162' post='5011165']
[quote name='Sugavanas' timestamp='1355624564' post='5011136']
But still on basics
[/quote]

If you are still on basics, you can't make a game.
Learn the language first.
[/quote]
This is the hard, but very real truth. You can't create a game in a language if you do not know the language. Learn your basics and learn them well.

EDIT: I figured I would answer your original question as well. If you look online quite a few companies have released the source code to their games. Id Software's Doom 3 comes to mind. You can find the source code [url="https://github.com/TTimo/doom3.gpl"]here[/url]. With that being said, be prepared to have it handed to you. The level of coding that goes into games is extremely high. With that being said, however, studying other peoples code is an important part of learning to program. Edited by ByteTroll

Share this post


Link to post
Share on other sites
[quote name='ByteTroll' timestamp='1355650660' post='5011213']
The level of coding that goes into games is extremely high. With that being said, however, studying other peoples code is an important part of learning to program.
[/quote]

I very much agree that studying code is an important factor, but I don't believe it's applicable yet to the OP.
At this stage a good understanding of the language is a priority, especially so with C++ as there are so many ways to write functioning but terribly written C++ code. Source code for finished games might not always reflect the best practices, or might just do some things in a terribly hack-ish way because of time constraints or because something needed to be fixed quickly (I haven't looked at the source code for Doom myself, so I couldn't say whether this applies here).


Getting code for complete games just handed to you is not going to be all too helpful right now, it mostly encourages copy-paste behaviour and unless every technique/pattern/whatever used in the code is thoroughly documented, you probably won't understand the reason why the developer chose to use a certain technique at a certain location, or what the technique is even supposed to do.

The important thing now is to understand how to use the basic paradigms provided by C++ correctly at an elementary level, and how to properly implement problem solutions with these. Learn about the basic OOP principles, learn about what kind of functionality C++ provides to support OOP, learn about what kind of useful stuff the C++ standard library provides, etc. and work your way up starting from a very simple 'hello world' application while applying the concepts you've learned and using the data structures (eg. lists, maps, vectors, queues, stacks, etc.) and functionality you've read about.

Once you have some knowledge of object-oriented design and the tools your language provides it'll become clearer to you how to implement a simple game such as tic-tac-toe yourself.

Share this post


Link to post
Share on other sites
Copy pasting doesnt work in game programming (or programming for that matter), learn your language well then consider game programming, even using an engine like ogre / panda3d would require you to understand your language. Think of it like this, what you are trying to do is make a movie on your own but in French and you can only understand basic french words, despite having all the equipment and resources you cant speak French very well.

Learn the language then move onto the movie,.. I mean game

Share this post


Link to post
Share on other sites
And my suggestion, is to to practice with C++ for at least a year. Once you've coded a few stuff, then go to programming games. I know this is harsh, but it's correct.

You'd need to practice with boolean expressions a lot. Statements like if(!(var && !var2) && (!var3 || var4)). Edited by farmdve

Share this post


Link to post
Share on other sites
Learning a language is important but what is even more so is that you understand different programming principles. Functions, variables, comparisons, operators, containers, loops etc.

I suggest starting with writing small programs that do different things, not just games. Find different ways to manipulate data. Find and solve your own small problems. Try to implement a basic sorting algorithm, that sort of stuff.

When it comes to simple games that doesn't require anything fancy I generally divide the code in to three parts. Input, read user input and handle it accordingly. Update, update the game state accordingly to the game rules. Drawing, draw everything to the screen.

If I want to put together a simple game I just need to figure out how to represent the game state and then fill in these three functions.

Share this post


Link to post
Share on other sites
[quote name='ultramailman' timestamp='1355625500' post='5011139']
To write your own game from scratch, use this:

[CODE]
int main(void)
{
return 0;
}
[/CODE]

Now you will have to add your own code to it, and turn it into a game.
[/quote]

I'm Sorry but this comment made me grin from Ear to Ear lol! As all the others said it's best to start small and move forward. my very first game was a text based CYOA ( Choose Your Own Adventure) , which eventually gave way to a text based RPG, which eventually gave way to a basic platformer, and etc. etc. and I definitely think looking at someone else's code and dissecting it can help but there are some principles you won't understand until you play with them yourself like the game loop . and how to best set up a basic game progress. any how goodluck! Edited by JonathanJ1990

Share this post


Link to post
Share on other sites
start with javascript if you want to learn quick.
otherwise most of time would be spend on fixing syntax errors in c++

Share this post


Link to post
Share on other sites
I really feel like as a beginner vanilla C is going to be more intuitive and straightforward than trying to learn C++ out the door.

I know I'm probably sounding facetious, but frankly, C versus C++ provides scant, almost zero advantage one way or another as either way whether you write C++ or not you're going to be compiling C at the end of the day, and is all up to whether or not you think you need the concept of distinct objects that are protected from access from one another and things like generic code or compile-time code generation, or operator overloading to make a decent program.

Give C a try if you're on the basics, and see if you pick it up faster than C++. If you *need* some feature of C++ at some point later for any reason, then by all means, use C++ if it suits you at that point.

I also laughed uproariously at ultramailman's post. Edited by darkhaven3

Share this post


Link to post
Share on other sites
[quote name='darkhaven3' timestamp='1355834264' post='5012017']
I really feel like as a beginner vanilla C is going to be more intuitive and straightforward than trying to learn C++ out the door.
[/quote]

Honestly, I feel if the OP is learning C++, let them learn C++ without telling them they need to learn regular old C. They're both Turing-complete languages, each with pros and cons. I personally find C++ to be more intuitive than C, but if someone is learning C then I'm not going to suggest switching because of my personal taste. (sorry [img]http://public.gamedev.net//public/style_emoticons/default/wacko.png[/img] I just feel like discussions of which language is better, especially with C/C++, are totally pointless and especially don't help a beginner)

But if you want to learn to program a game like tic-tac-toe, there are a number of tutorials out on the web, and many books you could get that would help you along. I don't know where your skills are, but try searching around the net for beginner c++ lessons, or checking out your local library for books on programming or c++ if you can't afford to buy one. But like everyone else has been saying, be careful about not copy-pasting, or you won't learn anything! When you do find a tutorial or a book or whatever, play around with the code and see what happens; try and make it better, or try and make it do something different. This way you aren't just copy-pasting, you're actually figuring out what the stuff you're typing in actually does. I hope that helps! [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

Share this post


Link to post
Share on other sites
[quote name='Riphath' timestamp='1355836081' post='5012026']They're both Turing-complete languages, each with pros and cons.[/quote]
Absolutely. I might find that using generic programming or templates is by and large leaving the door open for bad programming practice, but I appreciate and accept that you will probably feel that my usage of structs and function pointers is archaic and meaningless. I don't mean to make it sound like I'm saying "C is better than C++" or even "C++ sucks", I am simply sharing my feelings that as a beginner I found the idea of just writing a function and being able to call it a much simpler process than having to worry about whether or not other objects should be allowed to see it.

Of course that is completely arbitrary and up to subjective evaluation by anyone at any point, and by no means do I mean to represent my opinion as fact. I apologize.

[quote]I personally find C++ to be more intuitive than C, but if someone is learning C then I'm not going to suggest switching because of my personal taste. (sorry [img]http://public.gamedev.net//public/style_emoticons/default/wacko.png[/img] I just feel like discussions of which language is better, especially with C/C++, are totally pointless and especially don't help a beginner)[/quote]

Like I said, I meant what I said in an absolutely subjective manner and I did not mean to present my opinion as anything but. I've just always seen C as a more attractive option for game programming as a personal preference, and I am sure I'm not the only person who might feel that way is all.

Share this post


Link to post
Share on other sites
[quote name='darkhaven3' timestamp='1355834264' post='5012017']
Give C a try if you're on the basics, and see if you pick it up faster than C++. If you *need* some feature of C++ at some point later for any reason, then by all means, use C++ if it suits you at that point.
[/quote]

actually i think i can learn c++ fast

Share this post


Link to post
Share on other sites
[quote name='darkhaven3' timestamp='1355838876' post='5012040']
Of course that is completely arbitrary and up to subjective evaluation by anyone at any point, and by no means do I mean to represent my opinion as fact. I apologize.
[/quote]

It's ok! This is a forum, after all. You're allowed to express your opinion. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] It's just my opinion that if you talk about which language is best for beginners to start with to no end, you'll never stop being a beginner unless you actually start just programming!

Share this post


Link to post
Share on other sites
[quote name='darkhaven3' timestamp='1355834264' post='5012017']
I really feel like as a beginner vanilla C is going to be more intuitive and straightforward than trying to learn C++ out the door.
[/quote]I think both suck for a beginner, but if you must use one of them, you should use C++.

As long as you are just dealing with your own code, you don't have to use any of C++'s advanced features, and can adopt them at your own pace. The most important thing for the beginner is that C++ lets you get away with [b]never doing manual memory allocation[/b] if you just use std::vector, std::string, references, etc. everywhere. No news, no deletes, no pointers. You don't have to understand templates (or even classes, really) to use vector as a replacement of array, just like you don't have to understand streams in order to write "std::cout << ".

In C you can't do anything without first learning memory management inside and out, and even then you will constantly be tripping up and mired in debugging something.

Share this post


Link to post
Share on other sites
Sign in to follow this