Advertisement
stanevplamen

JustSnakeGame

Apr 29th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 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. using System.Collections;
  7. using System.Threading;
  8.  
  9. namespace JustSnake
  10. {
  11.     struct Position
  12.     {
  13.     public int row;
  14.     public int col;
  15.     public Position(int row, int col)
  16.         {
  17.             this.row = row;
  18.             this.col = col;
  19.         }
  20.     }
  21.    
  22.     class Program
  23.     {        
  24.         static void Main(string[] args)
  25.         {
  26.             Position[] directions = new Position[]
  27.             {
  28.                 new Position(0, 1),      // right
  29.                 new Position(0, -1),     // left
  30.                 new Position(1, 0),     // down
  31.                 new Position(-1, 0),     // top
  32.  
  33.             };
  34.             int sleepTime = 100;
  35.             int direction = 0; // 0
  36.             Random randomnumbersGenerator = new Random();
  37.             Console.BufferHeight = Console.WindowHeight;
  38.             Position food = new Position(randomnumbersGenerator.Next(0, Console.WindowHeight), randomnumbersGenerator.Next(0, Console.WindowWidth));
  39.  
  40.  
  41.             Queue<Position> snakeElements = new Queue<Position>();
  42.             for (int i = 0; i <= 5; i++)
  43.             {
  44.                 snakeElements.Enqueue(new Position(0, i));
  45.             }
  46.  
  47.             while (true)
  48.             {
  49.                 if (Console.KeyAvailable)
  50.                 {
  51.                     ConsoleKeyInfo userInput = Console.ReadKey();
  52.                     if (userInput.Key == ConsoleKey.LeftArrow)
  53.                     {
  54.                         direction = 1;
  55.                     }
  56.                     if (userInput.Key == ConsoleKey.RightArrow)
  57.                     {
  58.                         direction = 0;
  59.                     }
  60.                     if (userInput.Key == ConsoleKey.UpArrow)
  61.                     {
  62.                         direction = 3;
  63.                     }
  64.                     if (userInput.Key == ConsoleKey.DownArrow)
  65.                     {
  66.                         direction = 2;
  67.                     }
  68.                     // 0,1,2,3 индексирани от масива
  69.                 }
  70.  
  71.                 Position snakeHead = snakeElements.Last();
  72.                 Position nextDirection = directions[direction];
  73.                 Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);
  74.                 if (snakeNewHead.row < 0 ||
  75.                     snakeNewHead.col < 0 ||
  76.                     snakeNewHead.row >= Console.WindowHeight ||
  77.                     snakeNewHead.col >= Console.WindowWidth ||
  78.                     snakeElements.Contains(snakeNewHead))
  79.                 {
  80.                     Console.SetCursorPosition(0, 0);
  81.                     Console.WriteLine("Game over");
  82.                     Console.WriteLine("Your points are: {0}", ((snakeElements.Count-6) * 100));
  83.                     return; // приключва изпълнението на текущата функция - ще свърши програмата
  84.                 }
  85.                
  86.                 snakeElements.Enqueue(snakeNewHead);
  87.                 if (snakeNewHead.col == food.col && snakeNewHead.row == food.row)
  88.                 {
  89.                     // feeding snake - може да забързваме тук змията
  90.                     food = new Position(randomnumbersGenerator.Next(0, Console.WindowHeight), randomnumbersGenerator.Next(0, Console.WindowWidth));
  91.                     sleepTime--;
  92.  
  93.                 }
  94.                 else
  95.                 {
  96.                     // moving ...
  97.                     snakeElements.Dequeue();
  98.                 }
  99.  
  100.                 Console.Clear();
  101.                 foreach (Position position in snakeElements)
  102.                 {
  103.                     Console.SetCursorPosition(position.col, position.row);
  104.                     Console.WriteLine("*");
  105.                 }
  106.                
  107.                 Console.SetCursorPosition(food.col, food.row);
  108.                 Console.WriteLine("@");
  109.  
  110.                 Thread.Sleep(sleepTime);
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement