| Sacha (2) | |||||||||||
|
In order to make a successful and user- friendly game in C++, we need to remember the following things: First of all, simplicity is the key. Of course, if you are comfortable with the more advanced graphics capabilities of C++, you can go on to make a complex game such as Liero, but for now, the simpler the better. Also, we need to remember that a game has to be the right difficulty- not too easy, not too hard. It needs also to have some sort of reward (e.g a colorful message) when you win, so the user is playing for some reason. A game also needs to have a little more than plain text. For example, you could use a noughts and crosses board, or simply colorful text. When you are comfortable with these concepts, you can go on to actually making the game. If you are not familiar with outputting colored text, I suggest you learn how to do this before trying to make a game. It is actually very easy. First of all, just before you start the main process (before the int main () {), you need to add these lines:
then, before the command to output the text, you set the color with the command: setcolor (x);(replace the (x) with any number e.g. setcolor (7) will come out with the default color, white. Now, on to making the game. First of all, you need to have an idea of what kind of game you want to make. For the purpose of the tutorial, let us make a very simple game, called "Higher or Lower?". The user will be shown a number and asked if the next will be higher or lower. First of all, we need to declare our variables. We should have three unsigned short integers. These should be the first number, the second number, and the overall score. We should then also have a character, which will be the letter the user enters, "H" or "L", higher or lower. We can declare these like this:
Now, in order to fully randomize the numbers which are outputted, we need to add in a few lines of code. These are as follows, with comments to explain which each line does.
Once we have done this, we can start with the interface. First of all, at the top of the program at all times, we should have the score being shown. We also want to have a quick explanation of the game, and then start the game itself:
Then we can start the input sequence.
| |||||||||||
|
|
|||||||||||
| Sacha (2) | |||
If we put this all together, this is the finished project:
| |||
|
|
|||
| yjjlovewjf (1) | |
|
good job! | |
|
|
|
| dolorx (30) | |||||||||||
|
Good game but I had trouble following what the code did until I ran it. I think it would be more clear if you got rid of all of your goto statements and lined up your brackets. For Example: Instead of:
I think this makes it easier to read.
Another thing I notice is that with multiple return statements its hard to follow where the program is being exited at. Try to have one return statement if possible. Sometimes using multiple return statements is more readable but that is another story. Try to reduce redundent code. Example - instead of this
maybe this:
Now your only writing one line a of code every time you want to change your color. I believe by doing these few things it will make your code more readable and writable. Which will make you an even a better programmer. I did a quick refracting of your code and this is what I got.
| |||||||||||
|
Last edited on
|
|||||||||||
| leo (2) | |
it is nice thanx but it is nearly a C code. Classes are important in programming games in C++.
| |
|
|
|