示例#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);
        }
示例#2
0
        public void Update()
        {
            //updates player location
            Player.Move();
            //checks if food was found
            if (Representation[Player.LocationY, Player.LocationX] == Food.Icon)
            {
                Player.AddPointsToScore();
                Player.Grow();
                Representation[Player.LocationY, Player.LocationX] = Player.Body[0].Icon;
                SetFoodLocation();
            }
            else if (!(Representation[Player.LocationY, Player.LocationX] == ' ' || Representation[Player.LocationY, Player.LocationX] == Player.Icon))
            {
                Player.StatusValue = Player.Status.Dead;
                Console.Title      = "Died to: " + Representation[Player.LocationY, Player.LocationX].ToString();
            }

            //updates blank board
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    if (i == 0 || i == Height - 1)
                    {
                        Representation[i, j] = '=';
                    }
                    else
                    {
                        Representation[i, j] = j == 0 || j == (Width - 2) ? '|' : ' ';
                    }
                }
            }
            //draws members to board
            UpdateMembers();
        }