Archived

This topic is now archived and is closed to further replies.

Console Programming - Tetris

This topic is 5224 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right! Due to my lack of knowledge I have been forced to take a step back to try to make a console Tetris instead of the Win32/OGL one i was trying earlier. Things now seem to proceed fast but i still have a couple of questions about this. First one is about movement. In my Win32/OGL project i was using bool keys[VK_LEFT/VK_RIGHT] for the horizontal movement, all acording to NeHe standards . But now when I try it in console nothing happens. My question is then, Dosn''t VK_LEFT/VK_RIGHT work in console? My second, that is some sort of a thought. I have seen people here suggest the 4x4-array method for making the shapes of the four blocks. I read somewhere that it is a way to dynamically link the blocks together instead. Is there anyone that have made this and if so, can you perhaps post some info about this method? Thanks in advance! ----------------------------------------------------------- ----------------------------------------------------------- "Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr. http://www.gametutorials.com

Share this post


Link to post
Share on other sites
#1 - VK_LEFT/VK_RIGHT are win32 only. They are used in the windows message procedure but, dos does not have one. You would have to implement some form of input. ( Such as getch() or getchar() )

#2 - 1 way to dynamicaly link blocks is with linked lists. Read up on how linked lists work and that should do the trick.

Share this post


Link to post
Share on other sites
The bool keys thing is a concoction of Jeff''s that has nothing to do with console mode. That array gets updated in the generic WinProc code whenever an appropriate message is received (you can look through the source if you don''t believe me). Anyways, you''ll probably want to look into the getch() family of functions for your input. Just remember that this won''t be event driven, but procedural (is that the right terminology?) in nature.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
About the VK_LEFT/RIGHT I suspected something like that. I will try an old code I remember from one of the GameTutorial lessions, and if that dosn''t do the trick i will look into getch().

And about the other question, I have always wondered what linked lists are for. Perhaps now I find the answear to that

Thanks!

Share this post


Link to post
Share on other sites
Heres most of the input function in my ASCII tetris, hopefully this will help you a little ...

void UserInput(void)
{
input = false;

if(_kbhit())
{

bInput = true;

int key = _getch();

if(toupper(key) == 'Q')
{

//blah blah

}
else if(toupper(key) == 'P')
{

//blah blah

}

QueryPerformanceCounter(&previousTicks); // reset the time


}

else if(key == 32) // pressed the spacebar

{
//blah blah

}
else if(key == 224) // it's a cursor key!

{
switch(_getch())
{

case 75:
bLeft = true;
//left

break;
case 77:
bRight = true;
//right

break;
case 72:
bRotCounterClock = true;
//up

break;
case 80:
bRotClock = true;
//down

break;
}
}
}

}



An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on August 6, 2003 12:11:28 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Android_s
And about the other question, I have always wondered what linked lists are for. Perhaps now I find the answear to that



A linked list is where one object contains a pointer to the next object. It is great if you''re going to insert or remove lots of objects in the list, but you can''t jump to any object you want, you always have to go through them in order.

I doubt you''ll need a linked list for tetris, though.

Share this post


Link to post
Share on other sites
As for "dynamically linking blocks together", I don''t see what you mean. If, however, you mean to reserve memory only for the needed blocks (as oposed to a 4x4 array, where you allocate four times as much memory as is really needed), you could simply solve it by placing all the currently falling blocks in a CGroup class or something like that.

If you need some other help about the game logics, you can check out the full source of my Tetris game here. It uses Win32 for input and stuff, and OpenGL for the graphics, but the logics are the same wether you use OpenGL or ASCII graphics. The code is heavily commented, at least if you compare it to what my code usually looks like.


"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"
"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."

Share this post


Link to post
Share on other sites
Thanks for all the help!

I havn''t had mutch time to try all the methods that have been suggested here, but I hope I can sit down and try some tonight.

I still have one question for Tiffany Smith about her Tetris clone. Why is it that you write for example _getch() and _kbhit()? Is it the same as to write getch() and kbhit()?
Hmm, is it possible I can see the code to that Tetris you made? I would like to see how you did that drop-funktion.

And for Valderman. I ain''t sure exactly WHAT i meant by linking them together dynamically, it was just something I read a while ago. The thought is that when you want a speciffic shape, for example one of those straight lines, you could group 4 blocks together in a line. I don''t know if it''s possible without too mutch code, but it would be fun to see how it''s done.

Share this post


Link to post
Share on other sites
quote:
The thought is that when you want a speciffic shape, for example one of those straight lines, you could group 4 blocks together in a line. I don't know if it's possible without too mutch code, but it would be fun to see how it's done.
Check out the CGroup and CBlock classes in the link I posted before, that's one way to do it. Although it's most likely not the best way, it works fine for me.

