Advertisement
Stephen_MS

snake game

Nov 7th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. class JustSnake
  7. {
  8.     struct Position
  9.     {
  10.         public int X, Y;
  11.         public Position(int x, int y)
  12.         {
  13.             this.X = x;
  14.             this.Y = y;
  15.         }
  16.     }
  17.  
  18.     static void Main(string[] args)
  19.     {
  20.         // List with snake elements
  21.         Queue<Position> snakeElements = new Queue<Position>();
  22.  
  23.         // Random generator for snake food
  24.         Random randomGenerator = new Random();
  25.  
  26.         // The snake speed
  27.         double sleepTime = 100;
  28.  
  29.         // Available move directions
  30.         Position[] moveDirections = new Position[]
  31.         {
  32.             new Position(1, 0), // Right
  33.             new Position(0, 1), // Down
  34.             new Position(-1, 0), // Left
  35.             new Position(0, -1), // Up
  36.         };
  37.  
  38.         // Current direction
  39.         int currentDirection = 0; // 0 = right, 1 = down, 2 = left, 3 = up
  40.            
  41.         // Console settings
  42.         Console.CursorVisible = false;
  43.         Console.BufferHeight = Console.WindowHeight;
  44.  
  45.         // First snake and screen initialize
  46.         Position food = new Position(
  47.             randomGenerator.Next(1, Console.WindowWidth - 1),
  48.             randomGenerator.Next(1, Console.WindowHeight - 1));
  49.         for (int i = 0; i <= 6; i++)
  50.         {
  51.             snakeElements.Enqueue(new Position(i, 0));
  52.         }
  53.         foreach (var item in snakeElements)
  54.         {
  55.             Console.SetCursorPosition(item.X, item.Y);
  56.             Console.ForegroundColor = ConsoleColor.DarkGray;
  57.             Console.Write("*");
  58.         }
  59.         Position snakeHead = snakeElements.Last();
  60.         Console.SetCursorPosition(snakeHead.X, snakeHead.Y);
  61.         Console.ForegroundColor = ConsoleColor.Gray;
  62.         Console.Write("@");
  63.  
  64.         while (true)
  65.         {
  66.             // Read user key
  67.             if (Console.KeyAvailable)
  68.             {
  69.                 ConsoleKeyInfo pressedKey = Console.ReadKey();
  70.                 if (pressedKey.Key == ConsoleKey.RightArrow)
  71.                     if (currentDirection != 2) currentDirection = 0;
  72.                 if (pressedKey.Key == ConsoleKey.DownArrow)
  73.                     if (currentDirection != 3) currentDirection = 1;
  74.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  75.                     if (currentDirection != 0) currentDirection = 2;
  76.                 if (pressedKey.Key == ConsoleKey.UpArrow)
  77.                     if (currentDirection != 1) currentDirection = 3;
  78.             }
  79.  
  80.             // Write snake food
  81.             Console.SetCursorPosition(food.X, food.Y);
  82.             Console.ForegroundColor = ConsoleColor.Yellow;
  83.             Console.Write("+");
  84.  
  85.             // Previous snake head
  86.             Position prevSnakeHead = snakeElements.Last();
  87.  
  88.             // New snake head
  89.             Position newSnakeHead = new Position(
  90.                 prevSnakeHead.X + moveDirections[currentDirection].X,
  91.                 prevSnakeHead.Y + moveDirections[currentDirection].Y);
  92.  
  93.             // Check constraints
  94.             if (newSnakeHead.X >= Console.WindowWidth ||
  95.                 newSnakeHead.X < 0 ||
  96.                 newSnakeHead.Y >= Console.WindowHeight ||
  97.                 newSnakeHead.Y < 0 ||
  98.                 snakeElements.Contains(newSnakeHead))
  99.             {
  100.                 // Game is over
  101.                 Console.SetCursorPosition(0, 0);
  102.                 Console.ForegroundColor = ConsoleColor.Gray;
  103.                 Console.WriteLine("Game over!!! Your points: {0}", snakeElements.Count);
  104.                 return;
  105.             }
  106.  
  107.             // Write new snake head
  108.             Console.SetCursorPosition(prevSnakeHead.X, prevSnakeHead.Y);
  109.             Console.ForegroundColor = ConsoleColor.DarkGray;
  110.             Console.Write("*");
  111.  
  112.             // Add new snake element and draw it on the console
  113.             snakeElements.Enqueue(newSnakeHead);
  114.             Console.SetCursorPosition(newSnakeHead.X, newSnakeHead.Y);
  115.             Console.ForegroundColor = ConsoleColor.Gray;
  116.             Console.Write("@");
  117.  
  118.             // Check if the snake is on food
  119.             if (newSnakeHead.X == food.X && newSnakeHead.Y == food.Y)
  120.             {
  121.                 // Feed the snake (the snake is eating)
  122.                 food = new Position(
  123.                     randomGenerator.Next(1, Console.WindowWidth - 1),
  124.                     randomGenerator.Next(1, Console.WindowHeight - 1));
  125.                 Console.SetCursorPosition(food.X, food.Y);
  126.                 Console.ForegroundColor = ConsoleColor.Yellow;
  127.                 Console.Write("+");
  128.             }
  129.             else
  130.             {
  131.                 // Remove last snake element (the snake is moving)
  132.                 Position p = snakeElements.Dequeue();
  133.                 Console.SetCursorPosition(p.X, p.Y);
  134.                 Console.ForegroundColor = ConsoleColor.DarkGray;
  135.                 Console.Write(" ");
  136.             }
  137.  
  138.             // Slow the motion
  139.             Thread.Sleep((int)sleepTime);
  140.  
  141.             // Change the speed
  142.             sleepTime -= 0.05;
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement