/
Rackets.cs
257 lines (232 loc) · 8.72 KB
/
Rackets.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
namespace TableTennis
{
using System;
using System.Threading;
public class Rackets
{
public static int firtsRacketY;
public static int secondRacketY;
public static readonly int firstRacketX = Console.WindowWidth - 4;
public static readonly int secondRacketX = 3;
public static bool firstPlayerService = true;
public static ConsoleKeyInfo key;
public static Random randomGenerator = new Random(); //// Move second racket from computer.
public static void ComputerMoveSecondRacket()
{
int randomNumber = randomGenerator.Next(1, 101);
if (randomNumber <= MenuSettings.probability)
{
if (secondRacketY >= Ball.ballPositionY)
{
if (secondRacketY >= Table.offset + 2)
{
MoveSecondRacketUp();
}
}
else if ((secondRacketY + (MenuSettings.racketLength / 2)) < Ball.ballPositionY)
{
if ((secondRacketY + MenuSettings.racketLength) < (Console.WindowHeight - 1))
{
MoveSecondRacketDown();
}
}
}
while (Console.KeyAvailable)
{
key = Console.ReadKey(true);
//// check for ESC and Spacebar
SpecialKeys();
}
}
public static void MoveSecondRacketDown()
{
secondRacketY++;
Console.SetCursorPosition(secondRacketX, secondRacketY - 1);
Console.Write(' ');
DrawSecondRacket();
}
public static void MoveSecondRacketUp()
{
secondRacketY--;
Console.SetCursorPosition(secondRacketX, secondRacketY + MenuSettings.racketLength);
Console.Write(' ');
DrawSecondRacket();
}
public static void DrawFirstRacket()
{
Console.ForegroundColor = ConsoleColor.Blue;
for (int i = firtsRacketY; i < firtsRacketY + MenuSettings.racketLength; i++)
{
// change symbol color for the center of the racket
if (i == firtsRacketY + (MenuSettings.racketLength / 2))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(firstRacketX, i);
Console.Write('#');
Console.ForegroundColor = ConsoleColor.Blue;
}
else
{
Console.SetCursorPosition(firstRacketX, i);
Console.Write('#');
}
}
Console.ResetColor();
}
public static void DrawSecondRacket()
{
Console.ForegroundColor = ConsoleColor.Red;
for (int i = secondRacketY; i < secondRacketY + MenuSettings.racketLength; i++)
{
// change symbol color for the center of the racket
if (i == secondRacketY + (MenuSettings.racketLength / 2))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(secondRacketX, i);
Console.Write('#');
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.SetCursorPosition(secondRacketX, i);
Console.Write('#');
}
}
Console.ResetColor();
}
public static void MoveFirstPlayer()
{
while (Console.KeyAvailable)
{
key = Console.ReadKey(true);
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
FirstPlayerExecuteKey(key);
}
}
public static void MoveSecondPlayer()
{
while (Console.KeyAvailable)
{
key = Console.ReadKey(true);
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
SecondPlayerExecuteKey(key);
}
}
public static void FirstPlayerExecuteKey(ConsoleKeyInfo key)
{
if (key.Key == ConsoleKey.UpArrow)
{
if (firtsRacketY >= Table.offset + 2)
{
firtsRacketY--;
Console.SetCursorPosition(firstRacketX, firtsRacketY + MenuSettings.racketLength);
Console.Write(' ');
DrawFirstRacket();
}
}
if (key.Key == ConsoleKey.DownArrow)
{
if (firtsRacketY + MenuSettings.racketLength < Console.WindowHeight - 1)
{
firtsRacketY++;
Console.SetCursorPosition(firstRacketX, firtsRacketY - 1);
Console.Write(' ');
DrawFirstRacket();
}
}
//// check for ESC and Spacebar
SpecialKeys();
}
public static void SecondPlayerExecuteKey(ConsoleKeyInfo key)
{
if (key.Key == ConsoleKey.W)
{
if (secondRacketY >= Table.offset + 2)
{
secondRacketY--;
Console.SetCursorPosition(secondRacketX, secondRacketY + MenuSettings.racketLength);
Console.Write(' ');
//// MoveSecondRacketUp();
DrawSecondRacket();
}
}
if (key.Key == ConsoleKey.S)
{
if (secondRacketY + MenuSettings.racketLength < Console.WindowHeight - 1)
{
secondRacketY++;
Console.SetCursorPosition(secondRacketX, secondRacketY - 1);
Console.Write(' ');
//// MoveSecondRacketDown();
DrawSecondRacket();
}
}
//// check for ESC or Spacebar
SpecialKeys();
}
public static void NewService()
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Table.firstPass = true;
Table.DrawTable();
Table.PrintResult();
firtsRacketY = Table.offset + 1; //// rackets are in top right and bottom left corners
secondRacketY = Console.WindowHeight - MenuSettings.racketLength - 1;
DrawFirstRacket();
DrawSecondRacket();
if (firstPlayerService)
{
// player one service
Ball.ballPositionX = Console.WindowWidth - 5;
Ball.ballPositionY = Table.offset + (MenuSettings.racketLength / 2) + 1;
Ball.ballCurrentDirectionX = 0; // left
Ball.ballCurrentDirectionY = 2; // down
firstPlayerService = false; // next time player two will start
Ball.ballDirection = "Left";
}
else
{
// player two service
Ball.ballPositionX = 4;
Ball.ballPositionY = Console.WindowHeight - (MenuSettings.racketLength / 2) - 2;
Ball.ballCurrentDirectionX = 2; // right
Ball.ballCurrentDirectionY = 0; // up
firstPlayerService = true; // next time player one will start
Ball.ballDirection = "Right";
}
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(Ball.ballPositionX, Ball.ballPositionY);
Console.Write(Ball.ballSymbol);
Thread.Sleep(2000); //// wait 2 seconds before each new service starts // old value 4000
}
public static void SpecialKeys()
{
if (key.Key == ConsoleKey.Escape)
{
// exit game with escape
Data.ExitGame();
}
if (key.Key == ConsoleKey.Spacebar)
{
Console.SetCursorPosition((Console.WindowWidth / 2) - 2, Console.WindowHeight / 2);
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write("PAUSE");
do
{
key = Console.ReadKey(true);
}
while (key.Key != ConsoleKey.Spacebar);
Console.SetCursorPosition((Console.WindowWidth / 2) - 2, Console.WindowHeight / 2);
Console.Write(" ");
Console.ResetColor();
}
}
}
}