-
Notifications
You must be signed in to change notification settings - Fork 1
Snake Game: Explanation of the codes (Pass Task 4.2)
Explanation of the Snake Game's code
From the line 9-30 is mainly about setting up the game in terms of the start-up display.
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
char ch = '*';
bool gameLive = true;
ConsoleKeyInfo consoleKey;
When the game is started, a text will pop out saying “Press any key to continue”. When a button is pressed, the code char ch = '*', meaning the snake will be the "*".
int x = 0, y = 15;
int dx = 1, dy = 0;
int consoleWidthLimit = 79;
int consoleHeightLimit = 24;
Location and size of display is used in the code int consoleWidthLimit = 79, int consoleHeightLimit = 24, stating the game window width and height. The location for the starting position of the snake is also stated using the int x = 0, y = 2 code, and the direction of the starting snake will go to with the int dx = 1, dy = 0 code.
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.Clear();
The Console.BackgroundColor = ConsoleColor.DarkGray code is used to show the background color of the game state.
int delayInMillisecs = 50;
int delayInMillisecs = 50** code is used to make sure the snake can be seen to the human eye if not the snake would be too fast and uncrackable.
Basically, for lines 31st to 77th, are the settings of the gameplay screen, snake movement as well as the game mechanics.
bool trail = false;
A Boolean as trail (local variable) is set as false to whether to keep trails.
do
{
ConsoleColor cc = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Black;
Console.SetCursorPosition(0, 0);
Console.WriteLine("Arrows move up/down/right/left. Press 'esc' quit.");
Console.SetCursorPosition(x, y);
Console.ForegroundColor = cc;
// see if a key has been pressed
if (Console.KeyAvailable)
{
// get key and use it to set options
consoleKey = Console.ReadKey(true);
switch (consoleKey.Key)
{
case ConsoleKey.UpArrow: //UP
dx = 0;
dy = -1;
Console.ForegroundColor = ConsoleColor.Red;
break;
case ConsoleKey.DownArrow: // DOWN
dx = 0;
dy = 1;
Console.ForegroundColor = ConsoleColor.Cyan;
break;
case ConsoleKey.LeftArrow: //LEFT
dx = -1;
dy = 0;
Console.ForegroundColor = ConsoleColor.Green;
break;
case ConsoleKey.RightArrow: //RIGHT
dx = 1;
dy = 0;
Console.ForegroundColor = ConsoleColor.Black;
break;
case ConsoleKey.Escape: //END
gameLive = false;
break;
}
}
Starting from line 34th to 77th is a combination of five nested IF statement inside a DO WHILE Loop and a nest of Switch Case statement inside an IF statement within the same DO WHILE Loop.
ConsoleColor cc = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Black;
Console.SetCursorPosition(0, 0);
Console.WriteLine("Arrows move up/down/right/left. Press 'esc' quit.");
Console.SetCursorPosition(x, y);
Console.ForegroundColor = cc;
The game will set cursor color as the color of the foreground with the code
ConsoleColor cc = Console.ForegroundColor;
and then set the cursor color in Black with the codeConsole.ForegroundColor = ConsoleColor.Black;
as default. Next, the position of the cursor is fixed with the position of (0, 0) which is the top left corner of the game, and “Arrows move up/down/right/left. Press ‘ESC’ quit.” printed out as the Game Instruction. Then, the cursor will restore its position with the codeConsole.SetCursor Position(x,y);
and save then restore it with the current color.
if (Console.KeyAvailable)
{
// get key and use it to set options
consoleKey = Console.ReadKey(true);
switch (consoleKey.Key)
{
case ConsoleKey.UpArrow: //UP
dx = 0;
dy = -1;
Console.ForegroundColor = ConsoleColor.Red;
break;
case ConsoleKey.DownArrow: // DOWN
dx = 0;
dy = 1;
Console.ForegroundColor = ConsoleColor.Cyan;
break;
case ConsoleKey.LeftArrow: //LEFT
dx = -1;
dy = 0;
Console.ForegroundColor = ConsoleColor.Green;
break;
case ConsoleKey.RightArrow: //RIGHT
dx = 1;
dy = 0;
Console.ForegroundColor = ConsoleColor.Black;
break;
case ConsoleKey.Escape: //END
gameLive = false;
break;
}
}
The codes are referring to the user input tracking, direction color of snake switching, direction controlling function of the game. It is a nest of a Switch Case statement inside an IF statement. In general, the codes detect if a key has been pressed with
Console.KeyAvailable
andconsoleKey
variable set as the user input tracker before the Switch Case statement. Besides, the Switch Case statement will keep tracking the user input and proceed with changes accordingly. For example, if the user pressed the UP button, the direction of the y-axis in the game will -1 which means the snake will move upwards and change the snake color into red and remain until the direction changes again. Also, the user can end the game if Escape Key is detected or pressed.
// find the current position in the console grid & erase the character there if don't want to see the trail
Console.SetCursorPosition(x, y);
if (trail == false)
Console.Write(' ');
// calculate the new position
// note x set to 0 because we use the whole width, but y set to 1 because we use top row for instructions
x += dx;
if (x > consoleWidthLimit)
x = 0;
if (x < 0)
x = consoleWidthLimit;
y += dy;
if (y > consoleHeightLimit)
y = 2; // 2 due to top spaces used for directions
if (y < 2)
y = consoleHeightLimit;
// write the character in the new position
Console.SetCursorPosition(x, y);
Console.Write(ch);
// pause to allow eyeballs to keep up
System.Threading.Thread.Sleep(delayInMillisecs);
Line 78 to 103 shows the codes for the character’s position. From line 78 to line 82, the character’s trail will be erased from the console grid. The statement Console.SetCursorPosition(x, y)
in line 80 will detect the current position of the trail to be erased. The if statement where the trail will be erased by using Console.Write(' ')
if trail Boolean equals to false. From line 86 to line 96, the program will calculate the new position of the character based on the width and height limit of the console. Statements x += dx
and y += dy
are used before their respective if statement. The if statement will determine the value x and y which is the coordinate position of the character. Line 99 and line 100 statements will basically draw the character based on the position of the character. Line 103 statement will pause the character movement for an amount of time-based on the value of delayInMillisecs
.