public void ScoreNameTest() { SnakeGame.ScoreName stat = new SnakeGame.ScoreName(""); stat.Name = "Jon"; Assert.AreEqual("Jon", stat.Name); }
public static void Run() { ScoreName scorename = new ScoreName(); scorename.View(); BGM(); // display this string on the console during the game string ch = "*"; bool gameLive = true; ConsoleKeyInfo consoleKey; // location info & display int x = 0, y = 2; // y is 2 to allow the top row for directions & space int consoleWidthLimit = 79; int consoleHeightLimit = 24; string food = "@"; string sfood = "$"; int lastsFoodTime = 0; string obs = "|"; int lastFoodTime = 0; int foodDissapearTime = 10000; lastsFoodTime = Environment.TickCount; Random rand = new Random(); //inputs random numbers // Direction modification: Lewis Chin byte right = 0; byte left = 1; byte down = 2; byte up = 3; Index[] directions = new Index[] { new Index(0, 1), // right new Index(0, -1), // left new Index(1, 0), // down new Index(-1, 0), // up }; int direction = right; // clear to color Console.BackgroundColor = ConsoleColor.Black; //the background of the colour Console.Clear(); // delay to slow down the character movement so you can see it int delayInMillisecs = 50; int scores = 0; // //Location to spawn obstacle : Jonathan List <Index> obstacles = new List <Index>() { new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), new Index(rand.Next(0, consoleHeightLimit) /*height*/, rand.Next(0, consoleWidthLimit) /*Width*/), }; //Spawning obstacle : Jonathan foreach (Index obstacle in obstacles) { Console.ForegroundColor = ConsoleColor.Cyan; Console.SetCursorPosition(obstacle.indexx, obstacle.indexy); Console.Write(obs); } // Merge food and snake elements : Lewis Chin Queue <Index> elements = new Queue <Index>(); for (int i = 0; i <= 2; i++) { elements.Enqueue(new Index(1, i)); } //Spawn food : Jonathan Index fruit; do { fruit = new Index(rand.Next(0, consoleHeightLimit), rand.Next(0, consoleWidthLimit)); }while (elements.Contains(fruit) || obstacles.Contains(fruit)); Console.SetCursorPosition(fruit.indexx, fruit.indexy); Console.ForegroundColor = ConsoleColor.Red; Console.Write(food); //Spawn special food : Lee Zhe Sheng Index sfruit; do { sfruit = new Index(rand.Next(0, consoleHeightLimit), rand.Next(0, consoleWidthLimit)); }while (elements.Contains(sfruit) || obstacles.Contains(sfruit)); Console.SetCursorPosition(sfruit.indexx, sfruit.indexy); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(sfood); // Snake Character position : Lewis Chin foreach (Index position in elements) { Console.SetCursorPosition(position.indexx, position.indexy); Console.ForegroundColor = ConsoleColor.Green; Console.Write(ch); } int Timer = 0; //Timer : Jonathan Lee do // until escape { //Display Timer: Jonathan Lee ConsoleColor time = Console.ForegroundColor = ConsoleColor.Yellow; Console.SetCursorPosition(80, 0); Console.Write("Timer: " + Timer); Console.ForegroundColor = time; Timer++; // print directions at top, then restore position // save then restore current color ConsoleColor cc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Black; Console.SetCursorPosition(0, 0); Console.WriteLine("Arrows move up/down/right/left. Press 'esc' quit."); Console.SetCursorPosition(x, y); Console.ForegroundColor = cc; ConsoleColor score = Console.ForegroundColor = ConsoleColor.Yellow; Console.SetCursorPosition(100, 0); Console.Write("Score: " + scores); Console.ForegroundColor = score; // see if a key has been pressed // Direction modification : Lewis Chin if (Console.KeyAvailable) { // get key and use it to set options consoleKey = Console.ReadKey(true); switch (consoleKey.Key) { case ConsoleKey.UpArrow: //UP if (direction != down) { direction = up; } Console.ForegroundColor = ConsoleColor.Red; break; case ConsoleKey.DownArrow: // DOWN if (direction != up) { direction = down; } Console.ForegroundColor = ConsoleColor.Cyan; break; case ConsoleKey.LeftArrow: //LEFT if (direction != right) { direction = left; } Console.ForegroundColor = ConsoleColor.Green; break; case ConsoleKey.RightArrow: //RIGHT if (direction != left) { direction = right; } Console.ForegroundColor = ConsoleColor.Black; break; case ConsoleKey.Escape: //END gameLive = false; break; } } // Snake Head Direction : Lewis Chin Index snakeHead = elements.Last(); Index nextDirection = directions[direction]; Index snakeNewHead = new Index(snakeHead.indexy + nextDirection.indexy, snakeHead.indexx + nextDirection.indexx); if (snakeNewHead.indexx < 0) { snakeNewHead.indexx = consoleWidthLimit - 1; } if (snakeNewHead.indexy < 0) { snakeNewHead.indexy = consoleHeightLimit - 1; } if (snakeNewHead.indexy >= consoleHeightLimit) { snakeNewHead.indexy = 0; } if (snakeNewHead.indexx >= consoleWidthLimit) { snakeNewHead.indexx = 0; } // Game Over after snake eats itself : Lewis Chin foreach (Index position in elements) { if (snakeNewHead.indexx == position.indexx && snakeNewHead.indexy == position.indexy) { StreamWriter sw = File.AppendText("../../../ScoreBoard/ScoreBoard.txt"); sw.WriteLine("Name: " + scorename.Name + " [Score: " + scores.ToString() + " Timer: " + Timer.ToString() + "]", "\n"); sw.Close(); gameLive = false; GameOver(); } Console.SetCursorPosition(position.indexx, position.indexy); Console.ForegroundColor = ConsoleColor.Green; Console.Write(ch); } elements.Enqueue(snakeNewHead); Console.SetCursorPosition(snakeNewHead.indexx, snakeNewHead.indexy); Console.ForegroundColor = ConsoleColor.Green; if (direction == right) { Console.Write(ch); } if (direction == left) { Console.Write(ch); } if (direction == up) { Console.Write(ch); } if (direction == down) { Console.Write(ch); } // Snake length growth : Lewis Chin if (snakeNewHead.indexx == fruit.indexx && snakeNewHead.indexy == fruit.indexy) { //Eat Sound : Jonathan Lee System.Media.SoundPlayer Eat = new System.Media.SoundPlayer(); Eat.SoundLocation = "../../../Music/Hit.wav"; Eat.Play(); do { fruit = new Index(rand.Next(0, consoleHeightLimit), rand.Next(0, consoleWidthLimit)); }while (elements.Contains(fruit) || obstacles.Contains(fruit)); lastFoodTime = Environment.TickCount; Console.SetCursorPosition(fruit.indexx, fruit.indexy); Console.ForegroundColor = ConsoleColor.Red; Console.Write(food); scores += 1; } //Special food spawn if (snakeNewHead.indexx == sfruit.indexx && snakeNewHead.indexy == sfruit.indexy) { //Eat Sound : Jonathan Lee System.Media.SoundPlayer Eat = new System.Media.SoundPlayer(); Eat.SoundLocation = "../../../Music/Hit.wav"; Eat.Play(); do { sfruit = new Index(rand.Next(0, consoleHeightLimit), rand.Next(0, consoleWidthLimit)); }while (elements.Contains(sfruit) || obstacles.Contains(sfruit)); lastFoodTime = Environment.TickCount; Console.SetCursorPosition(sfruit.indexx, sfruit.indexy); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(sfood); scores += 10; } else { // Added space : Lewis Chin Index last = elements.Dequeue(); Console.SetCursorPosition(last.indexx, last.indexy); Console.Write(" "); } // Food appears every 10 seconds at different positions: Lewis Chin //Updated by Lee Zhe Sheng if (Environment.TickCount - lastsFoodTime >= foodDissapearTime) { Console.SetCursorPosition(sfruit.indexx, sfruit.indexy); Console.Write(" "); do { sfruit = new Index(rand.Next(0, consoleHeightLimit), rand.Next(0, consoleWidthLimit)); }while (elements.Contains(sfruit) || obstacles.Contains(sfruit)); lastsFoodTime = Environment.TickCount; } Console.SetCursorPosition(sfruit.indexx, sfruit.indexy); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(sfood); foreach (Index obstacle in obstacles) { if (snakeNewHead.indexx == obstacle.indexx && snakeNewHead.indexy == obstacle.indexy) { //bug fixed: Lee Zhe Sheng //Game End Score: Jonathan Lee StreamWriter sw = File.AppendText("../../../ScoreBoard/ScoreBoard.txt"); sw.WriteLine("Name: " + scorename.Name + " [Score: " + scores.ToString() + " Timer: " + Timer.ToString() + "]", "\n"); sw.Close(); gameLive = false; GameOver(); } } // write the character in the new position // length add after eat: Lewis Chin // pause to allow eyeballs to keep up System.Threading.Thread.Sleep(delayInMillisecs); } while (gameLive); }