Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This collision detection code is in my FRM_Snake form. The person who created the tutorial used an integer to serve as a "wall". I'd like to share this with classmates, so my question comes in two parts:

A) How would I make the integers (height=480, width=480) act as a "wall" that will kill the snake when hit?

B) I would like the snake to go through one side of the screen and come out the opposite way. How would I code that?

Thanks so much for your help :) It is much appreciated!

C#
public void Collisoin()
{
    for (int i = 1; i < snake.MySnake.Length; i++)
    {
        if (snake.MySnake[0].IntersectsWith(snake.MySnake[i]))
        {
            MessageBox.Show("Ouch! Quit biting yourself!\n Your total score is " + Score);
            Restart();
        }
    }

    //Use if you want game to be over when snake hits the edge of the screen
    if (snake.MySnake[0].X < 0 || snake.MySnake[0].X > 480)
    {
        MessageBox.Show("Haha! You're not a worm;\nyou can't chew through walls! Your total score is " + Score);
        Restart();
    }
    if (snake.MySnake[0].Y < 0 || snake.MySnake[0].Y > 480)
    {
        MessageBox.Show("Haha! You're not a worm;\nyou can't chew through walls! Your total score is " + Score);
        Restart();
    }
}
Posted
Updated 25-Jul-11 8:32am
v2

1 solution

Whenever a portion of your snake goes past 480, subtract 480 from the position (you can also use a mod function to handle this for you). Example:
C#
snake.MySnake[i].X = snake.MySnake[i].X % 480;

Also, whenever the snake goes less than zero, add 480 (I am not sure how mod works with negative numbers, so maybe actually add 480).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900