-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
218 lines (187 loc) · 6.81 KB
/
Program.cs
File metadata and controls
218 lines (187 loc) · 6.81 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
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
using System;
using SFML.Graphics;
using SFML.Window;
using SFML.System;
namespace Tetris
{
class Program
{
const int M = 20;
const int N = 10;
static int[,] field = new int[M, N];
struct Point
{
public int x, y;
}
static Point[] a = new Point[4];
static Point[] b = new Point[4];
static int[,] figures = new int[7, 4]
{
{ 1, 3, 5, 7 }, // I
{ 2, 4, 5, 7 }, // Z
{ 3, 5, 4, 6 }, // S
{ 3, 5, 4, 7 }, // T
{ 2, 3, 5, 7 }, // L
{ 3, 5, 7, 6 }, // J
{ 2, 3, 4, 5 } // O
};
static bool escapeButtonStatus = false;
static bool pause = false;
static uint score = 0;
static bool Check()
{
for (int i = 0; i < 4; i++)
{
if (a[i].x < 0 || a[i].x >= N || a[i].y >= M)
{
return false;
}
if (field[a[i].y, a[i].x] != 0)
{
return false;
}
}
return true;
}
static void Main(string[] args)
{
Random rand = new Random();
RenderWindow window = new RenderWindow(new VideoMode(320, 480), "The Game!");
Texture t1 = new Texture("D:\\Users\\user\\Documents\\Project\\TetrisCSharpSFML\\images/tiles.png");
Texture t2 = new Texture("D:\\Users\\user\\Documents\\Project\\TetrisCSharpSFML\\images/background.png");
Texture t3 = new Texture("D:\\Users\\user\\Documents\\Project\\TetrisCSharpSFML\\images/frame.png");
Sprite s = new Sprite(t1);
Sprite background = new Sprite(t2);
Sprite frame = new Sprite(t3);
int dx = 0;
bool rotate = false;
int colorNum = 1;
float timer = 0, delay = 0.3f;
Clock clock = new Clock();
{
int firstFigure = rand.Next(7);
for (int i = 0; i < 4; i++)
{
a[i].x = figures[firstFigure, i] % 2;
a[i].y = figures[firstFigure, i] / 2;
}
}
while (window.IsOpen)
{
if (!pause || !escapeButtonStatus)
timer += clock.Restart().AsSeconds();
window.DispatchEvents();
window.Closed += (sender, e) => window.Close();
window.LostFocus += (sender, e) => pause = true;
window.GainedFocus += (sender, e) => pause = false;
window.KeyPressed += (sender, e) =>
{
if (e.Code == Keyboard.Key.Escape) escapeButtonStatus = !escapeButtonStatus;
else if (e.Code == Keyboard.Key.Up) rotate = true;
else if (e.Code == Keyboard.Key.Left) dx = -1;
else if (e.Code == Keyboard.Key.Right) dx = 1;
};
if (pause || escapeButtonStatus)
{
System.Threading.Thread.Sleep(250);
continue;
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Down)) delay = 0.05f;
// Move
for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].x += dx; }
if (!Check()) for (int i = 0; i < 4; i++) a[i] = b[i];
// Rotate
if (rotate)
{
Point p = a[1]; // center of rotation
for (int i = 0; i < 4; i++)
{
int x = a[i].y - p.y;
int y = a[i].x - p.x;
a[i].x = p.x - x;
a[i].y = p.y + y;
}
if (!Check()) for (int i = 0; i < 4; i++) a[i] = b[i];
}
// Tick
if (timer >= delay)
{
for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].y += 1; }
if (!Check())
{
for (int i = 0; i < 4; i++) field[b[i].y, b[i].x] = colorNum;
colorNum = 1 + rand.Next(7);
int n = rand.Next(7);
for (int i = 0; i < 4; i++)
{
a[i].x = figures[n, i] % 2;
a[i].y = figures[n, i] / 2;
}
if (!Check())
{
window.Close();
break;
}
}
timer = 0;
}
else
{
System.Threading.Thread.Sleep(10);
}
// Check lines
int k = M - 1;
for (int i = M - 1; i > 0; i--)
{
int count = 0;
for (int j = 0; j < N; j++)
{
if (field[i, j] != 0)
{
count++;
}
field[k, j] = field[i, j];
}
if (count < N)
{
k--;
}
else
{
// increase user's score
score++;
}
}
if (k != 0)
{
Array.Clear(field, 0, N * (k + 1));
}
dx = 0; rotate = false; delay = 0.3f;
// Draw
window.Clear(Color.White);
window.Draw(background);
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
{
if (field[i, j] == 0) continue;
s.TextureRect = new IntRect(field[i, j] * 18, 0, 18, 18);
s.Position = new Vector2f(j * 18, i * 18);
s.Position += new Vector2f(28, 31); // offset
window.Draw(s);
}
for (int i = 0; i < 4; i++)
{
s.TextureRect = new IntRect(colorNum * 18, 0, 18, 18);
s.Position = new Vector2f(a[i].x * 18, a[i].y * 18);
s.Position += new Vector2f(28, 31); // offset
window.Draw(s);
}
window.Draw(frame);
window.Display();
}
Console.WriteLine("Game Over");
Console.WriteLine("Your score: " + score);
Console.ReadKey();
}
}
}