Пример #1
0
        private void GameTimer_Tick(object sender, EventArgs e)
        {
            // If Snake is not alive show announcement, stop timer and return
            if (Snake.Alive == false)
            {
                gameTimer.Stop();
                announceLabel.Text      = Helpers.AnnouncementText;
                announceLabel.BackColor = Helpers.AnnouncementBGC;
                announceLabel.ForeColor = Helpers.AnnouncementFGC;
                announceLabel.Visible   = true;
                Console.WriteLine("Snek dead. announceLabel.Visible = " + announceLabel.BackColor);
                return;
            }

            // Check for collision between snake head and point
            if (Point != null && Helpers.Collides(Snake.Head, Point.Sprite))
            {
                Console.WriteLine("Collision detected!");
                Console.WriteLine("Collision at: Snake(X: {0}, Y: {1}) and Point(X: {2}, Y: {3})", Snake.Head.X, Snake.Head.Y, Point.Sprite.X, Point.Sprite.Y);
                Point.Destroy();
                Point = null;
                Snake.Grow();
            }

            // Check for collision between snake head and snake body
            if (Snake.Tail.Count() != 0)
            {
                foreach (var part in Snake.Tail)
                {
                    if (Helpers.Collides(Snake.Head, part))
                    {
                        Snake.Kill();
                    }
                }
            }

            // If there is no point create one
            if (Point == null)
            {
                int xPos = Helpers.RandomInt(0, 19) * 15;
                int yPos = Helpers.RandomInt(0, 19) * 15;
                Point = new Food(gameContainer, xPos, yPos);
            }

            // If Snake is alive keep it moving
            Snake.Move();
            Snake.Update();
            scoreLabel.Text = "Score: " + (Snake.Length - 1);
        }