Results 1 to 3 of 3

Thread: Bounce a character around the screen using for loops & gotoxy

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    4

    Bounce a character around the screen using for loops & gotoxy

    Greets all,

    I'm following some examples of using the gotoxy function from a c book that i am reading, and can't seem to get the quiz/task. Here's how it goes, i am trying to get the character O to bounce around the screen, using a few four loops and the goto function. I am able to get the character to go diagonally accross the screen with...

    Code:
     void main(void)
    { int x,y,z;
    
    for(x=0,y=0,z=0;z<5000;x++,y++,z++)
    	{
    	  gotoxy(x,y);
    	  printf("O");
    	  delay(100);
    	  clrscr();
     	}
    getch();
    }
    But i want the character 'ball' to bounce around the screen. I asked my programming teacher to give me tips, but i asked him not to give me the answe completely else i won't learn anything. He said that i needed, 4 if statements, which i have tried adding into the code, but to no avail. I have now given up, and would like to find out the answer as i'm ripping my hair out.
    Last edited by K.A.J; Sep 24th, 2002 at 01:06 PM.

  2. #2
    Member Stef's Avatar
    Join Date
    May 2001
    Posts
    41
    Basically, those four if statements would detect if the ball touches the edges of the screen (one if-statement for each edge), and reverse its speed if it does.
    Oh, this assumes a screen from 0-80 horizontally, and 0-25 vertically, adjust the values as needed

    Code:
    void main(void)
    { 
      int x,y,z;
      int vx = 1;
      int vy = 1;
    
      for(x=0,y=0,z=0;z<5000;x+=vx,y+=vy,z++)
      {
        if( x<0 )
        {
          vx = 1;
          x = 0;
        }
        if( x>80 )
        {
          vx = -1;
          x = 80
        }
        if( y<0 )
        {
          vy = 1;
          y = 0;
        }
        if( y > 25 )
        {
          vy = -1;
          y = 25;
        }
        gotoxy(x,y);
        printf("O");
        delay(100);
        clrscr();
      }
      getch();
    }

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    4
    Thankyou, It all seems so straight forward after you see what you were doing wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width