pong game,while loop statement solution

Discussion in 'C++' started by DaVinci, Mar 24, 2006.

  1. DaVinci

    DaVinci Guest

    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.
     
    DaVinci, Mar 24, 2006
    #1
    1. Advertising

  2. DaVinci

    TB Guest

    DaVinci skrev:
    > 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.
    >


    Simple game loop:

    while( ! Game over () ) {
    Read player input ();
    Move the ball ();
    Draw next frame ();
    }

    --
    TB @ SWEDEN
     
    TB, Mar 24, 2006
    #2
    1. Advertising

  3. DaVinci

    loufoque Guest

    DaVinci wrote :

    > mvaddstr(i,j,"O");


    a ncurses-based pong !
    Wouldn't using a video framebuffer or an opengl context be better ?
     
    loufoque, Mar 24, 2006
    #3
  4. DaVinci

    Luke Meyers Guest

    DaVinci wrote:
    > I am writing a pong game.but met some problem.
    > the ball function to control the scrolling ball,
    >
    > void ball(int starty,int startx)


    This is going to sound odd, but I think the biggest problem in your
    program is right here. Functions *do* things, so it's most natural to
    name them with verbs or verb phrases. "ball" is a noun, not a verb
    (quit snickering, all you grammatical smart-alecks). Reading the
    function name, it's difficult for me -- and evidently for you -- to
    determine in any clear fashion what behavior this function is
    responsible for.

    I know, I know -- you're asking about looping, and I'm harping on about
    naming conventions. But work with me a moment -- what is it that you
    want this function to do? If you were going to rename it to a verb or
    verb phrase, what would you call it? For example, would it be
    moveBallOneStep()? Or perhaps moveBallForever()? Do you see how each
    of these names carries an obvious implication with regard to the
    looping logic? moveBallOneStep() obviously wants to just do one thing
    once, each time it's called, and not contain a loop. So, you would put
    the call to moveBallOneStep() *inside* a loop, rather than the loop
    inside the function.

    On the other hand, if you're going to moveBallForever(), then of course
    you'll need a loop similar to the one you've written. The thing with
    endless loops, though, is that they really hog the flow of control.
    main() doesn't get a chance to do anything as long as you're inside the
    body of moveBallForever() (or any other function called from main()).
    If you stay in that body indefinitely, the flow of control will never
    return to main(). Of course, you could always return after some
    condition is reached, e.g. moveBallUntilGameEnds().

    These aren't wonderful function names or anything, by the way -- just
    what I chose for purposes of illustration. They are, however, verb
    phrases.

    > {
    > int di ,i;
    > int dj,j;
    > di = 1;
    > dj = 1;
    > i = starty;
    > j = startx;
    > int ch;


    No need to declare everything at the top like this -- declare things as
    locally as possible, generally speaking. Also, save some vertical
    space by initializing things as you declare them, rather than declaring
    and then assigning. This is a good habit for efficiency, too, when you
    come to work with more complex objects.

    int i = starty;
    int j = startx;

    // (inside loop body)
    int const di = 1;
    int const dj = 1;

    Looks like you're not using ch, so remove it.

    > I had trid to use IPC to make them work,let ball() be invoked by child
    > process
    > but not work.


    Egad... that's way more convoluted. You need to understand the basic
    event loop paradigm; it's very common. Concurrent programming via
    mechanisms such as IPC is worlds more complex -- get solid on the
    fundamentals first!

    Luke
     
    Luke Meyers, Mar 25, 2006
    #4
  5. DaVinci

    DaVinci Guest

    TB 写é“:

    > DaVinci skrev:
    > > 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.
    > >

    >
    > Simple game loop:
    >
    > while( ! Game over () ) {
    > Read player input ();
    > Move the ball ();
    > Draw next frame ();
    > }
    >

    I had tried that.
    I change the while loop to moveBallOneStep()
    it looks like this:
    But the ball can move only one step when I input one character.
    If I didn't input anything,the ball will not move anymore.
    what I want is the ball will move all the time,at least it looks like
    moving all the time.

    If using the while loop moveBallForever() I can't jmp out of the loop
    all the same.


    > --
    > TB @ SWEDEN
     
    DaVinci, Mar 25, 2006
    #5
  6. DaVinci

    DaVinci Guest

    Luke Meyers 写é“:

    > DaVinci wrote:
    > > I am writing a pong game.but met some problem.
    > > the ball function to control the scrolling ball,
    > >
    > > void ball(int starty,int startx)

    >
    > This is going to sound odd, but I think the biggest problem in your
    > program is right here. Functions *do* things, so it's most natural to
    > name them with verbs or verb phrases. "ball" is a noun, not a verb
    > (quit snickering, all you grammatical smart-alecks). Reading the
    > function name, it's difficult for me -- and evidently for you -- to
    > determine in any clear fashion what behavior this function is
    > responsible for.
    >
    > I know, I know -- you're asking about looping, and I'm harping on about
    > naming conventions. But work with me a moment -- what is it that you
    > want this function to do? If you were going to rename it to a verb or
    > verb phrase, what would you call it? For example, would it be
    > moveBallOneStep()? Or perhaps moveBallForever()? Do you see how each
    > of these names carries an obvious implication with regard to the
    > looping logic? moveBallOneStep() obviously wants to just do one thing
    > once, each time it's called, and not contain a loop. So, you would put
    > the call to moveBallOneStep() *inside* a loop, rather than the loop
    > inside the function.
    >
    > On the other hand, if you're going to moveBallForever(), then of course
    > you'll need a loop similar to the one you've written. The thing with
    > endless loops, though, is that they really hog the flow of control.
    > main() doesn't get a chance to do anything as long as you're inside the
    > body of moveBallForever() (or any other function called from main()).
    > If you stay in that body indefinitely, the flow of control will never
    > return to main(). Of course, you could always return after some
    > condition is reached, e.g. moveBallUntilGameEnds().
    >
    > These aren't wonderful function names or anything, by the way -- just
    > what I chose for purposes of illustration. They are, however, verb
    > phrases.
    >
    > > {
    > > int di ,i;
    > > int dj,j;
    > > di = 1;
    > > dj = 1;
    > > i = starty;
    > > j = startx;
    > > int ch;

    >
    > No need to declare everything at the top like this -- declare things as
    > locally as possible, generally speaking. Also, save some vertical
    > space by initializing things as you declare them, rather than declaring
    > and then assigning. This is a good habit for efficiency, too, when you
    > come to work with more complex objects.
    >
    > int i = starty;
    > int j = startx;
    >
    > // (inside loop body)
    > int const di = 1;
    > int const dj = 1;
    >

    I will chang the variable di,and di
    di = -di;
    dj = -dj;

    so I can't use const
    > Looks like you're not using ch, so remove it.
    >
    > > I had trid to use IPC to make them work,let ball() be invoked by child
    > > process
    > > but not work.

    >
    > Egad... that's way more convoluted. You need to understand the basic
    > event loop paradigm; it's very common. Concurrent programming via
    > mechanisms such as IPC is worlds more complex -- get solid on the
    > fundamentals first!
    >
    > Luke
     
    DaVinci, Mar 25, 2006
    #6
  7. DaVinci

    DaVinci Guest

    loufoque wrote:
    > DaVinci wrote :
    >
    > > mvaddstr(i,j,"O");

    >
    > a ncurses-based pong !
    > Wouldn't using a video framebuffer or an opengl context be better ?

    NCURSES is more easy to use.
     
    DaVinci, Mar 26, 2006
    #7
  8. DaVinci

    DaVinci Guest

    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();
    ...
    }

    }
     
    DaVinci, Mar 26, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,287
    Tim Slattery
    Mar 30, 2005
  2. JariTapio

    a Multiplayer Pong, hmmm...

    JariTapio, May 8, 2005, in forum: Java
    Replies:
    3
    Views:
    830
    Knute Johnson
    May 9, 2005
  3. Dr_Locke_Z2A

    open source pong or equally simple game

    Dr_Locke_Z2A, Dec 22, 2005, in forum: C Programming
    Replies:
    4
    Views:
    461
    Chuck F.
    Dec 23, 2005
  4. Uday Bidkar
    Replies:
    4
    Views:
    521
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    443
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page