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 Snake2a
- {
- class Program
- {
- public enum Direction { Left, Right, Up, Down }
- static void Main(string[] args)
- {
- ConsoleKeyInfo cki;
- int positionX = 0;
- int positionY = 0;
- char symbol = '*';
- Direction direction = Direction.Right;
- Console.Title = "Arrow keys to stear. Esc to exit.";
- do{
- // 1. loop
- 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; // ConsoleColor.Blue;
- // Console.BackgroundColor = ConsoleColor.Yellow;
- Console.Write(symbol);
- // Console.WriteLine("Infinete loop");
- System.Threading.Thread.Sleep(150);
- }
- // 2. Event happend
- 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