Advertisement
wingman007

C#EventDriven_Snake_2b

Oct 7th, 2014
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Snake2b
  8. {
  9.     class Program
  10.     {
  11.         public enum Direction { Left, Right, Up, Down}
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             ConsoleKeyInfo cki;
  16.             int positionX = 0;
  17.             int positionY = 0;
  18.             Direction direction = Direction.Right;
  19.             char symbol = '*';
  20.             Console.Title = "Arrow keys to steer. Esc to exit.";
  21.             do
  22.             {
  23.                 // 1. loop. Endless loop of the life
  24.                 while (!Console.KeyAvailable)
  25.                 {
  26.                     switch (direction) {
  27.                         case Direction.Right:
  28.                             if (positionX < Console.WindowWidth - 1) positionX++;
  29.                             symbol = '>';
  30.                             break;
  31.                         case Direction.Left:
  32.                             if (positionX > 0) positionX--;
  33.                             symbol = '<';
  34.                             break;
  35.                         case Direction.Up:
  36.                             if (positionY > 0) positionY--;
  37.                             symbol = '^';
  38.                             break;
  39.                         case Direction.Down:
  40.                             if (positionY < Console.WindowHeight) positionY++;
  41.                             symbol = 'v';
  42.                             break;
  43.                     }
  44.  
  45.                     Console.Clear();
  46.                     Console.SetCursorPosition(positionX, positionY);
  47.                     Console.ForegroundColor = ConsoleColor.Gray;
  48.                     // Console.BackgroundColor = ConsoleColor.Yellow;
  49.                     Console.Write(symbol); // "*"
  50.                     // Console.WriteLine("The ifinite loop");
  51.                     System.Threading.Thread.Sleep(150);
  52.                 }
  53.                 // 2. event happend. Meteorit is passing by.
  54.                 cki = Console.ReadKey(true);
  55.                 switch (cki.Key) {
  56.                     case ConsoleKey.RightArrow :
  57.                         direction = Direction.Right;
  58.                         // if (positionX < Console.WindowWidth - 1) positionX++;
  59.                         break;
  60.                     case ConsoleKey.LeftArrow:
  61.                         direction = Direction.Left;
  62.                         // if (positionX > 0) positionX--;
  63.                         break;
  64.                     case ConsoleKey.UpArrow:
  65.                         direction = Direction.Up;
  66.                         // if (positionY > 0) positionY--;
  67.                         break;
  68.                     case ConsoleKey.DownArrow:
  69.                         direction = Direction.Down;
  70.                         // if (positionY < Console.WindowHeight) positionY++;
  71.                         break;
  72.                 }
  73.             } while (cki.Key != ConsoleKey.Escape);
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement