Example #1
0
 protected override void OnPaint(PaintEventArgs pe)
 {
     base.OnPaint(pe);
     Food.Draw(pe.Graphics);
     snake.Draw(pe.Graphics);
     PaintBorder(pe.Graphics);
 }
Example #2
0
        public void Update()
        {
            while (_gameOver == false)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo input = Console.ReadKey();
                    switch (input.Key)
                    {
                    case ConsoleKey.UpArrow:
                        snakeDirection = Direction.Up;
                        break;

                    case ConsoleKey.DownArrow:
                        snakeDirection = Direction.Down;
                        break;

                    case ConsoleKey.LeftArrow:
                        snakeDirection = Direction.Left;
                        break;

                    case ConsoleKey.RightArrow:
                        snakeDirection = Direction.Right;
                        break;
                    }
                }

                if ((DateTime.Now - _lastDate).TotalMilliseconds >= _frameRate)
                {
                    snake.Draw();
                    snake.Move(snakeDirection);

                    target.Draw();

                    if (snake.IsOverlapping(target))
                    {
                        snake.Eat();
                        target.SetNewPosition();
                    }

                    if (snake.GameOver() || snake.HitBorder())
                    {
                        _gameOver = true;
                    }


                    _lastDate = DateTime.Now;
                }
            }

            if (_gameOver == true)
            {
                Console.Clear();
                Console.WriteLine($"Game Over, Score: {snake.GetLength()}");
            }
        }