Thread: Text Based Pong Game Ready :-)

  1. #1
    Akilla
    Guest

    Cool Text Based Pong Game Ready :-)

    Hey Guys,
    I finally wrote the Text Based Pong game :-)

    Thanks to red baron for his help on hiding the cursor, changing the boundary values and the my_delay function.

    Here's the code:
    PHP Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <time.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 && ball.newx >= paddle.newx && ball.newx <= (paddle.newx 7))
        {
            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 <= 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;
        }

        if (
    paddle.newx delx >= && paddle.newx delx <= 80)
        {
            
    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 25;
        
    paddle.bufy 25;
        
    paddle.newx 25;
        
    paddle.newy 25;
        
    paddle.oldx 25;
        
    paddle.oldy 25;
        
    paddle.xspeed 2;
        
    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 ), &);  
    }

    void my_delay (int milliseconds)
    {
        
    time_t endnow clock();
        for ( 
    end now millisecondsnow endnow clock() )
        continue;
    }

    int grabinput()
    {
        
    char c;
        
    getch();
        
        if ((float) 
    == 27)
        {
            return 
    2;
        }
        else if ((float) 
    == 75)
        {
            return 
    0;
        }
        else if ((float) 
    == 77)
        {
            return 
    1;
        }
        
        
    fflush(stdin);
        return -
    1;
        
    }

    main()
    {
        
    int pressedkey;
        
    int bouncer;
        
    clrscr();
        
    initialize();
        
    cursor(0);
        
        while (
    checkbounce() == 1)
        {
            
    changecoordball();
            
    ball.resetcoords();
            
    placeball();
            
    placepaddle();
            
    my_delay(100);
            
            while (
    kbhit())
            {
                
    pressedkey grabinput();
            
                if (
    pressedkey != -&& pressedkey != 2)
                {
                    
    changecoordpaddle(pressedkey);
                    
    paddle.resetcoords();
                    
    placepaddle();
                }
                else if (
    pressedkey == 2)    
                {
                    
    cursor(1);
                    return 
    0;
                }
            }
        }
        
        
    gotoxy(3813);
        
    printf("CRASH!");
        
    my_delay(1000);
        
    gotoxy(124);
        
    printf("\nProgrammed by Mahurshi Akilla\nwww.akilla.tk\n");


    Play it and tell me how it is.
    This is my first game in C.
    Any suggestions/comments are welcome!!!!

    MY HOMEPAGE @ www.akilla.tk

  2. #2
    Akilla
    Guest

    Question no comments ?

    no comments ?

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    6
    I can't compile that, none of the include files i have contain the gotoxy() function or clrscr() function. Oh well, it looked interesting, if that helps...

    Brendan

  4. #4
    Akilla
    Guest

    BC++

    Should compile well with Borland C++ 5.5

  5. #5
    Akilla
    Guest

    EXE

    If you're having compiling problems, download
    the executable (30 KB)
    http://akillanet.tripod.com/software/textpong.zip

    here's the latest source code again.
    (This compiles well with Borland C++ 5.5)

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

    #define paddlelength 7

    struct gameinfo
    {
        
    int score;
    gameboard;

    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 && ball.newx >= paddle.newx && ball.newx <= (paddle.newx paddlelength))
        {
            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 paddlelength))
            {
                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 <= 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;
        }

        if (
    paddle.newx delx >= && paddle.newx delx paddlelength <= 80)
        {
            
    paddle.newx paddle.newx delx;
        }
    }

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

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

        
    gameboard.score 0;
    }

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

    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 paddlelength))
            {
                
    bounceval 0;
            }
            else if (
    ball.newx >= paddle.newx && ball.newx <= paddle.newx paddlelength)
            {
                
    gameboard.score++;
            }
        }

        return 
    bounceval;
    }

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

    void my_delay (int milliseconds)
    {
        
    time_t endnow clock();
        for ( 
    end now millisecondsnow endnow clock() )
        continue;
    }

    int grabinput()
    {
        
    char c;
        
    getch();
        
        if ((float) 
    == 27)
        {
            return 
    2;
        }
        else if ((float) 
    == 75)
        {
            return 
    0;
        }
        else if ((float) 
    == 77)
        {
            return 
    1;
        }
        
        
    fflush(stdin);
        return -
    1;
        
    }

    void showscore()
    {
        
    gotoxy(11);
        
    printf("Score: %d"gameboard.score);
    }

    main()
    {
        
    int pressedkey;
        
    int bouncer;
        
    clrscr();
        
    initialize();
        
    cursor(0);
        

        while (
    checkbounce() == 1)
        {
            
    changecoordball();
            
    ball.resetcoords();
            
    placeball();
            
    placepaddle();
            
    my_delay(65);
            
    showscore();
            
            while (
    kbhit())
            {
                
    pressedkey grabinput();
            
                if (
    pressedkey != -&& pressedkey != 2)
                {
                    
    changecoordpaddle(pressedkey);
                    
    paddle.resetcoords();
                    
    placepaddle();
                }
                else if (
    pressedkey == 2)    
                {
                    
                    return 
    0;
                }
            }
        }
        
        
    gotoxy(3813);
        
    printf("CRASH!");
        
    my_delay(1000);
        
    gotoxy(124);
        
    printf("\nProgrammed by Mahurshi Akilla\nhttp://mahurshi.tripod.com\n");
        
    cursor(1);


    Please tell me if there're any bugs/comments/etc

    www.akilla.tk

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    wooohooo very nice!!
    thumbs up to you , it works fine but it wont compile on MVS

    and ur webhoster wont allow external download , id suggest attaching the file to the board

    edit note: btw if its a C program why is it under the C++ forum

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Here

    I am attaching it to this.

    Can anyone tell me why it doesn't compile with other compilers?
    How do I make it work with all of the compilers?

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> Can anyone tell me why it doesn't compile with other compilers?

    You use some compiler-specific functions and stuff in there...

  9. #9
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    how do i know what functions I have to change (and to what they have to be changed) in order to make it totally ANSI C ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text based mmo type game.....
    By SONAR in forum Game Programming
    Replies: 0
    Last Post: 12-09-2008, 05:17 AM
  2. Text Based RPG & AI?
    By TylerMoyer in forum General AI Programming
    Replies: 4
    Last Post: 10-12-2007, 06:40 AM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM