-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (62 loc) · 2.16 KB
/
Copy pathProgram.cs
File metadata and controls
67 lines (62 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Text;
namespace SnakeGame
{
internal class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8; // Change the output encoding of the Console
Game game = new Game(); // Create new Game object
Console.WriteLine("(1) Easy mode:");
Console.WriteLine("(2) Normal mode:");
Console.WriteLine("(3) Hard mode:\n");
Console.WriteLine("Select mode:");
string mode = Console.ReadLine();
int speed = 50;
switch (mode)
{
case "1":
speed = 100;
break;
case "2":
speed = 50;
break;
default:
speed = 25;
break;
}
var inputTask = Task.Run(() => GetInput(game)); // Run the GetInput method asynchronously
while (!game.isGameOver) // Repeat until the player tries to eat themselves xd
{
Thread.Sleep(speed); // 50 best
game.GameTick();
}
Thread.Sleep(1000);
Console.WriteLine("GAME OVER!");
}
static void GetInput(Game game)
{
while (true)
{
// Read keyboard key
var key = Console.ReadKey(intercept: true).Key;
// Determine which key is pressed then set the move direction of the snake.
switch (key)
{
case ConsoleKey.UpArrow:
game.SetMoveDirection(MoveDirection.Up);
break;
case ConsoleKey.DownArrow:
game.SetMoveDirection(MoveDirection.Down);
break;
case ConsoleKey.LeftArrow:
game.SetMoveDirection(MoveDirection.Left);
break;
case ConsoleKey.RightArrow:
game.SetMoveDirection(MoveDirection.Right);
break;
}
}
}
}
}