Console Game [C++]

Recommended Posts

I've been learning C++ for about 9 months and I want to start making a good console game. I'm thinking something along the lines of snake but I'm not so sure where to begin. I think I should have a board (2-d array?) with "@" as the walls. I should have a function for updating the board. I'll have "*" as the snake and "+" for the bits of food. Now for my problem. How do I handle user input given that it uses the arrow keys? I'm looking for all sorts of help here, I'll put up some code when I have it!!!

Share on other sites
I think the conio.h functions might help with the input. I found this thread which might be helpful.

Share on other sites
Having some trouble (already, Not Good!)
What I want to do is create a board, char board[20][20]; and fill it entirely.
If it's a border, I want it to be an '@' and everything else, a space.

I don't really want to hard-code it because it won't teach me anything and there should be a much easier way to do it. Is for loops for every single row the only way?

Also, How do I display a full array? Do I have to go through all that with the for loops for every row again?
Thanks!

Also, for readibility, should I use const chars for borders and spaces?

Share on other sites
How are you doing it at the moment?

Share on other sites
It's very easy to implement: use a paper and a pen to figure it out, you won't learn, if you don't try to solve the very first complicated problem you encounter by yourself.

All these indexing/grid/hex-grid stuff are always very hard (work) to implement. You have to go through it for yourself, or you will never be able to do it.

I started to program a hexagonal-grid minesweeper 2 days ago, displaying the grid alone took me 2 hours to figure out. Panning an zooming the grid an other 2 hours (and maybe 5 sheets of A4 paper). Not to mention looping through the "neighbor" fields: about 3 hours. Despite that I had done a lot of grid stuff before this. And even a regular square grid minesweeper.

Share on other sites
I'm not! I just got to populating the borders and I thought there has to be a better way to do this! I was using for loops as in:

for (int i=0; i<19; i++){   board[0][i]='@';  //populate x axis (border)}for (int i=0; i<19; i++)  //populate y axis (border){   board[i][0]='@' <<"\n";  //should I be using endl or is there any difference?}

It's not that I can't do it already, it's just that there should be a way to do it thats faster...

Share on other sites
Quote:
 I thought there has to be a better way to do this!
Better tends to be a meaningless term in programming. Define better. If I gave you an alternative solution, what metrics would you use to determine its quality relative to your current approach?

Quote:
 it's just that there should be a way to do it thats faster...
Faster in what way? (Define faster) And why do you believe what you have is noticeably slow?

Share on other sites
Don't get stuck in these things. If it works, it works, that's the most important. It's meaningless to start optimizing a code that simple (and that is called once in the whole game, since it's just initialization). Who cares if it's not elegant? It will be better next time. That problem has nothing to do with syntax, or the structure of the language or something, it's worthless (= you won't benefit from it) to spend much time with it.

Share on other sites
Ok, Have the board displaying correctly, just needed to think things through first.
I just realised how big this code is going to get, should I learn how to use header files and the like?
I have an idea for the snake's movement, to have a function getting the direction (how?) and then another function to see what will happen if the snake moves there, if it's legal, move the snake and if it's illegal stop the game and show the finished screen with the dead snake's last position. If you understand what I mean, is that how I should do it or is there a better way?
Thanks

Share on other sites
Quote:
 I just realised how big this code is going to get, should I learn how to use header files and the like?
You've been learning C++ for 9 months. You have to confront how C++ handles organization of code at some point.

Quote:
 If you understand what I mean, is that how I should do it or is there a better way?
Maybe there is a better way, maybe there isn't. But the point is that you learn. If we feed you information, you don't learn.

Good point...

Share on other sites
Dude.

Make a game that works. Doesn't matter if it is in a single cpp file at this point. Eventually you'll get to the point where you learn that the code is hard to navigate.

The way that you get better at programming is by sucking at it and making mistakes, and figuring out why what you wrote sucks and learning not to write it that way.

This is called the craft of programming. There is no way to get better at it other than by putting in time.

So write your code. Make it work in whatever way you can. Once you have done that, then show it, and take whatever constructive criticism is given. Don't listen to any of the "you should never do X" or "you should always do Y". The people who say these things are wrong. There are reasons for and against using any particular methodology in a particular circumstance.

Share on other sites
Couple of problems that google's giving me NOTHING on!

1)How can I update the board instead of re-displaying it each time?
2)How can I get the input using the arrows?
3)As for moving the actual snake, I'd probarbly have something like a switch statment but how would I remove the last * in the snake's body and add a new * to the direction specified?

I can think of nothing at all to solve these problems, problem 3 and 1 are the main ones though, I can get by without the arrows...
Thanks!

Share on other sites
Quote:
 As for moving the actual snake, I'd probarbly have something like a switch statment but how would I remove the last * in the snake's body and add a new * to the direction specified?...I can think of nothing at all to solve these problems

What is so impossible in that?
Can't you "write" a value into an array?
Array[x][y] = '*'Array[t][s] = ' ' //or whatever your background char is

googling "input" "win32" whatever did give nothing?
That's impossible.

I'm not sure but I think it's possible to place the cursor on the screen, and update that single character, but not sure about it.
But at first, get it working: clear screen and redisplay.

Share on other sites
Windows? Win32? Console? Input/output? Take a look here.

Share on other sites
Quote:
 Original post by soitsthateasy1)How can I update the board instead of re-displaying it each time?

