Bounce around screen

This is a discussion on Bounce around screen within the C++ Programming forums, part of the General Programming Boards category; How do I bounce a star around the screen using gotoxy ? I tried the follwing but it doesn't work ...

  1. #1
    Akilla
    Guest

    Bounce around screen

    How do I bounce a star around the screen using gotoxy ?

    I tried the follwing but it doesn't work after sometime:
    PHP Code:
    #include <stdio.h>
    #include <conio.h>
    #include <dos.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 xaddyadddepvar=0;

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

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

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

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

        if (
    ball.newy == paddle.newy)
        {
            if (
    ball.newx >= paddle.newx && ball.newx <= (paddle.newx 3))
            {
                
    xadd = -ball.speed;
                
    depvar 1;
            }

            if (
    ball.newx > (paddle.newx 3) && ball.newx <= (paddle.newx 7))
            {
                
    xadd ball.speed;
                
    depvar 1;
            }
        }

        if (
    depvar != 1)
        {
            
    xadd 1;
            
    yadd 1;
        }

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

    void changecoordpaddle(int direction)
    {
        
    int delx;

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

        
    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.speed 2;

        
    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 (
    == 1)
        {
            
    changecoordball();
            
    resetcoordsball();
            
    placeball();
            
    delay(100);
            while (
    kbhit())
            {
                return 
    0;
            }
        }
        

    Any help is appreciated.

    COOL PROGRAMS @ www.akilla.tk

  2. #2
    Akilla
    Guest

    ok..

    Ok... here's the latest version that works

    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;
            }
        }


    But the movement of the ball is a little bit unnatural (especially while hitting the borders)

    I also see some trails (at the top and left borders)

    Can anyone help me figure out why these two things
    are happening ?

    SEE MY HOMEPAGE @ www.akilla.tk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 01:48 PM
  2. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 03:41 AM
  5. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 01:51 PM

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