Console Game [C++]

Started by
26 comments, last by BCullis 14 years, 2 months ago
Good point...
Going to learn about header files right now.
Advertisement
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.

Get off my lawn!

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!
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] = ' ' //or whatever your background char is</pre><br>googling "input" "win32" whatever did give nothing?<br>That's impossible.<br><br>I'm not sure but I think it's possible to place the cursor &#111;n the screen, and update that single character, but not sure about it.<br>But at first, get it working: clear screen and redisplay.
Windows? Win32? Console? Input/output? Take a look here.
Quote:Original post by soitsthateasy
1)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).
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!
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]
I still have no idea about moving the snake. Any thoughts...
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".

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement