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
- {
- public enum Direction { Left, Right, Up, Down}
- static void Main(string[] args)
- {
- ConsoleKeyInfo cki;
- int positionX = 0;
- int positionY = 0;
- Direction direction = Direction.Right;
- char symbol = '*';
- Console.Title = "Arrow keys to steer. Esc to exit.";
- do
- {
- // 1. loop. Endless loop of the life
- while (!Console.KeyAvailable)
- {
- switch (direction) {
- case Direction.Right:
- if (positionX < Console.WindowWidth - 1) positionX++;
- symbol = '>';
- break;
- case Direction.Left:
- if (positionX > 0) positionX--;
- symbol = '<';
- break;
- case Direction.Up:
- if (positionY > 0) positionY--;
- symbol = '^';
- break;
- case Direction.Down:
- if (positionY < Console.WindowHeight) positionY++;
- symbol = 'v';
- break;
- }
- Console.Clear();
- Console.SetCursorPosition(positionX, positionY);
- Console.ForegroundColor = ConsoleColor.Gray;
- // Console.BackgroundColor = ConsoleColor.Yellow;
- Console.Write(symbol); // "*"
- // Console.WriteLine("The ifinite loop");
- System.Threading.Thread.Sleep(150);
- }
- // 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