Thread: how to reprint food WITHIN the borders in snake..

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    how to reprint food WITHIN the borders in snake..

    If you visit this forum daily, you would notice that I have been posting topics about the snake game recently. I've been able to move the snake already and now, my problem is to reprint the food WITHIN the borders after it has been eaten because when it prints after it has been eaten, it sometimes prints outside the border. Need help here guys. Thank you. Here's my code btw.
    Code:
    while (snake == 1)
      {
        delay(40000000); // Delay of the loop.
        gotoxy (snakeX[0], snakeY[0]);
        printf("*");
        gotoxy (snakeX[nLength], snakeY[nLength]); // This erases the "trail" the snake leaves behind.
        printf(" ");
        for (i = nLength; i >= 1; i--) // Passing of values on the array. This means that the value of nSnakeX/Y[6] will be equivalent to the value of nSnakeX/Y[5] and so on. So nSnakeX/Y[1] would be equal to nSnake[0] and that makes the body of the snake.
        {
          snakeX[i] = snakeX[i - 1];
          snakeY[i] = snakeY[i - 1];
        }
    
    
        if ( kbhit() )
        {
          nDir = getch();
          if (nDir == 0)
          nDir = getch(); // Detects arrow key input.
        }
        
        if ((nDir == UP && nDir2 != DOWN) || (nDir == DOWN && nDir2 != UP) || (nDir == LEFT && nDir2 != RIGHT) || (nDir == RIGHT && nDir2 != LEFT))
                nDir2 = nDir; // This is used in order for the snake not to move in reverse. Example, if the snake is moving up, it will NOT move down because of the if statement.
    
    
        switch (nDir2)
        {
          case UP: snakeY[0]--; break;
          case DOWN: snakeY[0]++; break;
          case LEFT: snakeX[0]--; break;
          case RIGHT: snakeX[0]++; break;
        }
        
        if (snakeX[0] == food_x && snakeY[0] == food_y || snakeX[0] == food_y || snakeY[0] == food_x) // Collision detector with the food.
        {
          srand(time(NULL));
          food_x = rand() % LENGTH + 2;
          food_y = rand() % WIDTH + 2;
          gotoxy (food_x, food_y);
          printf("A");
        }
        if (nScore % 2 == 0 && nScore != 0) // Snake gets longer if the updated/current score is divisible by two.
             nLength++;
        if (snakeY[0] == 0 || snakeX[0] == 0 || snakeY[0] == LENGTH * 2 - 2 || snakeX[0] == WIDTH - 1) // Collision detector with the walls.
            {    
                snake = 0;
                gotoxy(33, 10);
                printf("Game Over!");
            }
      }    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > srand(time(NULL));
    Do this ONCE at the start of main.
    Calling srand() more than once - and especially if in a loop can make things decidedly non-random.


    > food_x = rand() % LENGTH + 2;
    > food_y = rand() % WIDTH + 2;
    How does 2..LENGTH+1 seem as a valid range to you?

    Perhaps some more constants which describe the actual dimensions of the field would help you make better calculations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get window position plus borders
    By hman in forum Windows Programming
    Replies: 8
    Last Post: 07-26-2010, 10:17 PM
  2. reprint originals
    By goran00 in forum C Programming
    Replies: 16
    Last Post: 04-02-2008, 07:28 AM
  3. OpenGL Questions... Borders and Colors...
    By Wraithan in forum Game Programming
    Replies: 11
    Last Post: 10-02-2006, 11:11 PM
  4. Finding the size of the borders.
    By jpfarias in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 08:50 PM
  5. strcmp, any borders?
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 02:48 AM