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 Snake1a
- {
- struct Point
- {
- public int x, y;
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- class Program
- {
- public enum Direction { Up, Down, Left, Right };
- static void Main(string[] args)
- {
- // Point a = new Point(10,10);
- // Point b = a;
- // b.x = 15;
- // b.y = 20;
- // Console.WriteLine("a point has x = {0} y = {1}", a.x, a.y);
- // Console.WriteLine("b point has x = {0} y = {1}", b.x, b.y);
- // return;
- ConsoleKeyInfo cki;
- int positionX = 0;
- int positionY = 0;
- Queue<Point> snake = new Queue<Point>();
- for (var i = 0; i < 5; i++) {
- snake.Enqueue(new Point(i, 0));
- }
- // or
- // Point[] snake = new Point[100];
- // 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();
- food = new Point(
- randomNumber.Next(0, Console.WindowWidth - 1),
- randomNumber.Next(0, Console.WindowHeight - 1));
- do
- {
- //1. the endles llop of the life
- while (!Console.KeyAvailable)
- {
- head = snake.Last();
- // my world!!! is here
- switch (direction)
- {
- 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;
- 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.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.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;
- }
- Console.Clear();
- snake.Enqueue(newHead);
- if (food.x == newHead.x && food.y == newHead.y) {
- do {
- food = new Point(
- randomNumber.Next(0, Console.WindowWidth - 1),
- randomNumber.Next(0, Console.WindowHeight - 1));
- } while(snake.Contains(food));
- }
- else
- {
- snake.Dequeue();
- }
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.SetCursorPosition(food.x, food.y);
- Console.Write("@");
- // Console.SetCursorPosition(positionX, positionY);
- Console.ForegroundColor = ConsoleColor.Green;
- // Console.BackgroundColor = ConsoleColor.Blue;
- // Console.Write(symbol);
- for (var 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("*");
- }
- }
- // Console.WriteLine("Endless loop!");
- System.Threading.Thread.Sleep(150);
- }
- // If I am out of my loop there was an Event
- // Take care of the event
- cki = Console.ReadKey(true);
- switch (cki.Key) {
- case ConsoleKey.RightArrow :
- // if (positionX < Console.WindowWidth -1) positionX++;
- direction = Direction.Right;
- break;
- case ConsoleKey.LeftArrow:
- // if (positionX > 0) positionX--;
- direction = Direction.Left;
- break;
- case ConsoleKey.UpArrow:
- // if (positionY > 0) positionY--;
- direction = Direction.Up;
- break;
- case ConsoleKey.DownArrow:
- // if (positionY < Console.WindowHeight) positionY++;
- direction = Direction.Down;
- break;
- }
- } while (cki.Key != ConsoleKey.Escape);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement