コード例 #1
0
 /// <summary>
 /// this method checks if the snake has come in contact with the apple
 /// </summary>
 /// <param name="apple"></param>
 /// <param name="snake"></param>
 /// <returns></returns>
 static bool DetectAppleCollision(Block apple, List <Block> snake)
 {
     if (snake[0].GetCode() == apple.GetCode())
     {
         return(true);
     }
     return(false);
 }
コード例 #2
0
        // spawns an apple and gives it a random coordinate on the grid
        static Block GenerateApple(Block apple, Random rand, List <Block> snake)
        {
            bool appleCheck = true;

            while (appleCheck)
            {
                appleCheck = false;
                // gives the apple a random coordinate
                apple = new Block(rand.Next(1, BoardX - 1), rand.Next(1, BoardY - 1));
                foreach (Block b in snake)
                {
                    // if the apple has the same coordinate as the snake, respawn the apple
                    if (b.GetCode() == apple.GetCode())
                    {
                        appleCheck = true;
                        break;
                    }
                }
            }
            return(apple);
        }