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.Tasks;
- namespace Snake2b
- {
- class Program
- {
- struct Point
- {
- public int x, y;
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- public enum Direction { Left, Right, Up, Down}
- static void Main(string[] args)
- {
- /*
- Point a = new Point(10, 10);
- Point b = a;
- b.x = 20;
- b.y = 15;
- Console.WriteLine("a.x = {0} a.y ={1}", a.x, a.y);
- Console.WriteLine("b.x = {0} b.y ={1}", b.x, b.y);
- return;
- */
- // Point[] snake = new Point[100];
- Queue<Point> snake = new Queue<Point>();
- for (int i = 0; i < 5; i++) {
- snake.Enqueue(new Point(i, 0));
- }
- List<Point> obstacles = new List<Point>() {
- new Point(10, 10),
- new Point(70, 15),
- new Point(45, 20)
- };
- ConsoleKeyInfo cki;
- int positionX = 0;
- int positionY = 0;
- // int position1X = 0;
- // int position1Y = 1;
- Direction direction = Direction.Right;
- char symbol = '*';
- Point segment, head, newHead = new Point(0,0), food;
- Random randomNumber = new Random();
- do {
- food = new Point(
- randomNumber.Next(0, Console.WindowWidth - 1),
- randomNumber.Next(0, Console.WindowHeight - 1));
- } while(snake.Contains(food) || obstacles.Contains(food));
- bool gameRunning = true;
- Console.Title = "Arrow keys to steer. Esc to exit.";
- do
- {
- // 1. loop. Endless loop of the life
- while (!Console.KeyAvailable)
- {
- head = snake.Last();
- switch (direction) {
- case Direction.Right:
- // if (positionX < Console.WindowWidth - 1) positionX++;
- // if (positionX == Console.WindowWidth - 1) positionX = 0;
- // positionX++;
- if (head.x == Console.WindowWidth - 1)
- {
- newHead = new Point(0, head.y);
- }
- else {
- newHead = new Point(head.x + 1, head.y);
- }
- symbol = '>';
- break;
- case Direction.Left:
- // if (positionX > 0) positionX--;
- // if (positionX == 0) positionX = Console.WindowWidth - 1;
- // positionX--;
- if (head.x == 0)
- {
- newHead = new Point(Console.WindowWidth - 1, head.y);
- }
- else
- {
- newHead = new Point(head.x - 1, head.y);
- }
- symbol = '<';
- break;
- case Direction.Up:
- // if (positionY > 0) positionY--;
- // if (positionY == 0) positionY = Console.WindowHeight - 1;
- // positionY--;
- if (head.y == 0)
- {
- newHead = new Point(head.x, Console.WindowHeight - 1);
- }
- else
- {
- newHead = new Point(head.x, head.y - 1);
- }
- symbol = '^';
- break;
- case Direction.Down:
- // if (positionY < Console.WindowHeight) positionY++;
- // if (positionY == Console.WindowHeight - 1) positionY = 0;
- // positionY++;
- if (head.y == Console.WindowHeight - 1)
- {
- newHead = new Point(head.x, 0);
- }
- else
- {
- newHead = new Point(head.x, head.y + 1);
- }
- symbol = 'v';
- break;
- }
- snake.Enqueue(newHead);
- // collisian detection food
- if (newHead.x == food.x && newHead.y == food.y)
- {
- do {
- food = new Point(
- randomNumber.Next(0, Console.WindowWidth - 1),
- randomNumber.Next(0, Console.WindowHeight - 1));
- } while(snake.Contains(food) || obstacles.Contains(food));
- }
- else
- {
- snake.Dequeue();
- }
- // collision detection with obstacle
- if (obstacles.Contains(newHead))
- {
- gameRunning = false;
- break;
- }
- Console.Clear();
- // food
- Console.SetCursorPosition(food.x, food.y);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write("@");
- // obstacles
- foreach(Point obstacle in obstacles){
- Console.SetCursorPosition(obstacle.x, obstacle.y);
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.Write("#");
- }
- // Console.SetCursorPosition(positionX, positionY);
- Console.ForegroundColor = ConsoleColor.Green;
- // Console.BackgroundColor = ConsoleColor.Yellow;
- // Console.Write(symbol); // "*"
- // Console.WriteLine("The ifinite loop");
- for (int i = 0; i < snake.Count; i++)
- {
- segment = snake.ElementAt(i);
- Console.SetCursorPosition(segment.x, segment.y);
- if (i == snake.Count - 1)
- {
- Console.Write(symbol);
- }
- else
- {
- Console.Write("*");
- }
- }
- System.Threading.Thread.Sleep(150);
- }
- if (!gameRunning)
- {
- Console.SetCursorPosition(10, 10);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write("Game Over! Hit Esc to exit!");
- }
- // 2. event happend. Meteorit is passing by.
- cki = Console.ReadKey(true);
- switch (cki.Key) {
- case ConsoleKey.RightArrow :
- direction = Direction.Right;
- // if (positionX < Console.WindowWidth - 1) positionX++;
- break;
- case ConsoleKey.LeftArrow:
- direction = Direction.Left;
- // if (positionX > 0) positionX--;
- break;
- case ConsoleKey.UpArrow:
- direction = Direction.Up;
- // if (positionY > 0) positionY--;
- break;
- case ConsoleKey.DownArrow:
- direction = Direction.Down;
- // if (positionY < Console.WindowHeight) positionY++;
- break;
- }
- } while (cki.Key != ConsoleKey.Escape);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement