Until keypressed

Hi all,
Im new in C++ and I wand to create simple ...... snake (game)

I want to continue moving my snake until some key will be pressed. Than change the way how my snake will move. To make it easy, my snake will be "*".

In pascal code will looks like:
...
repeat
x:=x+wayx;
y:=y+wayy;
GoToXY(x,y);
write("*");
clrscr;
until keypressed;
read(way);
...

If you know what I mean, please help me.
To make it easy, my snake will be "*".


That doesn't really make it easy. If you want to make it easy, get a game lib.

See this;

http://www.cplusplus.com/forum/articles/28558/
Could you just tell me how to display some text until key pressed and than if key is pressed display this pressed key?
There is no standard way in C++ to find out if a key was pressed, but there are numerous libraries that provide such functionality.

Btw, that Pseudocode looks suspiciously Pascal-ish. Actually... it IS Pascal.
hanst: he said it was pascal in his post. =P

brotchier: IMO, you shouldn't waste your time trying to do this stuff in the console. Seriously, install SFML. Learn how to do it the right way.

The console is junkier and is actually more difficult and restrictive to work with.
Last edited on
We are trying to lern Pascal and C++ in school and I just wanted to know how to do it....
But if there is no simple way, how you can help me I'll ask on another forum, but than you all for your replay :)
There are ways to do it, but they all involve platform specific functions (like Windows' WinAPI console functions) or additional libraries (ncurses), or nonstandard headers many people might not have (conio). The standard C++ lib doesn't give you a way to do it.

And again -- the main reason I'm not directly answering the question is because the question is flawed. What you are trying to do is something that probably shouldn't be done. You'll pretty much be wasting your time learning how to this in the console.

A snake game is a great beginner project.
A snake game in the console is a terrible one.
We had done it in Pascal. Just wanted to know how to translate it to C++.
It can be in conio.h
Indeed, he did say it was in pascal. Didn't see that :O

Well - like Disch said - you shouldn't do it. It's no easier - actually, probably more likely harder - than going for a more sophisticated solution than attempting to display it in the console, and the results aren't quite as satisfying either. And even if it would might for such a small game, such ad hoc solutions are completely unsuited for anything larger.
Last edited on
Pascal to C:


repeat - do{
x:=x+wayx; - x=x+wayx;
y:=y+wayy; - y=y+wayy;
GoToXY(x,y); - gotoxy(x,y);
write("*"); - printf("*");
clrscr; - clrscr();
until keypressed; - }while(getch());
read(way); - scanf("%x",&way); // where x is the declaration format
Last edited on
except gotoxy, clrscr, and getch are not standard functions.
They are pretty close, though, as they are a staple to old DOS console programming with Borland and Microsoft products.

An iskeypressed() function with timeout for Windows API:
http://www.cplusplus.com/forum/beginner/5619/#msg25047

To get unbuffered input you'll need to set the console mode appropriately with SetConsoleMode(). A very simple example can be found here:
http://www.cplusplus.com/forum/beginner/3523/#msg15435
Note that it is better to set the console mode in one spot and simply switch it back when you are done, and use ReadConsole() or ReadConsoleInput() to get input.

A snake game is a great beginner project.
A snake game in the console is a terrible one.

I learned how to use NCurses by writing a console snake game between classes when I was at the University. It still plays well, too. There's nothing wrong with learning something, even if it is simple.
Topic archived. No new replies allowed.