public SnakeFragment LastSnakeFragment() { int lastPlace = WholeSnake.Count - 1; SnakeFragment newFragment = new SnakeFragment(WholeSnake[lastPlace].XPosition, WholeSnake[lastPlace].YPosition); return(newFragment); }
public Snake(int x, int y) { Alive = true; StartPositionX = x; StartPositionY = y; DirectionX = 1; DirectionY = 0; List <SnakeFragment> listSnake = new List <SnakeFragment>(); SnakeFragment firstFragment = new SnakeFragment(x, y); listSnake.Add(firstFragment); WholeSnake = listSnake; }
public List <SnakeFragment> NewPositionsForSnakeFragments() { int numberOfFragment = WholeSnake.Count; List <SnakeFragment> lista = new List <SnakeFragment>(); SnakeFragment plats0 = new SnakeFragment((WholeSnake[0].XPosition + DirectionX), (WholeSnake[0].YPosition + DirectionY)); lista.Add(plats0); if (!SnakeGame.AddSnakeFragment) { numberOfFragment--; } for (int i = 0; i < numberOfFragment; i++) { lista.Add(WholeSnake[i]); } return(lista); }
public void AddOneMoreSnakeFragment(SnakeFragment snakeFragment) { WholeSnake.Add(snakeFragment); }
public static void PlaySnake() { bool playAgain = true; Console.OutputEncoding = Encoding.Unicode; while (playAgain) { playAgain = false; Counter = 0; GameBoardSnake.XSize = 25; GameBoardSnake.YSize = 15; Apple apple = new Apple(); Snake snake = new Snake(5, 5); SnakeFragment snakeFragment = new SnakeFragment(0, 0); Console.Clear(); Intro(); Console.ForegroundColor = ConsoleColor.Gray; //Console.WriteLine("\u263A"); Console.WriteLine("\n"); Console.WriteLine("\t\t\t\tTryck valfri tangent för att starta.\n"); Console.ReadKey(true); Console.Clear(); while (snake.Alive) { AddSnakeFragment = false; Console.SetCursorPosition(0, 0); Console.WriteLine("\n\n\n\n"); GameBoardSnake.PrintOutGameBoard(snake.WholeSnake, apple); if (Console.KeyAvailable) { switch (Console.ReadKey(true).Key) { case ConsoleKey.UpArrow: if (snake.DirectionY != 1) { snake.DirectionX = 0; snake.DirectionY = -1; } break; case ConsoleKey.DownArrow: if (snake.DirectionY != -1) { snake.DirectionX = 0; snake.DirectionY = +1; } break; case ConsoleKey.LeftArrow: if (snake.DirectionX != 1) { snake.DirectionX = -1; snake.DirectionY = 0; } break; case ConsoleKey.RightArrow: if (snake.DirectionX != -1) { snake.DirectionX = +1; snake.DirectionY = 0; } break; default: break; } } //if (AddSnakeFragment) //{ snakeFragment = snake.LastSnakeFragment(); } snake.WholeSnake = snake.NewPositionsForSnakeFragments(); //if (AddSnakeFragment) //{ snake.AddOneMoreSnakeFragment(snakeFragment); } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine($"\t\t\t\t\tPOÄNG: {SnakeGame.Counter}"); snake.CheckIfDies(); if (snake.DirectionY != 0) { System.Threading.Thread.Sleep(150); } else { System.Threading.Thread.Sleep(100); } } Console.WriteLine(); Console.WriteLine("\t\t\t\t\tDu är död :(\n\n"); System.Threading.Thread.Sleep(1000); Console.WriteLine("\t\t\t\t\tTryck för att se Highscore."); Console.ReadKey(true); Console.Clear(); HighScoreEachGame.OpenHighscores("highscoresnake.txt", HighScoreEachGame.ListSnake); HighScoreEachGame.PrintOutHighscores("----------------------SNAKE--------------------", HighScoreEachGame.ListSnake); Console.WriteLine(); Console.WriteLine($"\t\t\t\t\tDu fick {SnakeGame.Counter} poäng.\n"); int maybeHighscore = HighScoreEachGame.SeeIfHighscore(HighScoreEachGame.ListSnake, SnakeGame.Counter); if (maybeHighscore < 11) { Console.WriteLine("\t\t\t\t\tGrattis! Du kom in på Highscore-listan!"); HighScoreEachGame.PutHighScoreInList(maybeHighscore, SnakeGame.Counter, HighScoreEachGame.ListSnake); HighScoreEachGame.PutHighScoreInFile("highscoresnake.txt", HighScoreEachGame.ListSnake); Console.Clear(); HighScoreEachGame.OpenHighscores("highscoresnake.txt", HighScoreEachGame.ListSnake); HighScoreEachGame.PrintOutHighscores("----------------SNAKE----------------", HighScoreEachGame.ListSnake); Console.WriteLine(); } else { Console.WriteLine("\t\t\t\t\tBättre lycka nästa gång!\n\n"); } Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("\t\t\t\t\tVill du spela igen?"); Console.WriteLine("\t\t\t\t\t[1] --> JA!"); Console.WriteLine("\t\t\t\t\t[0] --> EXIT."); Console.Write("\t\t\t\t\t"); int choice = InputNumber(0, 1); if (choice == 1) { playAgain = true; } } Console.ReadLine(); }