-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
140 lines (129 loc) · 4.26 KB
/
Program.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ConsoleSimpleWorm.Game_components;
namespace ConsoleSimpleWorm
{
class Program
{
static public Config Conf;
public const byte WormLen = 3;
static void AutoSetting()
{
Conf = null;
do
{
try
{
Conf = new Config();
}
catch (Exception)
{
ConfigFile.CreateConfigFileDefault();
}
} while (Conf == null);
Console.SetWindowSize(Conf.FieldWidth, Conf.FieldHeight + 2);
Console.SetBufferSize(Conf.FieldWidth, Conf.FieldHeight + 2);
Console.SetCursorPosition(0, Conf.FieldHeight+1);
for (int i = 0; i < Conf.FieldWidth; i++) Console.Write("█");
}
static void PrintWormText(Worm worm)
{
for (int i = 0; i < worm.Length; i++)
{
Console.WriteLine("\t{0}: {1} x {2}", i, worm[i].X, worm[i].Y);
}
Console.WriteLine();
}
static void PrintWorm(Worm worm)
{
for (int i = 0; i < worm.Length; i++)
{
Console.SetCursorPosition(worm[i].X, worm[i].Y);
Console.WriteLine(Conf.WormSymbol);
}
}
static void PrintAction(WormElement newhead, WormElement oldend)
{
Console.SetCursorPosition(newhead.X, newhead.Y);
Console.WriteLine(Conf.WormSymbol);
if (oldend==null)
return;
Console.SetCursorPosition(oldend.X, oldend.Y);
Console.WriteLine(" ");
}
static void PrintFood(Element pos)
{
Console.SetCursorPosition(pos.X, pos.Y);
Console.Write(Conf.FoodSymbol);
}
static void GameStart()
{
AutoSetting();
Direction dir = Direction.Right;
ConsoleKeyInfo cki = new ConsoleKeyInfo();
Worm worm = new Worm(WormLen, (byte)((Conf.FieldWidth - WormLen) / 2), (byte)(Conf.FieldHeight / 2));
Food food = new Food(Conf.FieldHeight, Conf.FieldWidth);
food.GenerateNewPos(ref worm);
PrintWorm(worm);
PrintFood(food.Element);
while (true)
{
if (Console.KeyAvailable == true)
{
cki = Console.ReadKey(true);
KeyToDir(cki.Key, ref dir);
}
if(!worm.Go(dir, ref food))
{
Console.Clear();
Console.WriteLine("The end");
Console.ReadLine();
return;
}
if((worm[0].X == food.Element.X) && (worm[0].Y == food.Element.Y))
{
if(!food.GenerateNewPos(ref worm))
{
Console.Clear();
Console.WriteLine("You win");
Console.ReadLine();
return;
}
PrintFood(food.Element);
}
PrintAction(worm[0], worm.DeletedElement);
Thread.Sleep(200);
}
}
static void KeyToDir(ConsoleKey key, ref Direction dir)
{
switch(key)
{
case ConsoleKey.UpArrow:
if(dir != Direction.Down)
dir = Direction.Up;
break;
case ConsoleKey.DownArrow:
if (dir != Direction.Up)
dir = Direction.Down;
break;
case ConsoleKey.RightArrow:
if (dir != Direction.Left)
dir = Direction.Right;
break;
case ConsoleKey.LeftArrow:
if (dir != Direction.Right)
dir = Direction.Left;
break;
}
}
static void Main(string[] args)
{
GameStart();
}
}
}