Example #1
0
        public void moveSnake()
        {
            string currDirection = "";

            int index = 0;

            for (int i = 0; i < game.Children.Count; i++)
            {
                if (i == 0)
                {
                    currDirection = direction;
                    changeHead();
                    if (checkCollision())
                    {
                        start.Visibility = Visibility.Visible;
                        game.Children.Clear();
                        //game.Background = @"C:\\Users\\kychill\\Pictures\\Camera Roll\\go.png";
                        game.Background = new ImageBrush
                        {
                            ImageSource = new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + "\\go.png", UriKind.Absolute))
                        };
                        Snake.Clear();
                        break;
                    }
                    if (outOfBounds())
                    {
                        start.Visibility = Visibility.Visible;
                        game.Children.Clear();
                        game.Background = new ImageBrush
                        {
                            ImageSource = new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + "\\go.png", UriKind.Absolute))
                        };
                        Snake.Clear();
                        break;
                    }
                }
                else
                {
                    currDirection = Snake[i - 1].getLastDirection();
                }
                Rectangle currPart = (Rectangle)game.Children[i];
                if (currPart.Name == "food")
                {
                    if (checkFood(currPart))
                    {
                        double xs = Canvas.GetLeft((Rectangle)game.Children[i - 1]);
                        double ys = Canvas.GetTop((Rectangle)game.Children[i - 1]);
                        string d  = Snake[i - 1].getCurrentDirection();
                        addPart(xs, ys, d);
                        runningScore += 50;
                        score.Content = "Score: " + runningScore.ToString();
                        index         = index - 1;
                        break;
                    }
                }
                else
                {
                    //Console.WriteLine(Snake.Count);
                    //Console.WriteLine(game.Children.Count);
                    if (Snake == null)
                    {
                        break;
                    }
                    SnakePart s = Snake[index];
                    s.setLastDirection(s.getCurrentDirection());

                    double x = Canvas.GetLeft(currPart);
                    double y = Canvas.GetTop(currPart);

                    if (currDirection == "right")
                    {
                        Canvas.SetLeft(currPart, x + speed);
                        Canvas.SetTop(currPart, y);
                        s.setCurrentDirection("right");
                    }
                    else if (currDirection == "down")
                    {
                        Canvas.SetLeft(currPart, x);
                        Canvas.SetTop(currPart, y + speed);
                        s.setCurrentDirection("down");
                    }
                    else if (currDirection == "up")
                    {
                        Canvas.SetLeft(currPart, x);
                        Canvas.SetTop(currPart, y - speed);
                        s.setCurrentDirection("up");
                    }
                    else if (currDirection == "left")
                    {
                        Canvas.SetLeft(currPart, x - speed);
                        Canvas.SetTop(currPart, y);
                        s.setCurrentDirection("left");
                    }
                }
                index++;
            }
        }