Oh, here's the link again: Clicky

Oh yeah, the code for rotating the blocks is pretty ugly, if I remember correctly.


"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"
"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately ."


[edited by - Valderman on August 7, 2003 1:02:23 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Android_s
Why is it that you write for example _getch() and _kbhit()? Is it the same as to write getch() and kbhit()?



Yeah, I think they're interchangable.

quote:
Original post by Android_s
Hmm, is it possible I can see the code to that Tetris you made? I would like to see how you did that drop-funktion.



Sure, the link in my sig is now a zip file with the source and project + MIT license for the source.




An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on August 7, 2003 5:09:18 PM]

Share this post


Link to post
Share on other sites
i've made a console tetris back when i was new .. just do something like...

if(GetAsyncKeyState(VK_RIGHT) && !GetAsyncKeyState(VK_LEFT))
// move block right

and change the right/left to move left

on a further note: vk_right/left are just representations of virtuak keycodes, but you still need to include windows.h, you can't expect to get WM_CHAR messages but you can still use them in other functions

Also: for this to work properly it will have to draw frames up and check for new key presses often, however just use do processing/frames with a GetTickCount() and check for keys often

ex:

#define FPS 2
DWORD tick = GetTickCount();
while(true){
//checkforkeys / move block here
if(GetTickCount() - tick > 1000/FPS){
tick = GetTickCount();
//draw frame / move block down / whatever
}
Sleep(20);
}


plus you can make other timers if you want to keep a steady framerate while moving the block down at different speeds, and even a timer to put time between checking for keys


[edited by - drekkar on August 7, 2003 5:37:11 PM]

Share this post


Link to post
Share on other sites
Thanks to you all, again

I tried the _kbget(); and _getch(); functions and it works perfect!

Now I'm trying to get the Collission Detection to work and I think I'm going nuts on it. So, Tiffany and Valderman, is it ok if I check your codes and try to get it from there? I won't copy, promise

EDIT: Oh, Valderman, I checked your code and decided I am way to tired to get all of it. Classes ain't my strongest side...
I'll give it a shot tomorrow to see if I can figure out how you got those blocks together

[edited by - Android_s on August 7, 2003 6:54:31 PM]

Share this post


Link to post
Share on other sites
Look and copy as much as you like, it doesn't matter to me.

EDIT: I actually did this little project as an attempt to organize something more than a test app around classes.

"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"
"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately ."


[edited by - Valderman on August 7, 2003 6:56:49 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Android_s
Thanks to you all, again

I tried the _kbget(); and _getch(); functions and it works perfect!

Now I'm trying to get the Collission Detection to work and I think I'm going nuts on it. So, Tiffany and Valderman, is it ok if I check your codes and try to get it from there? I won't copy, promise



Absolutley, like it says in the license, you may do whatever you wish with the source. (although, if you copy any huge chunks of it, it would be much apprieciated if you give me credit).
Have fun!


An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on August 7, 2003 7:11:59 PM]

Share this post


Link to post
Share on other sites
Heh ValderMan, I''m actualy trying to do that exact same thing.

So far, It sounds like I have come up around with a similar design for the *game* part of the project. (CBlock, CGroup)

Now I must... resist... looking at.. your code... ARGGG

Share this post


Link to post
Share on other sites
Tiffany, I saw the source code for asciiris (nice game btw ) and I noticed that you included some win32api code in there? am i wrong? i'm guessing you used win32api code or windows.h for movement of the blocks?

I'm not sure since I haven't learned any win32 stuff yet, but i was just wondering.

thanks.

[edited by - TheOne1 on August 9, 2003 2:27:35 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by TheOne1
Tiffany, I saw the source code for asciiris (nice game btw ) and I noticed that you included some win32api code in there? am i wrong? i''m guessing you used win32api code or windows.h for movement of the blocks?

I''m not sure since I haven''t learned any win32 stuff yet, but i was just wondering.

thanks.



Hi
Yes you''re right, i used the win32api for drawing the pieces in AsciiRis, but C to get the input to move them.


An ASCII tetris clone... | AsciiRis

Share this post


Link to post
Share on other sites
AFAIK, you pretty much have to use Win32 API calls for this kind of thing, at least with MSVC, since there is no equivalent to the borland gotoxy() function.
If you look up Win32 API console functions, you can easily make your console game double-buffered.


"THREE FOOT TELESCOPING DEMON PENIS"
- Pouya

Share this post


Link to post
Share on other sites
What does double buffer mean? I''m guessing like in Tiffany''s game asciiris, double buffering is when you have kind of like a split screen? In Tiffany''s game, the game area and the menu area?

Share this post


Link to post
Share on other sites
No, double buffering is a technique where each frame... you draw on a invisible backbuffer.... when you finished drawing, you just switch the backbuffer with the frontbuffer so the backbuffer becomes visible.

The frontbuffer is now the backbuffer and vice-versa, so you draw on the new backbuffer for the next frame and switch them again, ad infinitum.

Share this post


Link to post
Share on other sites
For the Process Full Row function i scroll the screen buffer to scroll over the Row that has just been filled.
For the most part rather than double buffering, AsciiRis uses more of a dirty rectangle approach, (It essentially just redraws the part of the screen that needs to be redrawn at any one time, rather than the whole thing).



An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on August 9, 2003 12:13:13 PM]

