public static void Main(string [] args) { Console.CursorVisible = false; Console.BackgroundColor = ConsoleColor.Green; Console.Clear(); Console.ForegroundColor = ConsoleColor.Black; Wall.buildTheWall(); List <Point> body = new List <Point>(); Snake.createTheSnake(body); Apple.putAppleOnTheScreen(body); Snake.startPlaying(body); }
/// <summary> /// Everyone /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gameTimer_Tick(object sender, EventArgs e) { //When game is being played //Josh if (gameState == GameState.GameOn) { //Create objects if they don't exist if (GameCanvas.Children.Count == 0) { GameStart(); lastKey = Key.DbeAlphanumeric; } //If the snake collides with an apple, Generate a new apple, and increase the score if (Player.EatsApple(Player.headPos, apple.Position) == true) { this.Title = "Snake " + gameVersion + " - Score: " + Player.score; apple.SelfDestruct(GameCanvas); apple = new Apple(GameCanvas, Player); } //If the last key is a random key that it was set to earlier if (lastKey == Key.DbeAlphanumeric) { //Make the textblock fade in if (tB_PressKey.Opacity >= 0 && goingUp == true) { tB_PressKey.Opacity += .1; if (tB_PressKey.Opacity == 1) { goingUp = false; } } //and fade out else if (tB_PressKey.Opacity <= 1 && goingUp == false) { tB_PressKey.Opacity -= .1; if (tB_PressKey.Opacity <= .05) { goingUp = true; } } } //If a key has been pressed, but the textblock is visible, make it visible else if (lastKey != Key.DbeAlphanumeric && tB_PressKey.Opacity != 0) { tB_PressKey.Opacity = 0; goingUp = false; } //Otherwise, move the snake else if (lastKey == Key.Up || lastKey == Key.Down || lastKey == Key.Left || lastKey == Key.Right) { Player.Movement(lastKey); } //If the head of the snake is no longer within the window if (CheckOutOfBounds() == true) { //End the game gameState = GameState.GameOver; } //For each snake trail rectangle foreach (Point p in Player.trailPoints) { //if the snake head is overtop of the rectangle if (CheckCollision(p, Player.headPos) == true) { //End the game gameState = GameState.GameOver; } } } //Cam //When game is over, run gameover method else if (gameState == GameState.GameOver) { if (GameCanvas.Children.Count >= 1) { GameOver(); //Display leaderboards } } }
private static void moveTheSnake(List <Point> snakeBody) { bool gameIsStillRunning = true; bool youPressTheRightKeys = false; char snakeDirection = ' '; int numberOfMove = 0; //Assigns point a default value, use this if you are not sure about the default value of a type Point nextHead = default(Point); ConsoleKey input = Console.ReadKey(true).Key; if (input == ConsoleKey.DownArrow) { snakeBody.Reverse(); } do { Point last = snakeBody[snakeBody.Count - 1]; Console.SetCursorPosition(last.X, last.Y); snakeBody.RemoveAt(snakeBody.Count - 1); Console.Write(" "); nextHead = snakeBody[0]; do { if (input == ConsoleKey.UpArrow || input == ConsoleKey.W) { nextHead = new Point(nextHead.X, nextHead.Y - 1); SetCursorAtPositionXandYfor(nextHead); snakeDirection = 'U'; youPressTheRightKeys = false; numberOfMove++; } else if (input == ConsoleKey.DownArrow || input == ConsoleKey.S) { nextHead = new Point(nextHead.X, nextHead.Y + 1); SetCursorAtPositionXandYfor(nextHead); snakeDirection = 'D'; youPressTheRightKeys = false; numberOfMove++; } else if (input == ConsoleKey.LeftArrow || input == ConsoleKey.A) { nextHead = new Point(nextHead.X - 1, nextHead.Y); SetCursorAtPositionXandYfor(nextHead); snakeDirection = 'L'; youPressTheRightKeys = false; numberOfMove++; } else if (input == ConsoleKey.RightArrow || input == ConsoleKey.D) { nextHead = new Point(nextHead.X + 1, nextHead.Y); SetCursorAtPositionXandYfor(nextHead); snakeDirection = 'R'; youPressTheRightKeys = false; numberOfMove++; } else { if (numberOfMove == 0) { youPressTheRightKeys = true; input = Console.ReadKey(true).Key; } else { if (snakeDirection == 'D') { input = ConsoleKey.DownArrow; } else if (snakeDirection == 'U') { input = ConsoleKey.UpArrow; } else if (snakeDirection == 'L') { input = ConsoleKey.LeftArrow; } else if (snakeDirection == 'R') { input = ConsoleKey.RightArrow; } } } }while(youPressTheRightKeys); //To in_ert this new head snakeBody.Insert(0, nextHead); if (Console.KeyAvailable) { input = Console.ReadKey(true).Key; // This is to make sure that I cant go backwards when the snake is going upwards and so on.. if ((input == ConsoleKey.UpArrow || input == ConsoleKey.W) && snakeDirection == 'D') { input = ConsoleKey.DownArrow; } else if ((input == ConsoleKey.DownArrow || input == ConsoleKey.S) && snakeDirection == 'U') { input = ConsoleKey.UpArrow; } else if ((input == ConsoleKey.RightArrow || input == ConsoleKey.D) && snakeDirection == 'L') { input = ConsoleKey.LeftArrow; } else if ((input == ConsoleKey.LeftArrow || input == ConsoleKey.A) && snakeDirection == 'R') { input = ConsoleKey.RightArrow; } } //If the snake eats the Apple if (nextHead.X == Apple.AppleXPosition && nextHead.Y == Apple.AppleYPosition) { Apple.ApplesEaten++; Point nextTail = snakeBody[snakeBody.Count - 1]; //Increasing the snake's body if (snakeDirection == 'U') { nextTail = new Point(nextTail.X, nextTail.Y + 1); } else if (snakeDirection == 'D') { nextTail = new Point(nextTail.X, nextTail.Y - 1); } else if (snakeDirection == 'L') { nextTail = new Point(nextTail.X + 1, nextTail.Y); } else if (snakeDirection == 'R') { nextTail = new Point(nextTail.X - 1, nextTail.Y); } snakeBody.Insert(snakeBody.Count, nextTail); Apple.putAppleOnTheScreen(snakeBody); Snake.speed *= 0.960m; } //If the snake touches itself for (int i = 2; i < snakeBody.Count - 1; i++) { if (nextHead == snakeBody[i]) { gameIsStillRunning = false; } } //if it touches the wall if (nextHead.X == 1 || nextHead.X == 70 || nextHead.Y == 1 || nextHead.Y == 40) { gameIsStillRunning = false; } Thread.Sleep(Convert.ToInt32(speed)); //Display score Console.SetCursorPosition(3, 42); Console.Write("SCORE = " + Apple.ApplesEaten * 100); Console.SetCursorPosition(53, 42); Console.Write("GAME speed = " + (int)Snake.speed); }while(gameIsStillRunning); }