-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (67 loc) · 2.67 KB
/
Copy pathProgram.cs
File metadata and controls
78 lines (67 loc) · 2.67 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
68
69
70
71
72
73
74
75
76
77
78
/*
* Need more work and refactoring
* Status: working (fragile)
*/
using System;
using System.Threading;
using System.Text;
using System.Runtime.InteropServices;
namespace Csnake
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Snake";
const int height = 25;
const int width = 40;
const int init_game_speed = 150;
Coordinator cd = new Coordinator(height,width,init_game_speed);
ConsoleKeyInfo keyinfo;
bool stop_game_flag = false;
while (!stop_game_flag)
{
keyinfo = Console.ReadKey(true);
if(keyinfo.Key == ConsoleKey.Spacebar)
{
cd.StartGame();
while (!cd.GameOver())
{
keyinfo = Console.ReadKey(true);
if (keyinfo.Key == ConsoleKey.W && cd.LastDirection() != direction.up && cd.LastDirection() != direction.down)
{
cd.Direction(direction.up);
}
else if (keyinfo.Key == ConsoleKey.D && cd.LastDirection() != direction.right && cd.LastDirection() != direction.left)
{
cd.Direction(direction.right);
}
else if (keyinfo.Key == ConsoleKey.S && cd.LastDirection() != direction.down && cd.LastDirection() != direction.up)
{
cd.Direction(direction.down);
}
else if (keyinfo.Key == ConsoleKey.A && cd.LastDirection() != direction.left && cd.LastDirection() != direction.right)
{
cd.Direction(direction.left);
}
else if(keyinfo.Key ==ConsoleKey.Escape && cd.GameOver())
{
stop_game_flag = true;
}
else if(keyinfo.Key ==ConsoleKey.R && cd.GameOver())
{
cd.Restart();
break;
}
}
}
else if( keyinfo.Key == ConsoleKey.Escape)
{
stop_game_flag = true;
}
}
Console.Write($"\u001b[{height + 4};{0}H");
Console.CursorVisible = true;
}
}
}