Share this post


Link to post
Share on other sites
Right, have to blow new life in this old thread.

Tiffany, first I must say your AsciiRis code is very...good looking

I have made my tetris close work partially. Single blocks fall from the top and the collision detection works, although it is the damned ugliest CD-function i think i've ever seen. A quick hack. Starting to get tired of this console tetris...

Now to what confuses me: Your(Tiffany) rotation of the "piece-array". Perhaps I should just ask how one can make that rotation? I can't figure it out just by looking at your code. Is it simply to add one to the first value(array[someValue+1][someLoopValue]) or what!?



[edited by - Android_s on August 18, 2003 8:41:55 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Android_s
Right, have to blow new life in this old thread.


Hi again
quote:

Tiffany, first I must say your AsciiRis code is very...good looking

Thank you
quote:

Now to what confuses me: Your(Tiffany) rotation of the "piece-array". Perhaps I should just ask how one can make that rotation? I can't figure it out just by looking at your code. Is it simply to add one to the first value(array[someValue+1][someLoopValue]) or what!?


The data for the rotations is held in the "piece" array at the top of the program ...

/////////////

//piece data

piece Pieces[7] = { {LTGREEN, 0,0, 0,-1, 0,1, 1,1, 0,0, -1,0, 1,0, 1,-1, 0,0, 0,1, 0,-1, -1,-1,0,0, 1,0, -1,0, -1,1 }, //Lpiece

{RED, 0,0, 0,1, 0,-1, 1,-1, 0,0, 1,0, -1,0, -1,-1, 0,0, 0,-1, 0,1, -1,1, 0,0, -1,0, 1,0, 1,1 }, //backwardLpiece

{PURPLE, 0,0, 1,0, -1,0, 0,-1, 0,0, 0,-1, 0,1, -1,0, 0,0, -1,0, 1,0, 0,1, 0,0, 0,-1, 0,1, 1,0}, //Tpeice

{ORANGE, 0,0, 0,1, -1,0, -1,-1, 0,0, 1,0, 0,1, -1,1, 0,0, 0,1, -1,0, -1,-1, 0,0, 1,0, 0,1, -1,1 }, //zigzag peice

{YELLOW, 0,0, 0,-1, 0,1, 0,2, 0,0, 1,0, -1,0, -2,0, 0,0, 0,-1, 0,1, 0,2, 0,0, 1,0, -1,0, -2,0 }, //straightpeice

{BLUE, 0,0, 0,1, 1,0, 1,1, 0,0, 0,1, 1,0, 1,1, 0,0, 0,1, 1,0, 1,1, 0,0, 0,1, 1,0, 1,1 }}; //squarePiece



Each eight values specifies the X and Y coordinates for the rotations.
For instance, for the 'L' piece the first four numbers in the array "0,0, 0,-1, 0,1, 1,1," draw the 'L' shape, the next set of values for the 'L' piece "0,0, -1,0, 1,0, 1,-1," are the coordinates for the 'L' piece rotated at 90 degrees, and so on so forth.
That's how the rotations are specified, then i simply step through each rotation as the key is being pressed ...
In the "Process Piece" function, i have the following code ...
    
else if (bRotCounterClock)
{
pieceRotation--;
bRotCounterClock = false;

if(pieceRotation < 0)
pieceRotation += ROTATIONCOUNT;
}
else if (bRotClock)
{
pieceRotation++;
bRotClock = false;

if(pieceRotation >= ROTATIONCOUNT)
pieceRotation -= ROTATIONCOUNT;
}


This code simply decrements the piece rotation if the "up" key is pressed (counterclockwise (bRotCounterClock)) and increments the piece rotation if the "down" key is pressed (clockwise(bRotClock)) i test "pieceRotation" against "ROTATIONCOUNT" to make sure that it loops through the four rotations.

I hope all that makes sense.


An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on August 18, 2003 12:47:47 AM]

Share this post


Link to post
Share on other sites