Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Snake
- {
- class Program
- {
- public enum Direction { Left, Right, Up, Down}
- struct Point
- {
- public int x, y;
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- static void Main(string[] args)
- {
- ConsoleKeyInfo cki;
- // int positionX = 0;
- // int positionY = 0;
- Direction direction = Direction.Right;
- Queue<Point> snake = new Queue<Point>();
- for (var i = 0; i < 5; i++) {
- snake.Enqueue(new Point(0,i));
- }
- // or
- // int[][] snakeArray = new int[100][]; // nut will be difficult
- Random randomNumber = new Random();
- Point segment, head, newHead = new Point(0, 0),
- food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
- // newObstacle;
- List<Point> obstacles = new List<Point>() {
- new Point(10, 20),
- new Point(34, 12),
- new Point(67, 15)
- };
- bool collisionWithObstacle = false;
- double delay = 150.0;
- // foreach (Point obstacle in obstacles)
- // {
- // Console.SetCursorPosition(obstacle.x, obstacle.y);
- // Console.ForegroundColor = ConsoleColor.Blue;
- // Console.Write("#");
- // }
- char symbol = '>';
- // Console.WriteLine(Console.WindowHeight); // 25
- // Console.WriteLine(Console.WindowWidth); // 80
- // Console.SetCursorPosition(Console.WindowWidth - 1, Console.WindowHeight - 1);
- // Console.SetCursorPosition(0,0);
- // Console.Write("$");
- // return;
- // System.Console.WriteLine(Environment.TickCount);
- // return;
- int foodCounter = 0;
- int lastFeedingTime = Environment.TickCount;
- int score = 0;
- Console.Title = "Use the Arrow keys to steer! Esc to exit the fame!";
- do
- {
- while (!Console.KeyAvailable)
- {
- foodCounter++;
- head = snake.Last();
- switch (direction) {
- case Direction.Right :
- symbol = '>';
- // if (positionX == Console.WindowWidth - 1) positionX = 0;
- if (head.x == Console.WindowWidth - 1) newHead = new Point(0, head.y);
- else newHead = new Point(head.x + 1, head.y);
- break;
- case Direction.Left :
- symbol = '<';
- // if (positionX == 0) positionX = Console.WindowWidth - 1;
- if (head.x == 0) newHead = new Point(Console.WindowWidth - 1, head.y);
- else newHead = new Point(head.x - 1, head.y);
- break;
- case Direction.Up :
- symbol = '^';
- // if (positionY == 0) positionY = Console.WindowHeight;
- // else positionY--;
- if (head.y == 0) newHead = new Point(head.x, Console.WindowHeight -1);
- else newHead = new Point(head.x, head.y - 1);
- break;
- case Direction.Down :
- symbol = 'V';
- // if (positionY == Console.WindowHeight) positionY = 0;
- // else positionY++;
- if (head.y == Console.WindowHeight - 1) newHead = new Point(head.x,0);
- else newHead = new Point(head.x, head.y + 1);
- break;
- }
- snake.Enqueue(newHead);
- // collision with food
- if (food.x == newHead.x && food.y == newHead.y) {
- // generate new food
- // Make sure the food is not on the head of the snake
- // while (food.x == newHead.x && food.y == newHead.y)
- // {
- // food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
- // }
- // 2. the food is not on the body or obstacles
- do {
- food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
- } while (snake.Contains(food) || obstacles.Contains(food)); // the food is not going to be on an obstacle or vody of the snake
- // 3. the food is not on the head and obstacles
- // do
- // {
- // food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
- // } while ((food.x == newHead.x && food.y == newHead.y) || obstacles.Contains(food));
- /*
- // add obstacle if neccessasry
- do
- {
- newObstacle = new Point(
- randomNumber.Next(0,Console.WindowHeight - 1),
- randomNumber.Next(0, Console.WindowWidth - 1));
- }
- while (snake.Contains(newObstacle) || obstacles.Contains(newObstacle) ||
- (food.x != newObstacle.x && food.y != newObstacle.y));
- obstacles.Add(newObstacle);
- */
- // Console.SetCursorPosition(newObstacle.x, newObstacle.y);
- // Console.ForegroundColor = ConsoleColor.Cyan;
- // Console.Write("=");
- // foodCounter = 0;
- foodCounter = 0;
- lastFeedingTime = Environment.TickCount;
- }
- else {
- snake.Dequeue();
- }
- // detect colisions with obstacles
- foreach (Point obstacle in obstacles) {
- if (obstacle.x == newHead.x && obstacle.y == newHead.y) {
- collisionWithObstacle = true;
- }
- }
- /*
- // the time for food elapsed
- // 1.
- if (foodCounter == 80) {
- foodCounter = 0;
- do
- {
- food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
- } while (snake.Contains(food) || obstacles.Contains(food)); // the food is not going to be on an obstacle or vody of the snake
- }
- */
- // 2.
- if (Environment.TickCount - lastFeedingTime >= 8000)
- {
- lastFeedingTime = Environment.TickCount;
- do
- {
- food = new Point(randomNumber.Next(0, Console.WindowWidth - 1), randomNumber.Next(0, Console.WindowHeight - 1));
- } while (snake.Contains(food) || obstacles.Contains(food)); // the food is not going to be on an obstacle or vody of the snake
- }
- Console.Clear();
- // display food
- Console.SetCursorPosition(food.x, food.y);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write("@");
- // display obstalces
- foreach (Point obstacle in obstacles)
- {
- Console.SetCursorPosition(obstacle.x, obstacle.y);
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.Write("#");
- }
- // Console.SetCursorPosition(positionX, positionY);
- // Console.ForegroundColor = ConsoleColor.Blue;
- Console.ForegroundColor = ConsoleColor.Green;
- // Console.Write(symbol);
- // for (var i = 0; i < snake.LongCount(); i++) {
- for (var i = 0; i < snake.Count; i++) {
- segment = snake.ElementAt(i);
- Console.SetCursorPosition(segment.x,segment.y);
- // if (i == snake.LongCount() - 1) Console.Write(symbol);
- if (i == snake.Count - 1) Console.Write(symbol);
- else Console.Write("*"); ;
- }
- if (collisionWithObstacle) break;
- // Console.SetCursorPosition(0,0);
- // Console.Write(Environment.TickCount);
- // Thread.Sleep(150);
- if (delay > 50) delay -= 0.1;
- Thread.Sleep((int)delay);
- }
- if (collisionWithObstacle) {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.SetCursorPosition(10, 10);
- Console.Write("GAME OVER! Score: {0}\n Hit Escape to Exit the game. Press \"R\" to retry!", (snake.Count - 6) * 100);
- }
- cki = Console.ReadKey(true);
- switch (cki.Key) {
- case ConsoleKey.LeftArrow :
- if (direction != Direction.Right) direction = Direction.Left; // don't allow th snake to crawl over her tail
- // direction = Direction.Left;
- break;
- case ConsoleKey.RightArrow :
- if (direction != Direction.Left) direction = Direction.Right;
- // direction = Direction.Right;
- break;
- case ConsoleKey.UpArrow :
- if (direction != Direction.Down) direction = Direction.Up;
- // direction = Direction.Up;
- break;
- case ConsoleKey.DownArrow :
- if (direction != Direction.Up) direction = Direction.Down;
- // direction = Direction.Down;
- break;
- }
- // the player wants to try again
- if (collisionWithObstacle && cki.Key == ConsoleKey.R)
- {
- // handle an event in the game. The infinite loop has been left
- collisionWithObstacle = false;
- // new snake with 5 segments
- snake.Clear();
- for (var i = 0; i < 5; i++)
- {
- snake.Enqueue(new Point(0, i));
- }
- delay = 150.0;
- }
- } while (cki.Key != ConsoleKey.Escape);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement