![]() ![]() / to get the user points and save the value to text file While (snakeElements.Contains(obstacle) || obstacles.Contains(obstacle) || (food.row = obstacle.row & l = l)) new obstacle will not be drawn at the same row & column of food if snake or obstacles are already at certain position, new obstacle will not be drawn there RandomNumbersGenerator.Next(0, Console.WindowWidth)) Obstacle = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), Public void GenerateNewObstacle(ref Position food, Queue snakeElements, List obstacles) / function to generate new obstacle randomly if conditions put food on different position which is (snakeElements.Contains(food) || obstacles.Contains(food)) Ĭonsole.SetCursorPosition(l, food.row) a loop is created - while the program contains food and the obstacle is not hit RandomNumbersGenerator.Next(0, Console.WindowWidth)) //Food generate within console width Public void GenerateFood(ref Position food, Queue snakeElements, List obstacles)įood = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), //Food generated within console height / function to generate new food randomly with conditions ![]() WinSoundEffect() //thissound effect plays when game wonĬonsole.ForegroundColor = ConsoleColor.Green //Text color for game won initially snake elements has 4, increment 1 by eating 1 food, so eat 3 food to get 7 snake elements Public int WinningCheck(Queue snakeElements, int negativePoints) / Add winning requirement, snake eat 3 food within 30 seconds to win the game UserPoints = Math.Max(userPoints, 0) //if (userPoints Int userPoints = (snakeElements.Count - 4) * 100 - negativePoints //points calculated for player LoseSoundEffect() //this sound effect will be play if game overĬonsole.ForegroundColor = ConsoleColor.Red //Text color for game over If (snakeElements.Contains(snakeNewHead) || obstacles.Contains(snakeNewHead) || (Environment.TickCount-currentTime) > 30000) Public int GameOverCheck(int currentTime, Queue snakeElements, Position snakeNewHead,int negativePoints, List obstacles) / Check for Game over requirements, if snake eats itself, hit on obstacle or did not eat 3 food within 30 seconds, game over If (userInput.Key = ConsoleKey.DownArrow) If (direction != left) direction = right If (userInput.Key = ConsoleKey.RightArrow) If (direction != right) direction = left If (userInput.Key = ConsoleKey.LeftArrow) User key pressed statement: depends on which direction the user want to go to get food or avoid obstacleĬonsoleKeyInfo userInput = Console.ReadKey() Public void CheckUserInput(ref int direction, byte right, byte left, byte down,byte up) / function for reading user direction key input Show the obstacle in the windows with marking of "="Ĭonsole.SetCursorPosition(l, obstacle.row) Obstacles.Add(new Position(randomNumbersGenerator.Next(1, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth))) Random randomNumbersGenerator = new Random() The randomise obstacles will not exist in the first row at the beginning. Create obstacles objects and initialise certain random position of obstacles at every game play Public void InitialRandomObstacles(Listobstacles) / function to place first 5 obstacles randomly when game start Public void Direction(Position directions) / array to store directions, elements in game move depends on user direction key input / function to draw snake body in dark grey "*"Ĭonsole.ForegroundColor = ConsoleColor.DarkGray / function to draw obstacle in cyan "="Ĭonsole.ForegroundColor = ConsoleColor.Cyan PlayerWin.SoundLocation = + "/SnakeWin_1.wav" SoundPlayer playerWin = new SoundPlayer() / function to play the sound effect when player win PlayerLose.Play() //Play the die sound effect after player died PlayerLose.SoundLocation = + "/SnakeLose_1.wav" SoundPlayer playerLose = new SoundPlayer() / function to play sound effect when game over Play the background music at the beginning Locate the SoundPlayer to the correct sound directoryīackgroundMusic.SoundLocation = + "/SnakeBGM_1.wav" SoundPlayer backgroundMusic = new SoundPlayer() Create SoundPlayer objbect to control sound playback / function for playing background music / Define a structure for the position for every object in the game by row and column I have attached the code snippet below of my snake game. I am not sure what should i change in my code to achieve the desired result. ![]() I would like to make the console window screen as wall so that player lose the game if they hit the wall. I want the player to lose when it hits the border of the screen.Right now my snake can go from one side and come out on the other side. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |