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 Snake1b
- {
- struct Point
- {
- public int x, y;
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- class Program
- {
- 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));
- }
- ConsoleKeyInfo cki;
- int positionX = 0;
- int positionY = 0;
- // int position1X;
- // int position1Y;
- 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),
- randomNumber.Next(0, Console.WindowHeight));
- Console.Title = "Use arrows to stear! Esc to exit the game.";
- do
- {
- // 1. The endles 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;
- if (head.y == Console.WindowHeight - 1) newHead = new Point(head.x, 0);
- else newHead = new Point(head.x, head.y + 1);
- positionY++;
- symbol = 'V';
- break;
- }
- snake.Enqueue(newHead);
- 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));
- }
- else {
- snake.Dequeue();
- }
- Console.Clear();
- Console.SetCursorPosition(food.x, food.y);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write("@");
- // Console.SetCursorPosition(positionX, positionY);
- Console.ForegroundColor = ConsoleColor.Green;
- // Console.BackgroundColor = ConsoleColor.Blue;
- // Console.Write(symbol);
- // Console.Write("*");
- // Console.WriteLine("Endless 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(50);
- }
- // 2. A meteor is passing by
- cki = Console.ReadKey(true);
- switch (cki.Key) {
- case ConsoleKey.LeftArrow:
- // if (positionX > 0) positionX--;
- direction = Direction.Left;
- break;
- case ConsoleKey.RightArrow:
- // if (positionX < Console.WindowWidth - 1) positionX++;
- direction = Direction.Right;
- 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