private static void StartGame() { Graphics.initGraphics(Console.BufferWidth, Console.BufferHeight); Walls walls = new Walls(79, 25, '+'); walls.Draw(); Point point = new Point(4, 5, '*'); Snake snake = new Snake(point, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(Console.BufferWidth, Console.BufferHeight, '$'); Point food = foodCreator.GenerateFood(); food.Draw(); int count = 0; while (game) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); switch (key.Key) { case ConsoleKey.RightArrow: snake.Turn(Direction.RIGHT); break; case ConsoleKey.LeftArrow: snake.Turn(Direction.LEFT); break; case ConsoleKey.UpArrow: snake.Turn(Direction.UP); break; case ConsoleKey.DownArrow: snake.Turn(Direction.DOWN); break; } } snake.Move(); if (snake.isHit(food)) { snake.Grow(); food = foodCreator.GenerateFood(); food.Draw(); count++; } if (snake.isHit(walls.GetPoints()) || snake.isDead(snake.GetPoints())) { snake.Clear(); food.Clear(); GameOver(count); } Thread.Sleep(100); } Console.ReadKey(); }
static void Main(string[] args) { //Рамка Console.SetBufferSize(80, 25); Wall wall = new Wall(80, 25); wall.Draw(); //Точки Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.Right); snake.Draw(); FoodCreator fCreator = new FoodCreator(25, 80, '&'); Point food = fCreator.CreateFood(); food.Draw(); while (true) { if (wall.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { food = fCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.GKey(key.Key); } } WriteGameOver(); Console.ReadLine(); }
public GameController() { WINDOW_HEIGTH = Console.LargestWindowHeight; WINDOW_WIDTH = Console.LargestWindowWidth < (WINDOW_HEIGTH * 2 + STATISTIC_AREA) ? Console.LargestWindowWidth : WINDOW_HEIGTH * 2 + STATISTIC_AREA; delay = 120; Console.BackgroundColor = DEF_BACKGROUND_COLOR; Console.ForegroundColor = DEF_COLOR; Console.CursorVisible = false; Console.SetWindowSize(WINDOW_WIDTH, WINDOW_HEIGTH); Console.SetBufferSize(WINDOW_WIDTH, WINDOW_HEIGTH); //Создание стен this.walls = new walls(0, 0, WINDOW_WIDTH - STATISTIC_AREA - 1, WINDOW_HEIGTH - 1, WALLS); //Создание змейки Point tail = new Point((WINDOW_WIDTH - STATISTIC_AREA - 1) / 2, (WINDOW_HEIGTH - 1) / 2, SNAKE, DEF_COLOR, DEF_BACKGROUND_COLOR); this.snake = new Snake(tail, START_SNAKE_LENGTH, START_SNAKE_DIRECTION); this.foodCreator = new FoodCreator(2, WINDOW_WIDTH - STATISTIC_AREA - 2, 2, WINDOW_HEIGTH - 2, DEF_BACKGROUND_COLOR); this.statistics = new Statistics(delay, this.snake.length); }
static void Main(string[] args) { string name; while (true) { Console.Write("Type your name: "); name = Console.ReadLine(); if (name.Length < 3) { Console.Clear(); Console.WriteLine("Name will be longer than 3 symbols."); continue; } else { Console.Clear(); break; } } bool soundSwitch; while (true) { Console.Write("Do you want to turn on sounds? (Y/N): "); ConsoleKeyInfo answerkey = Console.ReadKey(); if (answerkey.Key == ConsoleKey.Y) { Console.Clear(); soundSwitch = true; break; } else if (answerkey.Key == ConsoleKey.N) { Console.Clear(); soundSwitch = false; break; } else { Console.WriteLine("\nPress \'Y\' or \'N\' key."); continue; } } Score score = new Score(9); Level level = new Level(1); Params settings = new Params(); Sounds sound = new Sounds(settings.GetResourceFolder()); Sounds pointsound = new Sounds(settings.GetResourceFolder()); if (soundSwitch == true) { sound.Play(level.level); } Console.SetWindowSize(80, 26); Walls walls = new Walls(80, 25); walls.Draw(); Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); Console.ForegroundColor = ConsoleColor.Red; snake.Draw(); Console.ForegroundColor = ConsoleColor.White; FoodCreator foodCreator = new FoodCreator(80, 25, '$'); Point food = foodCreator.CreateFood(); Console.ForegroundColor = ConsoleColor.Yellow; food.Draw(); Console.ForegroundColor = ConsoleColor.White; while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food, score, level, sound, soundSwitch)) { if (soundSwitch == true) { pointsound.Play("point"); } food = foodCreator.CreateFood(); Console.ForegroundColor = ConsoleColor.Yellow; food.Draw(); Console.ForegroundColor = ConsoleColor.White; } else { Console.ForegroundColor = ConsoleColor.Red; snake.Move(); Console.ForegroundColor = ConsoleColor.Yellow; } if (level.level == 1) { Thread.Sleep(100); } else if (level.level == 2) { Thread.Sleep(75); } else if (level.level == 3) { Thread.Sleep(50); } else { Thread.Sleep(40); } if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } } Messages gameover = new Messages(); gameover.WriteGameOver(name, score.score, level.level); if (soundSwitch == true) { sound.Stop(level.level); pointsound.Play("lose"); } ConsoleKeyInfo _key = Console.ReadKey(); if (_key.Key == ConsoleKey.Enter) { Application.Restart(); } }
static void Main() { Console.ForegroundColor = ConsoleColor.Green; Console.BackgroundColor = ConsoleColor.Black; Console.SetWindowSize(width, height); Console.WriteLine(" Приветствую в игре повелитель змея"); Console.WriteLine(" Выбери уровень сложности:" + "\n 1 - Easy peasy" + "\n 2 - Medium" + "\n 3 - Death"); int n = 0; n = Convert.ToInt32(Console.ReadLine()); Console.Clear(); if (n == 1) { int x1 = 10; int y1 = 10; walls = new Walls(x1, y1, '*'); snake = new Snake(x1 / 2, y1 / 2, 3); food = new FoodCreator(x1, y1, 'o'); } if (n == 2) { int x1 = 15; int y1 = 15; walls = new Walls(x1, y1, '*'); snake = new Snake(x1 / 2, y1 / 2, 3); //Выбор сложности размера поля food = new FoodCreator(x1, y1, 'o'); } if (n == 3) { int x1 = 20; int y1 = 20; walls = new Walls(x1, y1, '*'); snake = new Snake(x1 / 2, y1 / 2, 3); food = new FoodCreator(x1, y1, 'o'); } Console.SetWindowSize(width + 1, height + 1); Console.SetBufferSize(width + 1, height + 1); Console.CursorVisible = false; food.CreateFood(); switch (n) { case 1: { time = new Timer(Loop, null, 0, 400); break; } case 2: { time = new Timer(Loop, null, 0, 200); //Выбор сложности скорость break; } case 3: { time = new Timer(Loop, null, 0, 100); break; } } while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.Rotation(key.Key); } } }
static void Main(string[] args) { //Console.SetBufferSize(80, 25); Walls walls = new Walls(80, 25); walls.Draw(); //Рамочка HorizontalLines upline = new HorizontalLines(0, 78, 0, '+'); HorizontalLines downline = new HorizontalLines(0, 78, 24, '+'); VerticalLines leftline = new VerticalLines(0, 24, 0, '+'); VerticalLines rightline = new VerticalLines(0, 24, 78, '+'); upline.Draw(); downline.Draw(); leftline.Draw(); rightline.Draw(); // отрисовка точек Point p = new Point(4, 5, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(80, 25, ''); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } } while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } Thread.Sleep(100); snake.Move(); } }
static void Main(string[] args) { Console.SetWindowSize(80, 24); Console.SetBufferSize(80, 24); Menu(); // отрисовка рамки Walls walls = new Walls(80, 24); walls.Draw(); Point p = new Point(4, 4, '*'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Drow(); FoodCreator foodCreator = new FoodCreator(78, 22, '$'); Point food = foodCreator.CreateFood(); food.Draw(); while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { Console.ForegroundColor = ConsoleColor.Red; WriteAt("G A M E O V E R", 34, 12); Console.ResetColor(); Console.ReadLine(); break; } if (snake.Eat(food)) { food = foodCreator.CreateFood(); food.Draw(); } else { snake.Move(); } Thread.Sleep(70); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.HandleKey(key.Key); } Thread.Sleep(70); } Console.ReadKey(); }
static void Main(string[] args) { GameController gameController = new GameController(); gameController.StartScreen(); int hs = Console.LargestWindowHeight; int ws = 2 * hs; int delay = 120; int startSnakeLength = 2; walls walls = gameController.walls; walls.Draw(); Snake snake = gameController.snake; snake.Draw(); Statistics statistics = gameController.statistics; FoodCreator foodCreator = gameController.foodCreator; Food food = foodCreator.CreateNewFood(); food.Draw(); while (true) { if (walls.isHit(snake) || snake.isHitTail()) { break; } if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.direction = gameController.keyHandler(snake.direction, key); } if (snake.HaveEat(food)) { if (food.kind == '-') { snake.throwTail(); snake.throwTail(); } if (food.kind == 'F') { delay = (int)(delay * 0.95); } else if (food.kind == 'S') { delay = (int)(delay / 0.95); } statistics.setStatistics(delay, snake.length); do { food = foodCreator.CreateNewFood(); } while (!walls.isHit(food) && snake.isHit(food)); food.Draw(); } else { snake.Move(); } Thread.Sleep(delay); statistics.printStatistics(ws + 1); } //Console.WriteLine("Hello World!"); Console.WriteLine("GAME OVER"); Console.ReadLine(); }
private static void Start() { Walls walls = new Walls(Width, Height); walls.Draw(); Point p = new Point(4, 5, '@'); Snake snake = new Snake(p, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(Width, Height, '$'); Point food = foodCreator.CreateFood(snake); food.Draw(); int[,] s = new int[Width, Height]; while (true) { if (walls.IsHit(snake) || snake.IsHitTail()) { break; } if (snake.Eat(food)) { do { food = foodCreator.CreateFood(snake); } while (IsColision(snake, food)); food.Draw(); } else { for (int i = 0; i < Width; i++) { for (int j = 0; j < Height; j++) { s[i, j] = (int)Figures.EmptySpace; } } foreach (var item in walls.Points) { s[item.x, item.y] = (int)Figures.Barrier; } s[food.x, food.y] = (int)Figures.Destination; foreach (var item in snake.Points) { var first = snake.Points.Last(); if (item == first) { s[item.x, item.y] = (int)Figures.StartPosition; } else { s[item.x, item.y] = (int)Figures.Barrier; } } var li = new LeeAlgorithm(s); System.Drawing.Point head = new System.Drawing.Point(0, 0); System.Drawing.Point nextstep; if (!li.PathFound) { food = foodCreator.CreateFood(snake); } else { foreach (var item in li.Path) { if (item == li.Path.Last()) { s[item.Item1, item.Item2] = (int)Figures.StartPosition; head = new System.Drawing.Point(item.Item1, item.Item2); } else if (item == li.Path.First()) { s[item.Item1, item.Item2] = (int)Figures.Destination; } else { s[item.Item1, item.Item2] = (int)Figures.Path; } } nextstep = new System.Drawing.Point(li.Path[li.Path.Count - 2].Item1, li.Path[li.Path.Count - 2].Item2); var dir = GetDirection(head, nextstep); switch (dir) { case Direction.LEFT: snake.HandleKey(ConsoleKey.LeftArrow); break; case Direction.RIGHT: snake.HandleKey(ConsoleKey.RightArrow); break; case Direction.UP: snake.HandleKey(ConsoleKey.UpArrow); break; case Direction.DOWN: snake.HandleKey(ConsoleKey.DownArrow); break; } } int speed = 1; snake.Move(); Thread.Sleep(speed); } } WriteGameOver(); Console.ReadLine(); }