public static bool hard = false; // public static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.GetEncoding(1252); //adds special character Console.Title = "Snake Game"; // console typing //Location head = new Location(40, 12); //starting position of head of snake //Location head = new Location(startingCordX, startingCordY); //snake.Add(head); // adds the coords for the head of the snake into the list of locations Console.CursorVisible = false; //turns off the cursor stopping the flicking Console.BufferWidth = Console.WindowWidth = 80; //sets console window width size Console.BufferHeight = Console.WindowHeight = 30; //sets console window height size //direction = Direction.Right; //inital direction initalStar(); // runs inital star variable intro(); // runs title screen - when breaked menu(); // menu screen #region threading Thread thread = new Thread(Move); //adds a thread to move function Thread thread2 = new Thread(drawGrid); //creates a thread for the drawgrid function thread.IsBackground = false; // background thread thread.Start(); //starts the thread #endregion #region key inputs while (true) // whilst the game is active { ConsoleKeyInfo keyInfo = Console.ReadKey(true); //reads the input key though console switch (keyInfo.Key) //switch statement based on key input for player one { case ConsoleKey.Escape: return; case ConsoleKey.UpArrow: direction = Direction.Up; break; case ConsoleKey.DownArrow: direction = Direction.Down; break; case ConsoleKey.LeftArrow: direction = Direction.Left; break; case ConsoleKey.RightArrow: direction = Direction.Right; break; case ConsoleKey.W: directionP2 = DirectionP2.Up; break; case ConsoleKey.S: directionP2 = DirectionP2.Down; break; case ConsoleKey.A: directionP2 = DirectionP2.Left; break; case ConsoleKey.D: directionP2 = DirectionP2.Right; break; } } }
public static void firstRunSetup() { string line; // Line (n) from file string[] cordinates; //string cordinates in array string[] startingCord; //string for storing the starting coordinates in an array int barrierX; //intger for barrier X axis int barrierY; //integer for the barrier Y Axis #region Map One if (mapOne == true) { try { using (StreamReader reader = new StreamReader("MapOne/grid.txt")) { while ((line = reader.ReadLine()) != null) // as long as there is another line { cordinates = line.Split(','); //split the cordinates in the file barrierX = Int32.Parse(cordinates[0]); // store first string as an int in the barrierx variable barrierY = Int32.Parse(cordinates[1]); // store the second string as an int in the barrierY variable Location barriertemp = new Location(barrierX, barrierY); //store both variables in one location , x then y barrier.Add(barriertemp); //add the location into the list of the locations of barrier if (onePlayer == true) { startingCordX = 40; startingCordY = 12; startingDirectonP1 = "RIGHT"; } if (twoPlayer == true) { startingCordX = 20; startingCordY = 12; startingDirectonP1 = "LEFT"; startingCordXP2 = 40; startingCordYP2 = 12; startingDirectonP2 = "RIGHT"; } } } } catch { Console.Clear(); Console.Write("Map Files Not Found"); Console.ReadLine(); } } #endregion #region Map Two if (mapTwo == true) { try { using (StreamReader reader = new StreamReader("MapTwo/grid.txt")) { while ((line = reader.ReadLine()) != null) // as long as there is another line { cordinates = line.Split(','); //split the cordinates in the file barrierX = Int32.Parse(cordinates[0]); // store first string as an int in the barrierx variable barrierY = Int32.Parse(cordinates[1]); // store the second string as an int in the barrierY variable Location barriertemp = new Location(barrierX, barrierY); //store both variables in one location , x then y barrier.Add(barriertemp); //add the location into the list of the locations of barrier startingCordX = 40; startingCordY = 12; startingDirectonP1 = "LEFT"; startingDirectonP2 = "LEFT"; } } } catch { Console.Clear(); Console.Write("Map Files Not Found"); Console.ReadLine(); } } #endregion #region Custom Map if (mapCustom == true) { try { using (StreamReader reader = new StreamReader("Custom/grid.txt")) { while ((line = reader.ReadLine()) != null) // as long as there is another line { cordinates = line.Split(','); //split the cordinates in the file barrierX = Int32.Parse(cordinates[0]); // store first string as an int in the barrierx variable barrierY = Int32.Parse(cordinates[1]); // store the second string as an int in the barrierY variable Location barriertemp = new Location(barrierX, barrierY); //store both variables in one location , x then y barrier.Add(barriertemp); //add the location into the list of the locations of barrier } } } catch { Console.Clear(); Console.Write("Map Files Not Found"); Console.ReadLine(); } try { using (StreamReader reader = new StreamReader("Custom/startingLocation.txt")) { while ((line = reader.ReadLine()) != null) // as long as there is another line { startingCord = line.Split(','); //split the cordinates in the file startingCordX = Int32.Parse(startingCord[0]); // store first string as an int in the barrierx variable startingCordY = Int32.Parse(startingCord[1]); // store the second string as an int in the barrierY variable // reader.Close(); //closes the reader } } } catch { Console.Clear(); Console.Write("Starting Location Not Found"); Console.ReadLine(); } try { using (StreamReader reader = new StreamReader("Custom/startingLocationP2.txt")) { while ((line = reader.ReadLine()) != null) // as long as there is another line { startingCord = line.Split(','); //split the cordinates in the file startingCordXP2 = Int32.Parse(startingCord[0]); // store first string as an int in the barrierx variable startingCordYP2 = Int32.Parse(startingCord[1]); // store the second string as an int in the barrierY variable // reader.Close(); //closes the reader } } } catch { Console.Clear(); Console.Write("Starting Location For Player 2 Not Found"); Console.ReadLine(); } try { using (StreamReader reader = new StreamReader("Custom/p1direction.txt")) { string startingDirectionIn; startingDirectionIn = reader.ReadLine(); startingDirectonP1 = startingDirectionIn.ToUpper(); } } catch { Console.Clear(); Console.Write("Starting Direction Not Found"); Console.ReadLine(); } try { using (StreamReader reader = new StreamReader("Custom/p2direction.txt")) { string startingDirectionIn; startingDirectionIn = reader.ReadLine(); startingDirectonP2 = startingDirectionIn.ToUpper(); } } catch { Console.Clear(); Console.Write("Starting Direction Not Found"); Console.ReadLine(); } } #endregion try { using (StreamReader reader = new StreamReader("highScore.txt")) { while ((line = reader.ReadLine()) != null) // as long as there is another line { // var scores = File.ReadLines("scores.txt"); string[] parts = line.Split(','); string name = parts[0]; int points = int.Parse(parts[1]); scoreList.Add(points); score.Add(new Score(name, points)); } } } catch { Console.Clear(); Console.Write("High Scores Not Found"); Console.ReadLine(); } Location head = new Location(startingCordX, startingCordY); snake.Add(head); // adds the coords for the head of the snake into the list of locations Location headP2 = new Location(startingCordXP2, startingCordYP2); snakeP2.Add(headP2); // adds the coords for the head of the snake into the list of locations switch (startingDirectonP1) { case "RIGHT": direction = Direction.Right; break; case "UP": direction = Direction.Up; break; case "DOWN": direction = Direction.Down; break; case "LEFT": direction = Direction.Left; break; } switch (startingDirectonP2) { case "RIGHT": directionP2 = DirectionP2.Right; break; case "UP": directionP2 = DirectionP2.Up; break; case "DOWN": directionP2 = DirectionP2.Down; break; case "LEFT": directionP2 = DirectionP2.Left; break; } active = true; } // inital setup