Home > Archive > Unix Programming > March 2006 > while loop statement
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
while loop statement
|
|
| DaVinci 2006-03-24, 7:07 pm |
| I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,
void ball(int starty,int startx)
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
j = startx;
int ch;
while(1)
{
mvaddstr(i,j,"O");
refresh();
usleep(100000);
i = i + di;
j = j + dj;
if(i >= LINES -1 || i < 0)
{
di = -di;
}
if(j >= COLS -1 || j < 0)
{
dj = -dj;
}
}
}
the question is when I want to invoke the ball()function in main()
function,
I can't go out from the while loop. If I didn't write the while loop
statement
I didn't how to let the ball srcolling all the time.
I had trid to use IPC to make them work,let ball() be invoked by child
process
but not work.
any help is apreciated.
| |
| Pascal Bourguignon 2006-03-24, 7:07 pm |
| "DaVinci" <apple.davinci@gmail.com> writes:
> I am writing a pong game.but met some problem.
> the ball function to control the scrolling ball,
>
> void ball(int starty,int startx)
> {
> int di ,i;
> int dj,j;
> di = 1;
> dj = 1;
> i = starty;
> j = startx;
> int ch;
> while(1)
> {
> mvaddstr(i,j,"O");
> refresh();
> usleep(100000);
> i = i + di;
> j = j + dj;
>
> if(i >= LINES -1 || i < 0)
> {
> di = -di;
> }
> if(j >= COLS -1 || j < 0)
> {
> dj = -dj;
> }
> }
> }
> the question is when I want to invoke the ball()function in main()
> function,
> I can't go out from the while loop. If I didn't write the while loop
> statement
> I didn't how to let the ball srcolling all the time.
>
> I had trid to use IPC to make them work,let ball() be invoked by child
> process
> but not work.
>
> any help is apreciated.
When you want to go out of the while loop?
Let's say you want to go out fo the while loop when the function
go_on() returns 0. Then write:
while(!go_on()){
...
}
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| DaVinci 2006-03-25, 7:01 pm |
|
Pascal Bourguignon wrote:
> "DaVinci" <apple.davinci@gmail.com> writes:
>
>
> When you want to go out of the while loop?
>
> Let's say you want to go out fo the while loop when the function
> go_on() returns 0. Then write:
>
> while(!go_on()){
> ...
> }
>
>
the problem is that I can't make the ball run all the time no matter I
input a character or not.
I want it jump out from the loop for catch what I input to control the
board where to move.
Maybe I didn't speak it cleary.
anyway,could you give a very simple codes for how to write the
programme.
as simple as possible
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> There is no worse tyranny than to force a man to pay for what he does not
> want merely because you think it would be good for him. -- Robert Heinlein
| |
| Pascal Bourguignon 2006-03-25, 7:01 pm |
| "DaVinci" <apple.davinci@gmail.com> writes:
> Pascal Bourguignon wrote:
> the problem is that I can't make the ball run all the time no matter I
> input a character or not.
> I want it jump out from the loop for catch what I input to control the
> board where to move.
> Maybe I didn't speak it cleary.
Indeed.
> anyway,could you give a very simple codes for how to write the
> programme.
> as simple as possible
Well, I gather you want to exit from the loop when the user types
something at the terminal.
If this is the case, then you should realize that terminals are
normally configured so as to buffer input lines: the programs gets the
characters only when the user types RETURN.
Now, if you want to exit the while loop only when the user types
RETURN, you can simply check whether there's some input waiting to be
read from stdin. You can use select(2) or poll(2).
Otherwise, you'll have to configure the terminal to send the
characters as soon as they're typed by the user. See: termios(3)
then use select(2) or poll(2) as above.
int go_on(void){
struct timeval timeout={0,0};
fd_set set;
FD_ZERO(&set);
FD_SET(0,&set);
switch(select(1,&set,0,0,&timeout)){
case 1: /* one selector ready for reading */ return(0);
case 0: /* no selector ready for reading */ return(1);
case -1: perror("select stdin"); exit (1);
default: /* should not occur. we'll stop. */ return(1); }}
--
__Pascal Bourguignon__ http://www.informatimago.com/
The rule for today:
Touch my tail, I shred your hand.
New rule tomorrow.
| |
| DaVinci 2006-03-25, 7:01 pm |
|
Pascal Bourguignon wrote:
> "DaVinci" <apple.davinci@gmail.com> writes:
>
> Indeed.
>
>
>
>
> Well, I gather you want to exit from the loop when the user types
> something at the terminal.
>
> If this is the case, then you should realize that terminals are
> normally configured so as to buffer input lines: the programs gets the
> characters only when the user types RETURN.
>
> Now, if you want to exit the while loop only when the user types
> RETURN, you can simply check whether there's some input waiting to be
> read from stdin. You can use select(2) or poll(2).
raw() or cbreak()can do it.
> Otherwise, you'll have to configure the terminal to send the
> characters as soon as they're typed by the user. See: termios(3)
> then use select(2) or poll(2) as above.
>
>
>
> int go_on(void){
> struct timeval timeout={0,0};
> fd_set set;
> FD_ZERO(&set);
> FD_SET(0,&set);
> switch(select(1,&set,0,0,&timeout)){
> case 1: /* one selector ready for reading */ return(0);
> case 0: /* no selector ready for reading */ return(1);
> case -1: perror("select stdin"); exit (1);
> default: /* should not occur. we'll stop. */ return(1); }}
>
>
> --
I think I have solove my problem .I ignore one important function.
nodelay(WINDOW* win,true),which will not wait for one
character--getch() return ERR if I didn't type anything yet.
so in the main()
for(ch = getch(); ch!='q' ;ch = getch() )
{
moveBallOneStep();//only one step not exist a while loop
switch(ch)
{
case 'j':
moveBoardUp();
case 'k':
moveBoardDown();
...
}
}
> __Pascal Bourguignon__ http://www.informatimago.com/
> The rule for today:
> Touch my tail, I shred your hand.
> New rule tomorrow.
|
|
|
|
|