Advertisement
wingman007

C#EventDriven_Snake_1b

Oct 6th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 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 Snake1b
  8. {
  9.     class Program
  10.     {
  11.         public enum Direction { Left, Right, Up, Down}
  12.         static void Main(string[] args)
  13.         {
  14.             ConsoleKeyInfo cki;
  15.             int positionX = 0;
  16.             int positionY = 0;
  17.             Direction direction = Direction.Right;
  18.  
  19.             Console.Title = "Use arrows to stear! Esc to exit the game.";
  20.             do
  21.             {
  22.                 // 1. The endles loop of the life
  23.                 while (!Console.KeyAvailable)
  24.                 {
  25.                     switch (direction) {
  26.                         case Direction.Right:
  27.                             if (positionX < Console.WindowWidth - 1) positionX++;
  28.                             break;
  29.                         case Direction.Left:
  30.                             if (positionX > 0) positionX--;
  31.                             break;
  32.                         case Direction.Up:
  33.                             if (positionY > 0) positionY--;
  34.                             break;
  35.                         case Direction.Down:
  36.                             if (positionY < Console.WindowHeight) positionY++;
  37.                             break;
  38.                     }
  39.                     Console.Clear();
  40.                     Console.SetCursorPosition(positionX, positionY);
  41.                     Console.ForegroundColor = ConsoleColor.Gray;
  42.                     // Console.BackgroundColor = ConsoleColor.Blue;
  43.                     Console.Write("*");
  44.                     // Console.WriteLine("Endless loop");
  45.  
  46.                     System.Threading.Thread.Sleep(50);
  47.                 }
  48.  
  49.                 // 2. A meteor is passing by
  50.                 cki = Console.ReadKey(true);
  51.                 switch (cki.Key) {
  52.                     case ConsoleKey.LeftArrow:
  53.                         // if (positionX > 0) positionX--;
  54.                         direction = Direction.Left;
  55.                         break;
  56.                     case ConsoleKey.RightArrow:
  57.                         // if (positionX < Console.WindowWidth - 1) positionX++;
  58.                         direction = Direction.Right;
  59.                         break;
  60.                     case ConsoleKey.UpArrow:
  61.                         // if (positionY > 0) positionY--;
  62.                         direction = Direction.Up;
  63.                         break;
  64.                     case ConsoleKey.DownArrow:
  65.                         // if (positionY < Console.WindowHeight) positionY++;
  66.                         direction = Direction.Down;
  67.                         break;
  68.                 }
  69.  
  70.             } while (cki.Key != ConsoleKey.Escape);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement