Archived

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

Textris in C - need help and discussion...

This topic is 5006 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

OK here i am, a newbie to this site. I''m Vietnamese, 18. Can''t say my programming is good enough, and this is the first time for me to write games too. The first thing i can think of is making a testris clone, but aside from graphic, i want to write it in a text-based style, so i call it textris. And I don''t want to learn C++ yet, not before i finish this in C, so I took C in and start something. I''ve been trying looking for tutorials on the net and nothing pops out, so i need all your help. And maybe my topic will be helpful to someone who wants to write a testris clone. Will you help us out? OK, here is my problem: I (stupid) just can''t get the user input done. I''ve already written the code to move the bricks, but how could I: - Keep the bricks falling until the ESC key is press - But: when I press the directional button, it will move to the side My design is here: I''m using 3 arrays: - One for the existing bricks that had fallen - One for the falling brick - One to display the movement (by adding those two together) And since it''s pure text, i use 1 for brick and 0 for nothing. For example, a brick should look like this: 1 1 1 or this: 1 1 1 1 1 Thanks, and I will be back with more problem and idea to share. Van su khoi dau nan

Share this post


Link to post
Share on other sites
"Keep the bricks falling until the ESC key is press"
Do you want to game to go paused when esc pressed or do you want it to quit or what?

"But: when I press the directional button, it will move to the side"

Don´t understand what you mean by this..

I can try to help but I´m not right now sure what you want to know, so some more explanation would be nice..

Oh and btw. if you want to show something that need more than one space between chars then use code and /code tags inside [] these, like this (copy paste from your text):


1 1 1 or this: 1 1 1
1 1


Othwerise those empty spaces will be ignored..

[edited by - Mkk on April 3, 2004 7:18:54 AM]

Share this post


Link to post
Share on other sites
Er... thanks a bit for the tip.
I''ll explain it more:
What I mean was I want the bricks to keep falling while the program still wait for user input.
When I use getch() to catch keyboard input, it pauses the game so it''s not the right way. In fact, i don''t know how to keep the game running while i controll the brick.

The method i use to make brick falls is run a check to compare between the ''brick'' array and the ''textris'' array of existing bricks. If the conditions are met, another procedure will transform the ''brick'' array to move it down. I use a while() loop to keep one brick falls until it reach the bottom (isbottom==true).

So what i want is to control the brick while it is still dropping.

By the way, how can i catch the arrow button in C?

Share this post


Link to post
Share on other sites
Ok, it looks like I understood your words pretty wrong first time

Anyway..

kbhit() returns 0 if no key has been pressed so getch() will be
now called only if some key has been pressed and that means that
getch() will always get input immediatelly (your program won´t freeze).


if (kbhit())
{
key = getch();
}


kbhit() is included in conio.h.


Damn, I was wrong about the arrow keys, sorry.. (post edited)


[edited by - Mkk on April 4, 2004 6:37:43 AM]

Share this post


Link to post
Share on other sites
Thanks. In fact, I''ve discovered the solution right after i posted my post. But anyway i can''t get the code for arrow keys, all getch() for arrow keys return 224. Why''s that?
I''m using VS.Net 2002 with WinXP right now (been looking for C++Builder but can''t get it anywhere)... Is it the cause?

I''ve read another topic about making Testris using 3D array. What do you think? How do we use that idea? Let''s all share thoughts!

Share this post


Link to post
Share on other sites
The reason you''re only getting 224 is because the arrows, and a bunch of other keys, doesn''t have ASCII-values, which is what getch() returns. Since you''re running Windows, use GetAsyncKeyState() instead. It''s not blocking, so there''s no need for the if(kbhit()). Check the function out in the platform sdk-docs or msdn (http://msdn.microsoft.com/library).


--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.

Share this post


Link to post
Share on other sites
getch() can be used for key input.

You call getch once to check if its an extended key.( this is why the arrow keys return the same value ).

Then you call getch again straight away to get the specific key info.

Here is a sample program to show you the keys and their values. Just press keys to see their values. You will notice that the arrow keys are: extcode + 'K' for left. extcode + 'M' for right. extcode + 'P' for down. extcode + 'H' for up.

---------------------------------------------------------------
I've modified the code two posts down. Have a look there.
---------------------------------------------------------------
Pete

[edited by - pete_2004 on April 4, 2004 7:52:02 AM]

[edited by - pete_2004 on April 4, 2004 7:54:23 AM]

Share this post


Link to post
Share on other sites
Hmm I already edited my code because I thought I was wrong with my advices about arrow keys but now I tested and atleast for me getch() gives values:

left arrow: 75
up arrow: 72
right arrow: 77
down arrow: 80

Share this post


Link to post
Share on other sites
Yep, those codes are right.
I just ran my program again and noticed the dedicated arrow keys are extended on 0xE0. The numeric keypad keys are extended on 0x0 though. The direction values are K(75) M(77) P(80) H(72) in both cases though. Thats a bit strange. I remember doing this code before and all the arrow keys working on 0x0.

I've modified the program to handle both extexsion codes. You'll notice that when it is run the dedicated arrow keys have a different extension code to the numeric arrow keys. The new code captured the function keys too.

Here it is(..properly formatted : ) )



#include <conio.h>
#include <stdio.h>

void main()
{
bool notQuit = true;
bool extKey = false;
int extCode = 0;

while( notQuit )
{
if( kbhit() )
{
int result = _getch();

//check for extended key

//( 0xE0 signifies an extended key. it may need to

// be 0x00 for some keys )

if( result == 0xE0 )
{
result = _getch(); //get the extended key

extKey = true;
extCode = 0xE0;
}
else if( result == 0x0 )
{
result = _getch();
extKey = true;
extCode = 0x0;
}
else
{
//not extended key so use the result directly

if( result == 27 )
notQuit = false;
}

if( extKey ) printf( "extended key code(0x%X): ",extCode );
extKey = false;
printf("ascii# = %d character = %c\n", result, result );
}


if( notQuit == false )
{
printf("\n\nQuiting program...press any key to end\n");
_getch();
}
}
}






[edited by - pete_2004 on April 4, 2004 7:49:08 AM]

[edited by - pete_2004 on April 4, 2004 7:56:34 AM]

Share this post


Link to post
Share on other sites