Unfortunately, the console is not really designed to do what you want to do using standard C++. If you want to create a game using animated ASCII/console text graphics (such as a snake moving across a stationary map), you'll want to learn how to control the specific console you're using directly. For example, if you are using the Windows console, you can get information here:

http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx

There are all kinds of fun things that can be done with the console, if you are so inclined.

Quote:
 2)How can I get the input using the arrows?

See above. You will have to do some digging, but there are ways to read/search the console's input buffer for particular keypress events.

Quote:
 3)As for moving the actual snake, I'd probarbly have something like a switch statment but how would I remove the last * in the snake's body and add a new * to the direction specified?

There are a number of ways you can handle the snake's animation. I personally would not be overly concerned about wasting processing time drawing the map over each frame (or turn). I'd just draw the map, then the snake "on top" of it, letting each segment of the snake keep track of its own location on the game map, and updating those locations when the snake moves (for a snake you may, for example, iterate through the segments backwards, setting the location of each segment to the location of the segment previous to it, until you reach the first segment at which point you update it's location based on the current or user-entered direction).

Share on other sites
Quote:
 There are a number of ways you can handle the snake's animation. I personally would not be overly concerned about wasting processing time drawing the map over each frame (or turn). I'd just draw the map, then the snake "on top" of it, letting each segment of the snake keep track of its own location on the game map, and updating those locations when the snake moves (for a snake you may, for example, iterate through the segments backwards, setting the location of each segment to the location of the segment previous to it, until you reach the first segment at which point you update it's location based on the current or user-entered direction).
It's unnecessary to use a separate array for the snake (and resizing it every time the snake grows) if you have an array already. You just set the value of the array to '*' where the head is, and set to whatever at the tail (How do you know that?). It's really easy to check for self collision too this way.
But hell, we are solving it for you.

Maybe the separate array is a better idea
No! No! Leave it already! Not my problem!

Share on other sites
That would be great but I'm not using Win32.
I'll use system("CLS"); for now because there doesn't seem to be any other options!
I have an idea for moving the snake. It sounds horribly in-efficent though, I'll find out when I have it coded :P
Iterate through the array and find the tail. Find the rest of the snake's body using if statments then delete the tail and move it to two positions up on the snake's body. Then move the snake's head two positions in the direction given. It's a fairly vauge plan but I hope it works! I'll be back in a while with the code!

Also Szecs, I'm really not being guided through this so much that I'm not learning anything. I'm giving most of the ideas with some input from others. Everything in my code is of my own idea and own design, I'm not taking any code from anywhere else. Bear in mind that this is my first actual game apart from "Guess my Number" so it's a new experience for me. I'm not going to learn anything if I'm not guided.

[Edited by - soitsthateasy on February 16, 2010 9:03:17 AM]

Share on other sites
I still have no idea about moving the snake. Any thoughts...

Share on other sites
This may be outside what you're really looking to accomplish (or how you're looking to accomplish it), but have you considered using a 2d graphics API like SDL or SFML?
They have key event handling that's fairly straightforward, rendering calls that would give you the graphical update behavior you're looking for, and it would be a great introduction to using external libraries.

At some point along your game programming track (I assume) you'll reach 3d graphics, and will most definitely need a 3d API to talk to the graphics card. Starting with something lightweight like SFML would be a useful experience.

Of course, if you're aiming at writing *everything* yourself, an API would be considered a cheat. I'm just figuring it could save you many headaches in exchange for the minor one of "learning how to integrate an API into your project".

Share on other sites
The display and the game logic should be kept separate. The screen should be redrawn every time -- use a 2D array to represent the screen and to allow read/writes to the "screen" before displaying it. Try using a data structure to represent the parts of the snake, such as a double-ended queue; it allows for pushing positions at the front (i.e. when the head moves) and popping popping positions at the back (i.e. when the tail moves).

Share on other sites
Quote:
 Original post by soitsthateasyThat would be great but I'm not using Win32.

In that case, what are you developing with?

Share on other sites
@BCulis: I amn't using graphics at all for now but I'll check them out after this project. I'd like to have a good grasp of C++ before I start learning graphics.

@_fastcall: That would work but how could I display that queue inside of the board array?

@TANSTAAFL: Are you talking about the windows.h header file? or my OS? I'm running Vista 32-bit if it makes a difference. I'm not using the windows header file, I'm just using plain old C++ in the console window :)

Share on other sites
Quote:
 Original post by soitsthateasy@_fastcall: That would work but how could I display that queue inside of the board array?

Iterate through the deque and write a character for each position in the deque.

Quote:
 @TANSTAAFL: Are you talking about the windows.h header file? or my OS? I'm running Vista 32-bit if it makes a difference. I'm not using the windows header file, I'm just using plain old C++ in the console window :)

How can I tell {if a key, which key} was pressed before the user presses the ENTER key?
How can I make it so keys pressed by users are not echoed on the screen?
How can I move the cursor around on the screen?
How can I clear the screen? Is there something like clrscr()?
How can I change the colors on the screen?

Quote:
 This is not a standard C++ feature — C++ doesn't even require your system to have a keyboard or a screen. That means every operating system and vendor does it somewhat differently.Please read the documentation that came with your compiler for details on your particular installation.

In which case, would be here.

Share on other sites
Thanks very much! That's exactly what I was looking for!!!
That covers pretty much everything I think! Rated up.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628383
• Total Posts
2982371

• 10
• 9
• 15
• 24
• 11