using System; //the use of System namespace for Console class
using System.Collections.Generic; //Represents a list of strongly typed objects
that can be accessed by the index.
using System.Text; //The ASCII and Unicode character encodings are represented by
classes in this package.
using System.Collections; //Lists, queues, bit arrays, and hash tables are examples
of collections of objects defined by interfaces and classes.
using System.Threading; //in addition to classes for thread synchronisation and
data access
using System.Linq;
namespace Game_Snake
{
struct Location // Structure declration, Contains the information of rows and
cols
{
public Int32 rows;
public Int32 cols;
public Location(Int32 rows, Int32 cols)
{
this.rows = rows; //This keyword is a modifier for the first parameter
of an extension form, and it refers to the current instance of the class.
this.cols = cols;
}
}
class Program
{
static void Main(string[] args) // main function declaration which execute
all the program
{
byte rightposition = 0;// initialize right position is 0
byte leftposition = 1;// initialize left position is 1
byte downposition = 2; // initialize down position is 2
byte upposition = 3; // initialize up position is 3
Int32 food_time = 0; // last diet_snake time
Int32 food_finish_time = 10000; // diet_snake disappear time
Int32 neg_points = 0; // negative points
Location[] directions = new Location[] // structure of array
{
new Location(0, 1), // set right side position
new Location(0, -1), // set left side position
new Location(1, 0), // set down side position
new Location(-1, 0), // set up side position
};
double delay_time = 100; // delay time
Int32 direction = rightposition; // set direction equal to the right
position
Random randm_no = new Random(); // object for random no generation
Console.BufferHeight = Console.WindowHeight; //The aim is to adjust the
default value of Buffer Height to something else, as well as the height of Console
windows.
food_time = Environment.TickCount; //The Environment TickCount function
returns the number of milliseconds since the device began.
List<Location> hurdles = new List<Location>() // list named hurdels
created which contains different positions
{
new Location(10, 10),
new Location(20, 15),
new Location(9, 8),
new Location(20, 20),
new Location(8, 10),
};
foreach (Location hurdle in hurdles) // loop for display the hurdle
positions colored dark cyan
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.SetCursorPosition(hurdle.cols, hurdle.rows);
Console.Write("# "); // show hurdle shape
}
Queue<Location> Elements_of_snakes = new Queue<Location>(); // queue
declaration named element_of_snakes
for (int i = 0; i <= 8; i++) // loop for define the size of snake
{
Elements_of_snakes.Enqueue(new Location(0, i));
}
Location diet_snake; // structure type location named diet_snake
do
{
diet_snake = new Location(randm_no.Next(0,
Console.WindowHeight), //appear food position
randm_no.Next(0, Console.WindowWidth));
}
while (Elements_of_snakes.Contains(diet_snake) ||
hurdles.Contains(diet_snake));
Console.SetCursorPosition(diet_snake.cols, diet_snake.rows);// set
cursor position row and column wise
Console.ForegroundColor = ConsoleColor.DarkMagenta; // set forecolor
darkmagenta
Console.Write("*"); // food shape declare
foreach (Location Location in Elements_of_snakes)
{
Console.SetCursorPosition(Location.cols, Location.rows);
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("*");
}
while (true)
{
neg_points++;
if (Console.KeyAvailable) //The property is used to obtain a value
that indicates whether or not a key press is present in the input stream.
{
ConsoleKeyInfo userInput = Console.ReadKey();
if (userInput.Key == ConsoleKey.LeftArrow) // check left arrow
key pressed by user from keyboard
{
if (direction != rightposition) direction = leftposition;
}
if (userInput.Key == ConsoleKey.RightArrow) // check right
arrow key pressed by user from keyboard
{
if (direction != leftposition) direction = rightposition;
}
if (userInput.Key == ConsoleKey.UpArrow) // check up arrow key
pressed by user from keyboard
{
if (direction != downposition) direction = upposition;
}
if (userInput.Key == ConsoleKey.DownArrow) // check down arrow
key pressed by user from keyboard
{
if (direction != upposition) direction = downposition;
}
}
Location front_snake = Elements_of_snakes.Last(); // set snake
head position
Location next_position = directions[direction]; // set next
position according to the direction
Location snake_newfront = new Location(front_snake.rows +
next_position.rows,
front_snake.cols + next_position.cols); // set new head
position of snake
if (snake_newfront.cols < 0) snake_newfront.cols =
Console.WindowWidth - 1;
if (snake_newfront.rows < 0) snake_newfront.rows =
Console.WindowHeight - 1;
if (snake_newfront.rows >= Console.WindowHeight)
snake_newfront.rows = 0;
if (snake_newfront.cols >= Console.WindowWidth) snake_newfront.cols
= 0;
if (Elements_of_snakes.Contains(snake_newfront) ||
hurdles.Contains(snake_newfront))
{
// if snake strike the hurdle then set the cursor position to
0,0
Console.SetCursorPosition(0, 0);
Console.ForegroundColor = ConsoleColor.Red; // set fore color
red
// display message when end of the game
Console.WriteLine("End of the Game!");
// calculate the score of the player
Int32 Score_User = (Elements_of_snakes.Count - 5) * 100 -
neg_points;
// store maximum score of the player in variable score_user
Score_User = Math.Max(Score_User, 0);
//display the score of the player
Console.WriteLine("Your score is: {0}", Score_User);
//sleep screen
delay_time = 5000;
Thread.Sleep((Int32)delay_time);
return;
}
Console.SetCursorPosition(front_snake.cols, front_snake.rows);
Console.ForegroundColor = ConsoleColor.Yellow; // set fore color to
yellow
Console.Write("*"); // display food
Elements_of_snakes.Enqueue(snake_newfront);
Console.SetCursorPosition(snake_newfront.cols,
snake_newfront.rows);
Console.ForegroundColor = ConsoleColor.Yellow; // set fore color to
yellow
if (direction == rightposition) Console.Write(">");
if (direction == leftposition) Console.Write("<");
if (direction == upposition) Console.Write("^");
if (direction == downposition) Console.Write("v");
if (snake_newfront.cols == diet_snake.cols && snake_newfront.rows
== diet_snake.rows)
{
do
{
// feeding the snake by using random method
diet_snake = new Location(randm_no.Next(0,
Console.WindowHeight),
randm_no.Next(0, Console.WindowWidth));
}
while (Elements_of_snakes.Contains(diet_snake) ||
hurdles.Contains(diet_snake));
food_time = Environment.TickCount;
Console.SetCursorPosition(diet_snake.cols, diet_snake.rows);
Console.ForegroundColor = ConsoleColor.DarkMagenta; // set fore
color to dark magenta
Console.Write("*"); // display food
delay_time--; // delay time decrement by 1
Location hurdle = new Location(); //create hurdle variable
structure typed
do
{
//display hurdles by random method
hurdle = new Location(randm_no.Next(0,
Console.WindowHeight),
randm_no.Next(0, Console.WindowWidth));
}
while (Elements_of_snakes.Contains(hurdle) ||
hurdles.Contains(hurdle) ||
(diet_snake.rows != hurdle.rows && diet_snake.cols !=
hurdle.rows));
// add hurdles on screen when snake feed the foods at random
position
hurdles.Add(hurdle);
Console.SetCursorPosition(hurdle.cols, hurdle.rows);
Console.ForegroundColor = ConsoleColor.DarkCyan; // set fore
color to dark cyan
Console.Write("#"); // display hurdle for snake
}
else
{
// moving the snake...
Location last = Elements_of_snakes.Dequeue();
Console.SetCursorPosition(last.cols, last.rows);
Console.Write(" ");
}
if (Environment.TickCount - food_time >= food_finish_time)
{
neg_points = neg_points + 50;
Console.SetCursorPosition(diet_snake.cols, diet_snake.rows);
Console.Write(" ");
do
{
// display diet of snake at random position by using random
variable
diet_snake = new Location(randm_no.Next(0,
Console.WindowHeight),
randm_no.Next(0, Console.WindowWidth));
}
while (Elements_of_snakes.Contains(diet_snake) ||
hurdles.Contains(diet_snake));
food_time = Environment.TickCount;
}
Console.SetCursorPosition(diet_snake.cols, diet_snake.rows);
Console.ForegroundColor = ConsoleColor.DarkMagenta; // set fore
color to dark magenta
Console.Write("*"); // display snake food
delay_time -= 0.02;
Thread.Sleep((Int32)delay_time); // hold screen
}
}
}
}