-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathProgram.cs
193 lines (179 loc) · 6.25 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
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
using System;
using System.Collections.Generic;
using System.Threading;
namespace Tetris
{
class Program
{
// Settings
static int TetrisRows = 20;
static int TetrisCols = 10;
static int InfoCols = 10;
static int ConsoleRows = 1 + TetrisRows + 1;
static int ConsoleCols = 1 + TetrisCols + 1 + InfoCols + 1;
static List<bool[,]> TetrisFigures = new List<bool[,]>()
{
new bool[,] // ----
{
{ true, true, true, true }
},
new bool[,] // O
{
{ true, true },
{ true, true }
},
new bool[,] // T
{
{ false, true, false },
{ true, true, true },
},
new bool[,] // S
{
{ false, true, true, },
{ true, true, false, },
},
new bool[,] // Z
{
{ true, true, false },
{ false, true, true },
},
new bool[,] // J
{
{ true, false, false },
{ true, true, true }
},
new bool[,] // L
{
{ false, false, true },
{ true, true, true }
},
};
// State
static int Score = 0;
static int Frame = 0;
static int FramesToMoveFigure = 15;
static int CurrentFigureIndex = 2;
static int CurrentFigureRow = 0;
static int CurrentFigureCol = 0;
static bool[,] TetrisField = new bool[TetrisRows, TetrisCols];
static void Main(string[] args)
{
Console.Title = "Tetris v1.0";
Console.CursorVisible = false;
Console.WindowHeight = ConsoleRows + 1;
Console.WindowWidth = ConsoleCols;
Console.BufferHeight = ConsoleRows + 1;
Console.BufferWidth = ConsoleCols;
while (true)
{
Frame++;
// Read user input
if (Console.KeyAvailable)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape)
{
// Environment.Exit(0);
return; // Becase of Main()
}
if (key.Key == ConsoleKey.LeftArrow || key.Key == ConsoleKey.A)
{
// TODO: Move current figure left
CurrentFigureCol--; // TODO: Out of range
}
if (key.Key == ConsoleKey.RightArrow || key.Key == ConsoleKey.D)
{
// TODO: Move current figure right
CurrentFigureCol++; // TODO: Out of range
}
if (key.Key == ConsoleKey.DownArrow || key.Key == ConsoleKey.S)
{
Frame = 1;
Score++;
CurrentFigureRow++;
// TODO: Move current figure down
}
if (key.Key == ConsoleKey.Spacebar || key.Key == ConsoleKey.UpArrow || key.Key == ConsoleKey.W)
{
// TODO: Implement 90-degree rotation of the current figure
}
}
// Update the game state
if (Frame % FramesToMoveFigure == 0)
{
CurrentFigureRow++;
Frame = 0;
}
// // TODO: if (Collision())
// {
// AddCurrentFigureToTetrisField()
// CheckForFullLines()
// if (lines remove) Score++;
// }
// Redraw UI
DrawBorder();
DrawInfo();
// TODO: DrawTetrisField();
DrawCurrentFigure();
Thread.Sleep(40);
}
}
static void DrawBorder()
{
Console.SetCursorPosition(0, 0);
string line = "╔";
line += new string('═', TetrisCols);
/* for (int i = 0; i < TetrisCols; i++)
{
line += "═";
} */
line += "╦";
line += new string('═', InfoCols);
line += "╗";
Console.Write(line);
for (int i = 0; i < TetrisRows; i++)
{
string middleLine = "║";
middleLine += new string(' ', TetrisCols);
middleLine += "║";
middleLine += new string(' ', InfoCols);
middleLine += "║";
Console.Write(middleLine);
}
string endLine = "╚";
endLine += new string('═', TetrisCols);
endLine += "╩";
endLine += new string('═', InfoCols);
endLine += "╝";
Console.Write(endLine);
}
static void DrawInfo()
{
Write("Score:", 1, 3 + TetrisCols);
Write(Score.ToString(), 2, 3 + TetrisCols);
Write("Frame:", 4, 3 + TetrisCols);
Write(Frame.ToString(), 5, 3 + TetrisCols);
}
static void DrawCurrentFigure()
{
var currentFigure = TetrisFigures[CurrentFigureIndex];
for (int row = 0; row < currentFigure.GetLength(0); row++)
{
for (int col = 0; col < currentFigure.GetLength(1); col++)
{
if (currentFigure[row, col])
{
Write("*", row + 1 + CurrentFigureRow, col + 1 + CurrentFigureCol);
}
}
}
}
static void Write(string text, int row, int col, ConsoleColor color = ConsoleColor.Yellow)
{
Console.ForegroundColor = color;
Console.SetCursorPosition(col, row);
Console.Write(text);
Console.ResetColor();
}
}
}