public static void printAtLocation(String text, Location loc) { int row = loc.locationArray[0]; int col = loc.locationArray[1]; Console.Clear(); for (int j = 0; j < col; j++) { Console.WriteLine(); } for (int i = 0; i < row; i++) { Console.Write(" "); } Console.Write(text); }
public void testKeys(ConsoleKeyInfo key) { if (key != null && key.Key == ConsoleKey.DownArrow) { loc = new Location(loc.loc[0], loc.loc[1] + 1); } else if (key != null && key.Key == ConsoleKey.UpArrow) { loc = new Location(loc.loc[0], loc.loc[1] - 1); } else if (key != null && key.Key == ConsoleKey.LeftArrow) { loc = new Location(loc.loc[0]-1, loc.loc[1]); } else if (key != null && key.Key == ConsoleKey.RightArrow) { loc = new Location(loc.loc[0]+1, loc.loc[1]); } }
static void Main(string[] args) { string mysnake = "Snake"; Console.OutputEncoding = System.Text.Encoding.GetEncoding(1252); Console.ForegroundColor = ConsoleColor.Green; Console.Title = mysnake; Console.WriteLine(mysnake); Console.ForegroundColor = ConsoleColor.White; Location head = new Location(40, 12); List<Location> snake = new List<Location>(); snake.Add(head); Location next; Location star = new Location(60, 20); while (true) { next = snake[0]; Console.Clear(); foreach (Location location in snake) { Console.SetCursorPosition(head.X, head.Y); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write((char)178); } Console.SetCursorPosition(star.X, star.Y); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("*"); Console.ResetColor(); ConsoleKeyInfo keyInfo = Console.ReadKey(true); switch (keyInfo.Key) { case ConsoleKey.Escape: return; case ConsoleKey.UpArrow: if (head.Y > 0) head.Y--; break; case ConsoleKey.DownArrow: if (head.Y < 24) head.Y++; break; case ConsoleKey.LeftArrow: if (head.X > 0) head.X--; break; case ConsoleKey.RightArrow: if (head.X < 79) head.X++; break; } snake.Insert(0, next); if (next.X == star.X && next.Y == star.Y) { Random random = new Random(); star.X = random.Next(0, 80); star.Y = random.Next(0, 25); } else { snake.RemoveAt(snake.Count - 1); } } }
public Snake() { loc = new Location(0, 0); }
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