Beispiel #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();
        }
Beispiel #2
0
        public static bool IsSnakeAteApple(Snake snake, Apple apple, ref int score, int appleScoreCost = 10)
        {
            bool result = false;

            //if (apple.X == snake.Head.X && apple.Y == snake.Head.Y)
            if ((snake.Head.X <= apple.X + (apple.Sprite.FrameSize.Width / 2)) &&
                (snake.Head.X >= apple.X - (apple.Sprite.FrameSize.Width / 2)) &&
                ((snake.Head.Y <= apple.Y + (apple.Sprite.FrameSize.Height / 2)) &&
                 (snake.Head.Y >= apple.Y - (apple.Sprite.FrameSize.Height / 2))))
            {
                result = true;
                score += appleScoreCost;
            }
            return(result);
        }
Beispiel #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;
        }