Example #1
0
        private void Menu_Play_Click(object sender, RoutedEventArgs e)
        {
            GameGrid.Visibility = Visibility.Visible;
            MenuGrid.Visibility = Visibility.Collapsed;
            if (GameField.ActualWidth > 0 && GameField.ActualHeight > 0)
            {
                GameArea = new Point(GameField.ActualWidth, GameField.ActualHeight);
            }
            else if (GameField.Width > 0 && GameField.Height > 0)
            {
                GameArea = new Point(GameField.Width, GameField.Height);
            }
            else
            {
                GameArea = new Point(400, 400); // 18 - menu place
            }

            //GameArea = new Point(200, 200);
            fieldBorder        = new Rectangle();
            fieldBorder.Stroke = Brushes.Black;
            fieldBorder.HorizontalAlignment = HorizontalAlignment.Left;
            fieldBorder.VerticalAlignment   = VerticalAlignment.Top;
            fieldBorder.Width  = GameArea.X;
            fieldBorder.Height = GameArea.Y;

            var tempBody = new List <Code.DirPoint>();

            tempBody.Add(new Code.DirPoint(GameArea.X / 2 - distance, GameArea.Y / 2));
            //for (int i = 2; i < 15; i++)
            //{
            //    tempBody.Add(new Code.DirPoint(GameField.ActualWidth / 2 - i * distance, GameField.ActualHeight / 2));
            //}
            snake = new DrawableSnake(
                new Code.DirPoint(GameArea.X / 2, GameArea.Y / 2),                   // Head
                new Code.DirPoint(GameArea.X / 2 - 2 * distance, GameArea.Y / 2),    // Tail
                tempBody,                                                            // Body
                new Uri(spriteMapPath, UriKind.Relative));                           // Sprite map

            paused            = false;
            _endOfGame        = false;
            _score            = 0;
            ScoreValue.Header = "0";
            apple             = new Code.Apple(
                (GameArea.X / 2) + 2 * distance,            // X
                GameArea.Y / 2,                             // Y
                new Uri(spriteMapPath, UriKind.Relative));  // Sprite map

            GameLogic.MakeNewApple(snake, GameArea.X, GameArea.Y, ref apple);

            gameTimer          = new System.Windows.Threading.DispatcherTimer();
            gameTimer.Tick    += new EventHandler(FrameTick);
            gameTimer.Interval = timerInterval;
            gameTimer.Start();
        }
Example #2
0
 public static void CheckSnakeCollisions(DrawableSnake snake, out bool gameOver)
 {
     gameOver = false;
     // Head collided with the tail
     if (snake.Head.X == snake.Tail.X && snake.Head.Y == snake.Tail.Y)
     {
         gameOver = true;
         return;
     }
     for (int i = 0; i < snake.BodyLength; i++)
     {
         if (snake.Head.X == snake.BodyPoints[i].X && snake.Head.Y == snake.BodyPoints[i].Y)
         {
             gameOver = true;
             return;
         }
     }
 }
Example #3
0
        public static void MakeNewApple(DrawableSnake snake, double areaWidth, double areaHeight, ref Apple apple)
        {
            Point  coords;
            Random rand = new Random();
            bool   goodPoint;
            double fieldHorizCells = areaWidth / snake.HeadSprite.FrameSize.Width;
            double fieldVertCells  = areaHeight / snake.HeadSprite.FrameSize.Height;

            for (;;)
            {
                coords = new Point(
                    rand.Next(0, Convert.ToInt32(fieldHorizCells) - 1) * snake.HeadSprite.FrameSize.Width,     // X
                    rand.Next(0, Convert.ToInt32(fieldVertCells) - 1) * snake.HeadSprite.FrameSize.Height);    // Y
                goodPoint = true;

                if ((coords.X == snake.Tail.X && coords.Y == snake.Tail.Y) ||
                    (coords.X == snake.Head.X && coords.Y == snake.Head.Y))
                {
                    continue;       // Collision, next cycle
                }
                for (int i = 0; i < snake.BodyLength; i++)
                {
                    if (coords.X == snake.BodyPoints[i].X && coords.Y == snake.BodyPoints[i].Y)
                    {
                        goodPoint = false;
                        break;
                    }
                }
                if (goodPoint)
                {
                    break;
                }
            }
            apple.X = coords.X;
            apple.Y = coords.Y;
        }