-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
602 lines (524 loc) · 12.6 KB
/
main.cpp
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include <windows.h>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <cstring>
struct point
{
public:
short x;
short y;
};
const int w = 15;
const int h = 30;
const point obj[7][4] =
{
{{0,0},{1,0},{-1,0},{0,1}},
{{0,0},{1,0},{1,1},{0,1}},
{{0,-1},{0,0},{0,1},{0,2}},
{{0,0},{-1,0},{0,1},{1,1}},
{{0,0},{1,0},{0,1},{-1,1}},
{{0,0},{-1,0},{0,-1},{0,-2}},
{{0,0},{1,0},{0,-1},{0,-2}}
};
const COLORREF clrTable[6] = {RGB(90,90,90),RGB(0,255,0),RGB(255,0,0),RGB(0,0,255),RGB(255,255,0),RGB(0,255,255)};
point cur[4];
WORD screenW,screenH;
RECT workSpace,ClnArea;
int rectsize;
point fieldCords[w][h];
BYTE field[w][h] = {};
BYTE figNum = 0;
BYTE curClr = 1;
int points = 0, maxpoints;
HWND hWnd;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
namespace Drawing
{
void resizeField();
void drawRect(HDC hdc,short x, short y);
void drawEl(HDC hdc,WORD x, WORD y);
void setZone(RECT*,short,short,short,short);
void setZonePos(RECT*, short,short);
void fillrct(HDC hdc,short x, short y,HBRUSH hb);
}
namespace Controls
{
void MoveDown();
void MoveLeft();
void MoveRight();
void rotateF();
}
void newFig();
void Copy(point*,point*);
void UpdateScore();
void NewGame();
void read();
void write();
void formOutPut(char*);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wc = {};
wc.cbSize=sizeof(wc);
wc.hInstance=hThisInstance;
wc.lpszClassName="MainWindow";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadCursor(NULL, IDC_NO);
wc.lpfnWndProc=WindowProcedure;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOWTEXT);
if(!RegisterClassEx(&wc))
MessageBox(NULL,"Can't register window class","Error",MB_OK | MB_ICONERROR);
hwnd = CreateWindowEx(0,
"MainWindow",
"Tetris",
WS_OVERLAPPEDWINDOW,
0,0,500,500,NULL,NULL,hThisInstance,NULL);
if(!hwnd)
MessageBox(NULL,"Can't create window","Error",MB_OK | MB_ICONERROR);
hWnd = hwnd;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,hwnd,NULL,NULL))
{
DispatchMessage(&msg);
}
return msg.message;
}
short xc,yc,hill = 0;
point last[4];
short pensize = 1;
bool pressedUP=false;
bool pressedLEFT=false;
bool pressedRIGHT=false;
bool pressedDOWN=false;
bool fullscreen = false;
bool stars = false;
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_ERASEBKGND:
{
HDC hdc = GetDC(hwnd);
FillRect(hdc,&ClnArea,(HBRUSH)COLOR_CAPTIONTEXT);
HBRUSH brush = CreateSolidBrush(clrTable[0]);
FillRect(hdc,&workSpace,brush);
HGDIOBJ orig = SelectObject(hdc,brush);
for(int i=0; i<h; i++)
for(int j=0; j<w; j++)
if(field[j][i])
{
brush = CreateSolidBrush(clrTable[field[j][i]]);
DeleteObject(SelectObject(hdc,brush));
Drawing::drawRect(hdc,j,i);
}
DeleteObject(SelectObject(hdc,orig));
SwapBuffers(hdc);
ReleaseDC(hwnd,hdc);
return 0;
break;
}
case WM_CREATE:
{
newFig();
read();
SetTimer(hwnd,1,200,NULL);
SetTimer(hwnd,2,50,NULL);
return 0;
break;
}
case WM_SIZE:
{
screenW = LOWORD(lParam);
screenH = HIWORD(lParam);
if(screenW/w*h>screenH)
rectsize = screenH/h;
else
rectsize = screenW/w;
pensize = rectsize/10;
SetRect(&ClnArea,0,0,screenW,screenH);
Drawing::resizeField();
return 0;
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd,&ps);
HBRUSH brush = CreateSolidBrush(clrTable[0]);
HGDIOBJ orig = SelectObject(hdc,brush);
for(int i=0; i<4; i++)
Drawing::fillrct(hdc,last[i].x,last[i].y,brush);
DeleteObject(SelectObject(hdc,orig));
Drawing::drawEl(hdc,xc,yc);
char buff[50] = "Score: ";
formOutPut(buff);
DrawText(hdc,buff,-1,&ClnArea,0);
SwapBuffers(hdc);
EndPaint(hwnd,&ps);
for(int i=0; i<4; i++)
{
last[i].x = cur[i].x+xc;
last[i].y = cur[i].y+yc;
}
return 0;
break;
}
case WM_KEYUP:
{
switch(wParam)
{
case VK_UP:
pressedUP=false;
break;
case VK_DOWN:
pressedDOWN=false;
break;
case VK_LEFT:
pressedLEFT=false;
break;
case VK_RIGHT:
pressedRIGHT=false;
break;
}
return 0;
break;
}
case WM_KEYDOWN:
{
WORD lol = HIWORD(lParam);
if(wParam==VK_UP)
if(!pressedUP)
{
Controls::rotateF();
pressedUP=true;
}
switch(wParam)
{
case VK_DOWN:
{
pressedDOWN=true;
break;
}
case VK_LEFT:
{
if(lol>500)pressedLEFT=true;
if(!pressedLEFT)Controls::MoveLeft();
break;
}
case VK_RIGHT:
{
if(lol>500)pressedRIGHT=true;
if(!pressedRIGHT)Controls::MoveRight();
break;
}
case VK_CONTROL:
{
NewGame();
break;
}
case VK_F1:
{
stars = !stars;
break;
}
case VK_ESCAPE:
{
PostQuitMessage(0);
break;
}
case VK_BACK:
{
fullscreen=!fullscreen;
if(fullscreen)
{
SetWindowLong(hwnd,GWL_STYLE,WS_POPUP);
ShowWindow(hwnd,SW_SHOWMAXIMIZED);
}
else
{
SetWindowLong(hwnd,GWL_STYLE,WS_OVERLAPPEDWINDOW);
ShowWindow(hwnd,SW_SHOWNORMAL);
SetWindowPos(hwnd,NULL,0,0,300,300,0);
}
}
}
return 0;
break;
}
case WM_TIMER:
{
switch(wParam)
{
case 1:
{
if(!pressedDOWN)
Controls::MoveDown();
break;
}
case 2:
{
if(pressedDOWN)
{
Controls::MoveDown();
points++;
}
if(pressedLEFT)Controls::MoveLeft();
if(pressedRIGHT)Controls::MoveRight();
if(stars)
{
HDC hdc = GetDC(hwnd);
for(int i=0;i<workSpace.left;i++)
SetPixel(hdc,i,50,RGB(255,255,0));
ReleaseDC(hwnd,hdc);
}
break;
}
}
return 0;
break;
}
case WM_CLOSE:
{
if(points>maxpoints)
maxpoints = points;
write();
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
}
void read()
{
FILE *fp;
fp = fopen("score.data", "rb");
fread(&maxpoints,4,1,fp);
fclose(fp);
}
void write()
{
FILE *fp;
fp = fopen("score.data", "wb");
fwrite(&maxpoints,4,1,fp);
fclose(fp);
}
void NewGame()
{
for(int i=0; i<h; i++)
for(int j=0; j<w; j++)
field[j][i] = 0;
if(points>maxpoints)
maxpoints = points;
write();
points = 0;
newFig();
InvalidateRect(hWnd,NULL,true);
}
void formOutPut(char* buff)
{
char score[10];
char maxscore[10];
itoa(points,score,10);
itoa(maxpoints,maxscore,10);
strcat(buff,score);
strcat(buff," Record: ");
strcat(buff,maxscore);
}
inline bool checkLine(short y)
{
for(int j=0; j<w; j++)
if(!field[j][y])
return false;
return true;
}
void checkForPoints()
{
short high = 0;
bool filled = true;
bool redraw = false;
while(filled)
{
filled = false;
for(int i=high; i<h; i++)
if(checkLine(i))
{
high = i;
filled = true;
}
if(filled)
{
for(int i=high; i>0; i--)
for(int j=0; j<w; j++)
field[j][i] = field[j][i-1];
points += 100;
redraw = true;
}
}
InvalidateRect(hWnd,NULL,redraw);
}
void newFig()
{
xc = w / 2;
yc = -2;
srand(time(NULL));
figNum = rand() % 7;
curClr = 1+rand() % 5;
for(int i = 0; i<4; i++)
{
cur[i] = obj[figNum][i];
last[i].x = cur[i].x+xc;
last[i].y = cur[i].y+yc;
}
checkForPoints();
}
bool checkForColl(point* arr,short x,short y)
{
for(int i=0; i<4; i++)
{
if(arr[i].y+y<0)
return false;
if(field[arr[i].x+x][arr[i].y+y])
return true;
}
return false;
}
void Drawing::fillrct(HDC hdc,short x,short y,HBRUSH hb)
{
if(y<0)
return;
RECT r;
WORD left = fieldCords[x][y].x;
WORD top = fieldCords[x][y].y;
SetRect(&r,left,top,left+rectsize,top+rectsize);
FillRect(hdc,&r,hb);
}
void Drawing::resizeField()
{
WORD width = rectsize*w;
WORD height = rectsize*h;
workSpace.left = (screenW-width)/2-pensize*2;
workSpace.top = (screenH-height)/2;
workSpace.right = workSpace.left + width;
workSpace.bottom= workSpace.top + height;
int x=workSpace.left;
int y=workSpace.top;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
fieldCords[j][i].x = x;
fieldCords[j][i].y = y;
x+=rectsize;
}
x=workSpace.left;
y+=rectsize;
}
InvalidateRect(hWnd,NULL,true);
}
void fall()
{
for(int i=0; i<4; i++)
{
field[cur[i].x+xc][cur[i].y+yc] = curClr;
if(cur[i].y+yc<0)
{
NewGame();
break;
}
}
newFig();
}
void Controls::MoveDown()
{
for(int i=0; i<4; i++)
if(yc+1+cur[i].y>=h )
{
fall();
return;
}
if(checkForColl(cur,xc,yc+1))
{
fall();
return;
}
yc++;
InvalidateRect(hWnd,NULL,false);
}
bool FalloutX(short x)
{
for(int i=0; i<4; i++)
if(x+cur[i].x<0 || x+cur[i].x>=w)
return true;
return false;
}
void Controls::MoveLeft()
{
if(!FalloutX(xc-1) && !checkForColl(cur,xc-1,yc))
{
xc--;
InvalidateRect(hWnd,&workSpace,false);
}
}
void Controls::MoveRight()
{
if(!FalloutX(xc+1)&& !checkForColl(cur,xc+1,yc))
{
xc++;
InvalidateRect(hWnd,&workSpace,false);
}
}
void Copy(point* f,point* s)
{
for(int i=0; i<4; i++)
f[i] = s[i];
}
void Controls::rotateF()
{
if(figNum==1)
return;
short off = 0;
point temp[4];
for(int i=0; i<4; i++)
{
temp[i]=cur[i];
short t = temp[i].x;
temp[i].x = -temp[i].y;
temp[i].y = t;
if(yc+temp[i].y>=h)
return;
if(xc+temp[i].x<0 || xc+temp[i].x>=w)
if(off<abs(temp[i].x))
off = temp[i].x;
}
if(!checkForColl(temp,xc-off,yc))
{
xc-=off;
Copy(cur,temp);
InvalidateRect(hWnd,&workSpace,false);
}
}
void Drawing::drawRect(HDC hdc, short x, short y)
{
if(y<0)
return;
WORD left = fieldCords[x][y].x;
WORD top = fieldCords[x][y].y;
Rectangle(hdc,left,top,left+rectsize,top+rectsize);
}
void Drawing::drawEl(HDC hdc,WORD x, WORD y)
{
HBRUSH brush = CreateSolidBrush(clrTable[curClr]);
HGDIOBJ orig = SelectObject(hdc,brush);
for(int i=0; i<4; i++)
Drawing::drawRect(hdc,cur[i].x+x,cur[i].y+y);
DeleteObject(SelectObject(hdc,orig));
}