Advertisement
wingman007

C#EventDriven_Snake_2a

Oct 7th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 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 Snake2a
  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.             char symbol = '*';
  19.             Direction direction = Direction.Right;
  20.  
  21.             Console.Title = "Arrow keys to stear. Esc to exit.";
  22.             do{
  23.                 // 1. loop
  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; // ConsoleColor.Blue;
  48.                     // Console.BackgroundColor = ConsoleColor.Yellow;
  49.                    
  50.                     Console.Write(symbol);
  51.                     // Console.WriteLine("Infinete loop");
  52.                     System.Threading.Thread.Sleep(150);
  53.                 }
  54.                 // 2. Event happend
  55.                 cki = Console.ReadKey(true);
  56.                 switch (cki.Key) {
  57.                     case ConsoleKey.RightArrow :
  58.                         direction = Direction.Right;
  59.                         // if (positionX < Console.WindowWidth - 1) positionX++;
  60.                         break;
  61.                     case ConsoleKey.LeftArrow:
  62.                         direction = Direction.Left;
  63.                         // if (positionX > 0 ) positionX--;
  64.                         break;
  65.                     case ConsoleKey.UpArrow:
  66.                         direction = Direction.Up;
  67.                         // if (positionY > 0) positionY--;
  68.                         break;
  69.                     case ConsoleKey.DownArrow:
  70.                         direction = Direction.Down;
  71.                         // if (positionY < Console.WindowHeight) positionY++;
  72.                         break;
  73.                 }
  74.             } while(cki.Key != ConsoleKey.Escape);
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement