Snake
Snake
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
while (true)
{
neg_points++;
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");
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
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;