Pong - Simple question

This is a discussion on Pong - Simple question within the Game Programming forums, part of the General Programming Boards category; I am trying to write a text based Pong game (I know this sounds very stupid, but...) Here's what I ...

  1. #1
    Akilla
    Guest

    Pong - Simple question

    I am trying to write a text based Pong game (I know this sounds very stupid, but...)

    Here's what I came up with so far :

    PHP Code:
    #include <stdio.h>
    #include <conio.h>

    struct gamecomponent
    {
        
    int bufxbufy;
        
    int oldxoldy;
        
    int newxnewy;
        
    int speed;
        
    void resetcoords();
    ballpaddle;

    void gamecomponent :: resetcoords()
    {
        
    oldx bufx;
        
    oldy bufy;
        
    bufx newx;
        
    bufy newy;
    }

    void changecoordball()
    {
        
    int xaddyadd;

        if (
    ball.newx >= 80)
        {
            
    xadd = -ball.speed;
        }

        if (
    ball.newx <= 0)
        {
            
    xadd ball.speed;
        }

        if (
    ball.newy >= 25)
        {
            
    yadd = -ball.speed;
        }

        if (
    ball.newy <= 0)
        {
            
    yadd ball.speed;
        }

        
    ball.newx ball.newx xadd;
        
    ball.newy ball.newy yadd;
    }

    void changecoordpaddle(int direction)
    {
        
    int xadd;
        
        if (
    direction == 1)
        {
            
    xadd ball.speed;
        }
        else
        {
            
    xadd = -ball.speed;
        }
    }    

    void initialize()
    {
        
    ball.bufx 1;
        
    ball.bufy 1;
        
    ball.newx 1;
        
    ball.newy 1;
        
    ball.oldx 1;
        
    ball.oldy 1;
        
    ball.speed 1;

        
    paddle.bufx 1;
        
    paddle.bufy 25;
        
    paddle.newx 1;
        
    paddle.newy 25;
        
    paddle.oldx 1;
        
    paddle.oldy 25;
        
    paddle.speed 1;
    }

    void placeball()
    {
        
    gotoxy(ball.oldxball.oldy);
        
    printf(" ");

        
    gotoxy(ball.newxball.newy);
        
    printf("*");
    }

    void placepaddle()
    {
        
    gotoxy(paddle.oldxpaddle.oldy)
        
    printf("       ");
        
        
    gotoxy(paddle.newxpaddle.newy)
        
    printf("=======");
    }

    int checkbounce()
    {
        
    int bounceval 1;

        if (
    ball.newy == paddle.newy)
        {
            if (
    ball.newx paddle.newx || ball.newx > (paddle.newx 7))
            {
                
    bounceval 0;
            }
        }
        
        return 
    bounceval;
    }
        

    main()
    {
        
    clrscr();
        
    initialize();

        while (!
    kbhit())
        {
        }

    My question is :

    How do I write a function in the main() which does the
    following :

    1. Run checkbounce function (when an 'if' condition occurs) I know how to do this one.

    2. Run changecoordball() function every 1/100th of a second

    3. Run Placeball function

    4. Sense the input from the user (arrow keys) and
    pass 1 to the changecoordpaddle() function when he/she presses right arrow key and 0, if he/she presses left arrow key

    5. Run placepaddle function

    I want all these 5 steps to be taken care of by 1 function.

    Can anyone help me out ? I am new to this.

    HOMEPAGE @ www.akilla.tk

  2. #2
    Akilla
    Guest

    oh

    oh, and also, changecoordpaddle function should be:
    PHP Code:
    void changecoordpaddle(int direction)
    {
        
    int delx;
        
        if (
    direction == 1)
        {
            
    delx paddle.speed;
        }
        else
        {
            
    delx = -paddle.speed;
        }
        
        
    paddle.newx paddle.newx delx;

    Also, the 5 steps function should also run
    ball.resetcoords() and paddle.resetcoords()
    So there is a total of 7 things that the function should do.

    Please help me as to how I can write this function.

    HOMEPAGE @ www.akilla.tk

  3. #3
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    k i'll try to help you out since no one else has tried...

    1. you already know how to do...

    2. maybe sleep function sleep(time in milliseconds);

    3. just run it (not sure what the problem is?)

    4. check out the link in my sig for that one look at the code for dungeons of moria, more specifically look at my_getch() function (in .c file) and look at keys.h file.


    try these for now and if you need more help post your questions in this thread, i'll try to help you out the best i can.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  4. #4
    Akilla
    Guest

    here..

    This is the latest code:
    PHP Code:
    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>

    struct gamecomponent
    {
        
    int bufxbufy;
        
    int oldxoldy;
        
    int newxnewy;
        
    int xspeedyspeed;
        
    void resetcoords();
    ballpaddle;

    void gamecomponent :: resetcoords()
    {
        
    oldx bufx;
        
    oldy bufy;
        
    bufx newx;
        
    bufy newy;
    }

    void changecoordball()
    {
        if (
    ball.newy == paddle.newy)
        {
            if (
    ball.newx >= paddle.newx && ball.newx <= (paddle.newx 3))
            {
                if (
    ball.xspeed 0)
                {
                    
    ball.xspeed = -ball.xspeed;
                }
                
                if (
    ball.yspeed 0)
                {
                    
    ball.yspeed = -ball.yspeed;
                }
            }

            if (
    ball.newx > (paddle.newx 3) && ball.newx <= (paddle.newx 7))
            {
                if (
    ball.xspeed 0)
                {
                    
    ball.xspeed = -ball.xspeed;
                }
                
                if (
    ball.yspeed 0)
                {
                    
    ball.yspeed = -ball.yspeed;
                }
            }
        }

        if (
    ball.newx >= 80)
        {
            if (
    ball.xspeed 0)
            {
                
    ball.xspeed = -ball.xspeed;
            }
        }

        if (
    ball.newx <= 0)
        {
            if (
    ball.xspeed 0)
            {
                
    ball.xspeed = -ball.xspeed;
            }
        }

        if (
    ball.newy >= 25)
        {
            if (
    ball.yspeed 0)
            {
                
    ball.yspeed = -ball.yspeed;
            }
        }

        if (
    ball.newy <= 0)
        {
            if (
    ball.yspeed 0)
            {
                
    ball.yspeed = -ball.yspeed;
            }
        }

        
    ball.newx ball.newx ball.xspeed;
        
    ball.newy ball.newy ball.yspeed;
        
    }

    void changecoordpaddle(int direction)
    {
        
    int delx;

        if (
    direction == 1)
        {
            
    delx paddle.xspeed;
        }
        else
        {
            
    delx = -paddle.xspeed;
        }

        
    paddle.newx paddle.newx delx;
    }

    void initialize()
    {
        
    ball.bufx 1;
        
    ball.bufy 1;
        
    ball.newx 1;
        
    ball.newy 1;
        
    ball.oldx 1;
        
    ball.oldy 1;
        
    ball.xspeed 1;
        
    ball.yspeed 1;

        
    paddle.bufx 1;
        
    paddle.bufy 25;
        
    paddle.newx 1;
        
    paddle.newy 25;
        
    paddle.oldx 1;
        
    paddle.oldy 25;
        
    paddle.xspeed 1;
        
    paddle.yspeed 1;
    }

    void placeball()
    {
        
    gotoxy(ball.oldxball.oldy);
        
    printf(" ");
        
    gotoxy(ball.newxball.newy);
        
    printf("*");
    }

    void placepaddle()
    {
        
    gotoxy(paddle.oldxpaddle.oldy);
        
    printf("       ");
        
    gotoxy(paddle.newxpaddle.newy);
        
    printf("=======");
    }

    int checkbounce()
    {
        
    int bounceval 1;

        if (
    ball.newy == paddle.newy)
        {
            if (
    ball.newx paddle.newx || ball.newx > (paddle.newx 7))
            {
                
    bounceval 0;
            }
        }

        return 
    bounceval;
    }


    main()
    {
        
    clrscr();
        
    initialize();

        while (
    == 1)
        {
            
    changecoordball();
            
    ball.resetcoords();
            
    placeball();
            
    delay(100);
            while (
    kbhit())
            {
                return 
    0;
            }
        }


    However, it is leaving trails at the left and the top of the screen.
    It has something to do with if conditions (for left and top boundaries) but I couldn't find where.

    The movement of the ball is a little unnatural when at the time of bouncing on the borderes.

    I want to make the cursor dissappear.

  5. #5
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    k change the bounds for the ball from 80 and 25 to 79 and 24 because when the ball is printed on the edge it goes to a new line hence you see extra balls printed, that should fix the bounds.

    to make the cursor disapear i only know how with the windows.h header add this function
    Code:
    void cursor (int yn)
    {
      CONSOLE_CURSOR_INFO c;
      c.dwSize   = 1;
      c.bVisible = yn;
      SetConsoleCursorInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ), &c );  
    }
    and call cursor(0); in main to make it disappear, use a 1 to make it appear again.

    lastly main must be declared int main its the proper style.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  6. #6
    Akilla
    Guest

    Exclamation Problem

    Your solution worked fine.

    I have TC++ 3 and Borland C++ 5.5

    I dont have windows.h in TC++ 3, so I used
    borland c++ 5.5 to compile it.

    It doesn't recognize the "delay" command.
    But when I used "sleep" it is going either very
    fast (for numbers below 1) and very slow (for numbers
    over 1)

    Is there another delay comand in borland c++ 5.5 ?

    here's my code as of now:
    PHP Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <dos.h>

    struct gamecomponent
    {
        
    int bufxbufy;
        
    int oldxoldy;
        
    int newxnewy;
        
    int xspeedyspeed;
        
    void resetcoords();
    ballpaddle;

    void gamecomponent :: resetcoords()
    {
        
    oldx bufx;
        
    oldy bufy;
        
    bufx newx;
        
    bufy newy;
    }

    void changecoordball()
    {
        if (
    ball.newy == paddle.newy)
        {
            if (
    ball.newx >= paddle.newx && ball.newx <= (paddle.newx 3))
            {
                if (
    ball.xspeed 0)
                {
                    
    ball.xspeed = -ball.xspeed;
                }
                
                if (
    ball.yspeed 0)
                {
                    
    ball.yspeed = -ball.yspeed;
                }
            }

            if (
    ball.newx > (paddle.newx 3) && ball.newx <= (paddle.newx 7))
            {
                if (
    ball.xspeed 0)
                {
                    
    ball.xspeed = -ball.xspeed;
                }
                
                if (
    ball.yspeed 0)
                {
                    
    ball.yspeed = -ball.yspeed;
                }
            }
        }

        if (
    ball.newx >= 79)
        {
            if (
    ball.xspeed 0)
            {
                
    ball.xspeed = -ball.xspeed;
            }
        }

        if (
    ball.newx <= 1)
        {
            if (
    ball.xspeed 0)
            {
                
    ball.xspeed = -ball.xspeed;
            }
        }

        if (
    ball.newy >= 24)
        {
            if (
    ball.yspeed 0)
            {
                
    ball.yspeed = -ball.yspeed;
            }
        }

        if (
    ball.newy <= 1)
        {
            if (
    ball.yspeed 0)
            {
                
    ball.yspeed = -ball.yspeed;
            }
        }

        
    ball.newx ball.newx ball.xspeed;
        
    ball.newy ball.newy ball.yspeed;
        
    }

    void changecoordpaddle(int direction)
    {
        
    int delx;

        if (
    direction == 1)
        {
            
    delx paddle.xspeed;
        }
        else
        {
            
    delx = -paddle.xspeed;
        }

        
    paddle.newx paddle.newx delx;
    }

    void initialize()
    {
        
    ball.bufx 1;
        
    ball.bufy 1;
        
    ball.newx 1;
        
    ball.newy 1;
        
    ball.oldx 1;
        
    ball.oldy 1;
        
    ball.xspeed 1;
        
    ball.yspeed 1;

        
    paddle.bufx 1;
        
    paddle.bufy 25;
        
    paddle.newx 1;
        
    paddle.newy 25;
        
    paddle.oldx 1;
        
    paddle.oldy 25;
        
    paddle.xspeed 1;
        
    paddle.yspeed 1;
    }

    void placeball()
    {
        
    gotoxy(ball.oldxball.oldy);
        
    printf(" ");
        
    gotoxy(ball.newxball.newy);
        
    printf("*");
    }

    void placepaddle()
    {
        
    gotoxy(paddle.oldxpaddle.oldy);
        
    printf("       ");
        
    gotoxy(paddle.newxpaddle.newy);
        
    printf("=======");
    }

    int checkbounce()
    {
        
    int bounceval 1;

        if (
    ball.newy == paddle.newy)
        {
            if (
    ball.newx paddle.newx || ball.newx > (paddle.newx 7))
            {
                
    bounceval 0;
            }
        }

        return 
    bounceval;
    }

    void cursor (int yn)
    {
      
    CONSOLE_CURSOR_INFO c;
      
    c.dwSize   1;
      
    c.bVisible yn;
      
    SetConsoleCursorInfo GetStdHandle STD_OUTPUT_HANDLE ), &);  
    }

    main()
    {
        
    clrscr();
        
    initialize();
        
    cursor(0);

        while (
    == 1)
        {
            
    changecoordball();
            
    ball.resetcoords();
            
    placeball();
            
    sleep(.5);
            while (
    kbhit())
            {
                
    cursor(1);
                return 
    0;
            }
        }


    Also, I want to do the following

    When user presses right or left arrow, it should run
    changecoordpaddle, and pass 1 to the function if it is
    right arrow and 0 if it is left arrow.
    it should run paddle.resetcoords() immediately after that.


    Thanks for your help!


    SEE MY HOMEPAGE @ www.akilla.tk

  7. #7
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    Code:
    void my_delay ( int milli )
    {
      time_t end, now = clock();
      for ( end = now + milli; now < end; now = clock() )
        continue;
    }
    try that

    and for the keys check out my game (dungeons of moria) www.geocities.com/golsafidi i can't give you all the answers
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  8. #8
    Akilla
    Guest

    Unhappy Thanks a lot

    Thanks a lot for the delay command..

    I've seen your game...
    but i only got a slight picture of what was happening.
    how does KEY_LEFT work directly ? is it defined anywhere ?
    Is there a simpler code that recognizes 'ESC' and arrow keys ?

  9. #9
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    look in keys.h and at my_getch() function thats all you need.

    basically call like this

    input=my_getch();

    if(input==K_LEFT)
    0;
    else if (input==K_RIGHT)
    1;

    dont forget to look in keys.h!
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  10. #10
    Akilla
    Guest

    hey

    Hey..
    THANKS VERY MUCH!!!!

    I figured out a way to use kbhit and getch functions
    to get the values..
    I had several problems with it and i could fix it by kbhit..

    I pasted all the code in C++ forum.

    Please play it and post your comments there..

    Once again, thanks very much for your patience.

    MY HOMEPAGE @ www.akilla.tk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21