Tetris clone input

Started by
9 comments, last by Adamb 18 years, 7 months ago
Hi guys, I'm currently working on a Console based Tetris clone using ANSI C++. I'm using cout for display. The problem is this: How can I implement an input system for this? I want to input 1 char per update either for rotation of block or movement. I can't use cin because input has to be entered & then enter pressed, which is no good because I want the game to play fluently. I'll need some kind of stream, one that just accepts 1 char at a time. Any1 got any ideas? Thanks for any help!
Reject the basic asumption of civialisation especially the importance of material possessions
Advertisement
I don't believe ANSI C++ has anything that immediate as far as input goes. You'd probably have to (1) get into the assembly yourself for any/every system you wanted to support, OR (2) use a game/input library for every platform you which to support.

Also, this is a game for a console? Like, a Gameboy, XBox, PS2, or other such device? If that's the case, using true ANSI C++ (in other words no extra libraries) would be a horrible undertaking. Or am I misreading that?
Would using peek() work? If it's EOF, don't try to get a character. I've never tried. I never use cin except in trivial little ways.
I think, but I maybe wrong, that there is a function (cin.get() or cin.getline()) that doesnt need you to hit enter, I might be wrong, but its something to look into.
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery? Forbid it, Almighty God! I know not what course others may take; but as for me, give me liberty or give me death!"~Patrick Henry
Lol Nychold, when I said console I meant a DOS box. Still not sure how to do this. I've tried cin.get(), think getLine() gets a word at a time not a char. Its a pain having to press return everytime I want to enter a move. Any1 else any ideas?
Reject the basic asumption of civialisation especially the importance of material possessions
Heh, I made tetris in a console once too. Try including conio.h, then use the function kbhit() to see if a key is being pressed. Then use getch() to see what key it is. Like this:
if(kbhit()){    char keypress = getch();    if(keypress == 'x')         //User pressed lowercase x}

conio isnt technically a standard library though, but it comes with Visual C++ and Dev-C++, and probably most other compilers. You should definitly try learning SDL or something :)
Thanks Adamb!, that worked great! Its all working now basically! A few minor bugs but shaping up nicely. Next: To add an OpenGL Renderer! Thats gona take a long time, will be fun though!

How did you go about creating your Tetris? I made the playing board a matrix of booleans. If a brick existed on the board the boolean for that position equaled true. And the bricks were made of boolean matrices too. Any better ways to go about it?

Also, in the future i'd like to make my own input. I suppose I would need to create something like the 'MainWindowProc' like in Win32 apps? Is this what one would call a messaging system? I've nothing against SDL but I don't really like using alternative libraries, I don't like not knowing how the code works, silly I know LOL
Reject the basic asumption of civialisation especially the importance of material possessions
Quote:Original post by Cacks
Any better ways to go about it?

http://www.gamedev.net/community/forums/topic.asp?topic_id=325279
Quote:Original post by Cacks
How did you go about creating your Tetris? I made the playing board a matrix of booleans. If a brick existed on the board the boolean for that position equaled true. And the bricks were made of boolean matrices too. Any better ways to go about it?

Also, in the future i'd like to make my own input. I suppose I would need to create something like the 'MainWindowProc' like in Win32 apps? Is this what one would call a messaging system? I've nothing against SDL but I don't really like using alternative libraries, I don't like not knowing how the code works, silly I know LOL


Yeah, that's pretty much how I did it, except instead of bools, it was ints, and the ints stood for what color to draw the pieces. Then there was a struct for the current block, like struct block { x[4], y[4] }; or some such thing. There's probably a better way to do it, but I was a complete noob when I made it, so... :)

About making your own input: SDL is definitly easier, but it's not too difficult to do everything with the windows API, tho it won't be portable. There's a good tutorial here: http://www.winprog.org/tutorial/

Thanks for the help!

Don't suppose there is anyway of colouring chars in the console?
Reject the basic asumption of civialisation especially the importance of material possessions

This topic is closed to new replies.

Advertisement