Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (112 loc) · 2.96 KB
/
Copy pathProgram.cs
File metadata and controls
123 lines (112 loc) · 2.96 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Threading;
namespace Snake
{
class Program
{
int Height = 20;
int Wide = 30;
int[] x = new int[50];
int[] y = new int[50];
int fruitX;
int fruitY;
int parts = 3;
ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();
char key = 'W';
Random rnd = new Random();
Program()
{
x[0] = 5;
y[0] = 5;
Console.CursorVisible = false;
fruitX = rnd.Next(2, (Wide - 2));
fruitY = rnd.Next(2, (Height - 2));
}
public void WriteBoard()
{
Console.Clear();
for(int i = 1; i<=(Wide+2); i++)
{
Console.SetCursorPosition(i, 1);
Console.Write("-");
}
for (int i = 1; i <= (Wide + 2); i++)
{
Console.SetCursorPosition(i, (Height+2));
Console.Write("-");
}
for (int i = 1; i <= (Height + 1); i++)
{
Console.SetCursorPosition(1, i);
Console.Write("|");
}
for (int i = 1; i <= (Height + 2); i++)
{
Console.SetCursorPosition((Wide+2),i);
Console.Write("|");
}
}
public void Input()
{
if (Console.KeyAvailable)
{
keyInfo = Console.ReadKey(true);
key = keyInfo.KeyChar;
}
}
public void WritePoint(int x, int y)
{
Console.SetCursorPosition(x, y);
Console.Write("#");
}
public void Logic()
{
if (x[0] == fruitX)
{
if (y[0] == fruitY)
{
parts++;
fruitX = rnd.Next(2, (Wide - 2));
fruitY = rnd.Next(2, (Height - 2));
}
}
for(int i = parts; i>1; i--)
{
x[i - 1] = x[i - 2];
y[i - 1] = y[i - 2];
}
switch (key)
{
case 'w':
y[0]--;
break;
case 's':
y[0]++;
break;
case 'd':
x[0]++;
break;
case 'a':
x[0]--;
break;
}
for(int i =0; i<=(parts-1); i++)
{
WritePoint(x[i], y[i]);
WritePoint(fruitX, fruitY);
}
Thread.Sleep(100);
}
static void Main(string[] args)
{
Program snake = new Program();
while (true)
{
snake.WriteBoard();
snake.Input();
snake.Logic();
}
Console.ReadKey();
}
}
}