本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下
该课程设计用VC++6.0操作如下:
1、文件->新建->文件->左边选C/C++ Header File->右边文件名命名为“tetris.h”->路径假定为桌面文件夹:tetris->确定。然后将下面红色字体标记的“头文件”代码粘贴至其中,保存并退出(或者关闭工作空间)。
2、文件->新建->文件->左边选C/C++ Header File->右边文件名命名为“tetris.h”->路径假定为桌面文件夹:tetris->确定。新建“tetris.c”源文件,将下面“源代码”代码粘贴到其中,保存并退出(或者关闭工作空间)。
3、文件->新建->工程->左边选Win32 Application ->右边工程名称命名为:tetris->路径假定为桌面文件夹:tetris->确定->一个空工程->完成。接下来:工程->增加到工程->文件。这时候,将头文件和源代码添加进去,调试使用。
1.头文件
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
|
//1.自定义枚举类型,定义7种形态的游戏方块
typedef enum tetris_shape
{
ZShape=0,
SShape,
LineShape,
TShape,
SquareShape,
LShape,
MirroredLShape
}shape;
//2.函数声明
//(1)操作方块函数
int maxX(); //取得当前方块的最大x坐标
int minX(); //取得当前方块的最小x坐标
void turn_left(); //当前方块逆时针旋转90度
void turn_right();
int out_of_table();
void transform();
int leftable();
int rightable();
int downable();
void move_left();
void move_right();
//(2)操作游戏桌面的函数
int add_to_table();
void remove_full();
//(3)控制游戏函数
void new_game();
void run_game();
void next_shape();
int random( int seed);
//(4)绘图函数
void paint();
void draw_table();
//(5)其他功能函数
void key_down( WPARAM wParam);
void resize();
void initialize();
void finalize();
//(6)回调函数,用来处理Windows消息
LRESULT CALLBACK WndProc ( HWND , UINT , WPARAM , LPARAM );
|
2.源代码
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
|
//1.文件包含
#include<windows.h>
#include<time.h>
#include<stdio.h>
#include"tetris.h"
//2.常量定义
#define APP_NAME "TETRIS"
#define APP_TITLE "Tetris Game"
#define GAMEOVER "GAME OVER"
#define SHAPE_COUNT 7
#define BLOCK_COUNT 4
#define MAX_SPEED 5
#define COLUMS 10
#define ROWS 20
#define RED RGB(255,0,0)
#define YELLOW RGB(255,255,0)
#define GRAY RGB(128,128,128)
#define BLACK RGB(0,0,0)
#define WHITE RGB(255,255,255)
#define STONE RGB(192,192,192)
#define CHARS_IN_LINE 14
#define SCORE "SCORE %4d"
|
3.全局变量定义
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
//(1)
char score_char[CHARS_IN_LINE]={0};
//(2)
char * press_enter= "Press Enter key..." ;
//(3)帮助提示信息
char *help[]=
{
"press space or up key to transform shape." ,
"Press left or right key to mover shape." ,
"Press down key to speed up." ,
"Press enter key to pause game." ,
"Enjoy it.:-)" ,
0
};
//(4)枚举游戏的状态
enum game_state
{
game_start,
game_run,
game_pause,
game_over,
}state=game_start;
//(5)定义方块的颜色
COLORREF shape_color[]=
{
RGB(255,0,0),
RGB(0,255,0),
RGB(0,0,255),
RGB(255,255,0),
RGB(0,255,255),
RGB(255,0,255),
RGB(255,255,255)
};
//(6)方块的7中类型
int shape_coordinate[SHAPE_COUNT][BLOCK_COUNT][2]=
{
{{0,1},{0,0},{-1,0},{-1,1}},
{{0,-1},{0,0},{1,0},{1,1}},
{{0,-1},{0,0},{0,1},{0,2}},
{{-1,0},{0,0},{1,0},{0,1}},
{{0,0},{1,0},{0,1},{1,1}},
{{-1,-1},{0,-1},{0,0},{0,1}},
{{1,-1},{0,-1},{0,0},{0,1}}
};
//(7)得分
int score=0;
//(8)下一个方块
shape next=0;
//(9)当前方块
shape current=0;
//(10)当前方块的每一部分坐标
int current_coordinate[4][2]={0};
//(11)游戏桌面
int table[ROWS][COLUMS]={0};
//(12)当前方块的x坐标
int shapex=0;
//(13)当前方块的\y坐标
int shapey=0;
//(14)方块下移速度
int speed=0;
//(15)每一帧开始时间
clock_t start=0;
//(16)每一帧结束时间
clock_t finish=0;
//(17)windows绘图用变量
HWND gameWND;
HBITMAP memBM;
HBITMAP memBMOld;
HDC memDC;
RECT clientRC;
HBRUSH blackBrush;
HBRUSH stoneBrush;
HBRUSH shapeBrush[SHAPE_COUNT];
HPEN grayPen;
HFONT bigFont;
HFONT smallFont;
//4.主要处理函数
//(1)取最大坐标
int maxX()
{
int i=0;
int x=current_coordinate[i][0];
int m=x;
for (i=1;i<BLOCK_COUNT;i++)
{
x=current_coordinate[i][0];
if (m<x)
{
m=x;
}
}
return m;
}
//(2)取最小坐标
int minX()
{
int i=0;
int x=current_coordinate[i][0];
int m=x;
for (i=1;i<BLOCK_COUNT;i++)
{
x=current_coordinate[i][0];
if (m>x)
{
m=x;
}
}
return m;
}
//(3)逆时针转动方块
void turn_left()
{
int i=0;
int x,y;
for (i=0;i<4;i++)
{
x=current_coordinate[i][0];
y=current_coordinate[i][1];
current_coordinate[i][0]=y;
current_coordinate[i][1]=-x;
}
}
//(4)顺时针旋转方块
void turn_right()
{
int i=0;
int x,y;
for (i=0;i<4;i++)
{
x=current_coordinate[i][0];
y=current_coordinate[i][1];
current_coordinate[i][0]=-y;
current_coordinate[i][1]=x;
}
}
//(5)检查方块是否越界
int out_of_table()
{
int i=0;
int x,y;
for (i=0;i<4;i++)
{
x=shapex+current_coordinate[i][0];
y=shapey+current_coordinate[i][1];
if (x<0||x>(COLUMS-1)||y>(ROWS-1))
{
return 1;
}
if (table[y][x])
{
return 1;
}
}
return 0;
}
//(6)旋转方块
void transform()
{
if (current==SquareShape)
{
return ;
}
turn_right();
if (out_of_table())
{
turn_left();
}
}
//(7)判断方块是否向左移动
int leftable()
{
int i=0;
int x,y;
for (i=0;i<4;i++)
{
x=shapex+current_coordinate[i][0];
y=shapey+current_coordinate[i][1];
if (x<=0||table[y][x-1]==1)
{
return 0;
}
}
return 1;
}
//(8)判断方块是否向右移动
int rightable()
{
int i=0;
int x,y;
for (i=0;i<4;i++)
{
x=shapex+current_coordinate[i][0];
y=shapey+current_coordinate[i][1];
if (x>=(COLUMS-1)||table[y][x+1]==1)
{
return 0;
}
}
return 1;
}
//(9)判断方块是否向下移动
int downable()
{
int i=0;
int x,y;
for (i=0;i<4;i++)
{
x=shapex+current_coordinate[i][0];
y=shapey+current_coordinate[i][1];
if (y>=(ROWS-1)||table[y+1][x]==1)
{
return 0;
}
}
return 1;
}
//(10)向左移动当前方块
void move_left()
{
if (leftable())
{
shapex--;
}
}
//(11)向右移动当前方块
void move_right()
{
if (rightable())
{
shapex++;
}
}
//(12)向下移动当前方块
void move_down()
{
if (downable())
{
shapey++;
}
else
{
if (add_to_table())
{
remove_full();
next_shape();
}
else
{
state=game_over;
}
}
}
//(13)将当前方块固定到桌面上
int add_to_table()
{
int i=0;
int x,y;
for (i=0;i<4;i++)
{
x=shapex+current_coordinate[i][0];
y=shapey+current_coordinate[i][1];
if (y<0||table[y][x]==1)
{
return 0;
}
table[y][x]=1;
}
return 1;
}
//(14)删除填满的行
void remove_full()
{
int c=0;
int i,j;
for (i=ROWS-1;i>0;i--)
{
c=0;
for (j=0;j<COLUMS;j++)
{
c+=table[i][j];
}
if (c==COLUMS)
{
memmove (table[1],table[0], sizeof ( int )*COLUMS*i);
memset (table[0],0, sizeof ( int )*COLUMS);
score++;
speed=(score/100)%MAX_SPEED;
i++;
}
else if (c==0)
{
break ;
}
}
}
//(15)创建新游戏
void new_game()
{
memset (table,0, sizeof ( int )*COLUMS*ROWS);
start= clock ();
next=random(SHAPE_COUNT);
score=0;
speed=0;
}
//(16)运行游戏
void run_game()
{
finish= clock ();
if ((finish-start)>(MAX_SPEED-speed)*100)
{
move_down();
start= clock ();
InvalidateRect(gameWND,NULL,TRUE);
}
}
//(17)操作当前方块
void next_shape()
{
current=next;
memcpy (current_coordinate,shape_coordinate[next], sizeof ( int )*BLOCK_COUNT*2);
shapex=(COLUMS-((maxX(current)-minX(current))))/2;
shapey=0;
next=random(SHAPE_COUNT);
}
//(18)取随机数
int random( int seed)
{
if (seed==0)
{
return 0;
}
srand ((unsigned) time (NULL));
return ( rand ()%seed);
}
//(19)绘图
void paint()
{
PAINTSTRUCT ps;
HDC hdc;
draw_table();
hdc=BeginPaint(gameWND,&ps);
BitBlt(hdc,clientRC.left,clientRC.top,clientRC.right,clientRC.bottom,memDC,0,0,SRCCOPY);
EndPaint(gameWND,&ps);
}
//(20)绘制游戏桌面
void draw_table()
{
HBRUSH hBrushOld;
HPEN hPenOld;
HFONT hFontOld;
RECT rc;
int x0,y0,w;
int x,y,i,j;
char * str;
w=clientRC.bottom/(ROWS+2);
x0=y0=w;
FillRect(memDC,&clientRC,blackBrush);
// 如果游戏是开始或结束状态
if (state==game_start||state==game_over)
{
memcpy (&rc,&clientRC, sizeof (RECT));
rc.bottom=rc.bottom/2;
hFontOld=SelectObject(memDC,bigFont);
SetBkColor(memDC,BLACK);
//如果游戏是开始状态,用黄色字显示游戏开始画面
if (state==game_start)
{
str=APP_TITLE;
SetTextColor(memDC,YELLOW);
}
//如果游戏是结束状态,用红色字显示GAME OVER
else
{
str=GAMEOVER;
SetTextColor(memDC,RED);
}
DrawText(memDC,str, strlen (str),&rc,DT_SINGLELINE|DT_CENTER|DT_BOTTOM);
SelectObject(memDC,hFontOld);
hFontOld=SelectObject(memDC,smallFont);
rc.top=rc.bottom;
rc.bottom=rc.bottom*2;
if (state==game_over)
{
SetTextColor(memDC,YELLOW);
sprintf (score_char,SCORE,score);
DrawText(memDC,score_char, strlen (score_char),&rc,DT_SINGLELINE|DT_CENTER|DT_TOP);
}
SetTextColor(memDC,STONE);
DrawText(memDC,press_enter, strlen (press_enter),&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
SelectObject(memDC,hFontOld);
return ;
}
//桌面上残留的方块
hBrushOld=SelectObject(memDC,stoneBrush);
for (i=0;i<ROWS;i++)
{
for (j=0;j<COLUMS;j++)
{
if (table[i][j]==1)
{
x=x0+j*w;
y=y0+i*w;
Rectangle(memDC,x,y,x+w+1,y+w+1);
}
}
}
SelectObject(memDC,hBrushOld);
//画当前的方块
hBrushOld=SelectObject(memDC,shapeBrush[current]);
for (i=0;i<4;i++)
{
x=x0+(current_coordinate[i][0]+shapex)*w;
y=y0+(current_coordinate[i][1]+shapey)*w;
if (x<x0||y<y0)
{
continue ;
}
Rectangle(memDC,x,y,x+w+1,y+w+1);
}
SelectObject(memDC,hBrushOld);
//画桌面上的表格线
hPenOld=SelectObject(memDC,grayPen);
for (i=0;i<=ROWS;i++)
{
MoveToEx(memDC,x0,y0+i*w,NULL);
LineTo(memDC,x0+COLUMS*w,y0+i*w);
}
for (i=0;i<=COLUMS;i++)
{
MoveToEx(memDC,x0+i*w,y0,NULL);
LineTo(memDC,x0+i*w,y0+ROWS*w);
}
SelectObject(memDC,hPenOld);
//画玩家得分
x0=x0+COLUMS*w+3*w;
y0=y0+w;
hFontOld=SelectObject(memDC,smallFont);
SetTextColor(memDC,YELLOW);
sprintf (score_char,SCORE,score);
TextOut(memDC,x0,y0,score_char, strlen (score_char));
//画下一个方块
y0+=w;
SetTextColor(memDC,STONE);
TextOut(memDC,x0,y0, "NEXT" ,4);
x0+=w;
y0+=2*w;
hBrushOld=SelectObject(memDC,shapeBrush[next]);
for (i=0;i<4;i++)
{
x=x0+shape_coordinate[next][i][0]*w;
y=y0+shape_coordinate[next][i][1]*w;
Rectangle(memDC,x,y,x+w+1,y+w+1);
}
SelectObject(memDC,hBrushOld);
//打印帮助信息
x0=(COLUMS+2)*w;
y0+=4*w;
SetTextColor(memDC,GRAY);
i=0;
while (help[i])
{
TextOut(memDC,x0,y0,help[i], strlen (help[i]));
y0+=w;
i++;
}
SelectObject(memDC,hFontOld);
}
//(21)处理按键
void key_down( WPARAM wParam)
{
//如果游戏不是运行状态,按下回车键
if (state!=game_run)
{
if (wParam==VK_RETURN)
{
switch (state)
{
case game_start:
next_shape();
state=game_run;
break ;
case game_pause:
state=game_run;
break ;
case game_over:
new_game();
next_shape();
state=game_run;
break ;
}
}
}
//如果游戏状态是运行
else
{
switch (wParam)
{
case VK_SPACE:
case VK_UP:
transform();
break ;
case VK_LEFT:
move_left();
break ;
case VK_RIGHT:
move_right();
break ;
case VK_DOWN:
move_down();
break ;
case VK_RETURN:
state=game_pause;
break ;
}
}
InvalidateRect(gameWND,NULL,TRUE);
}
//(22)改变窗口大小
void resize()
{
HDC hdc;
LOGFONT lf;
hdc=GetDC(gameWND);
GetClientRect(gameWND,&clientRC);
SelectObject(memDC,memBMOld);
DeleteObject(memBM);
memBM=CreateCompatibleBitmap(hdc,clientRC.right,clientRC.bottom);
memBMOld=SelectObject(memDC,memBM);
DeleteObject(bigFont);
memset (&lf,0, sizeof (LOGFONT));
lf.lfWidth=(clientRC.right-clientRC.left)/CHARS_IN_LINE;
lf.lfHeight=(clientRC.bottom-clientRC.top)/4;
lf.lfItalic=1;
lf.lfWeight=FW_BOLD;
bigFont=CreateFontIndirect(&lf);
DeleteObject(smallFont);
lf.lfHeight=clientRC.bottom/(ROWS+2);
lf.lfWidth=lf.lfHeight/2;
lf.lfItalic=0;
lf.lfWeight=FW_NORMAL;
smallFont=CreateFontIndirect(&lf);
ReleaseDC(gameWND,hdc);
}
//(23)处理消息
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SIZE:
resize();
return 0;
case WM_ERASEBKGND:
return 0;
case WM_PAINT:
paint();
return 0;
case WM_KEYDOWN:
key_down(wParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
//其他消息用Windows默认的消息处理函数处理
return DefWindowProc(hwnd,message,wParam,lParam);
}
//(24)初始化
void initialize()
{
LOGFONT lf;
HDC hdc;
int i;
hdc=GetDC(gameWND);
GetClientRect(gameWND,&clientRC);
memDC=CreateCompatibleDC(hdc);
memBM=CreateCompatibleBitmap(hdc,clientRC.right,clientRC.bottom);
memBMOld=SelectObject(memDC,memBM);
blackBrush=CreateSolidBrush(BLACK);
stoneBrush=CreateSolidBrush(STONE);
//创建每个方块对应颜色的画笔
for (i=0;i<SHAPE_COUNT;i++)
{
shapeBrush[i]=CreateSolidBrush(shape_color[i]);
}
grayPen=CreatePen(PS_SOLID,1,GRAY);
memset (&lf,0, sizeof (LOGFONT));
//创建一个大字体
lf.lfWidth=(clientRC.right-clientRC.left)/CHARS_IN_LINE;
lf.lfHeight=(clientRC.bottom-clientRC.top)/4;
lf.lfItalic=0;
lf.lfWeight=FW_NORMAL;
smallFont=CreateFontIndirect(&lf);
ReleaseDC(gameWND,hdc);
}
//(25)释放资源
void finalize()
{
int i=0;
DeleteObject(blackBrush);
DeleteObject(stoneBrush);
for (i=0;i<SHAPE_COUNT;i++)
{
DeleteObject(shapeBrush[i]);
}
DeleteObject(grayPen);
DeleteObject(bigFont);
DeleteObject(smallFont);
SelectObject(memDC,memBMOld);
DeleteObject(memBM);
DeleteObject(memDC);
}
//(26) 入口函数
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MSG msg;
WNDCLASS wndclass;
//设置窗口样式
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=( HBRUSH )GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=APP_NAME;
RegisterClass(&wndclass);
//创建Windows窗口
gameWND=CreateWindow(APP_NAME,
APP_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,NULL,
hInstance,NULL);
initialize();
ShowWindow(gameWND,iCmdShow);
UpdateWindow(gameWND);
new_game();
for (;;)
{
if (state==game_run)
{
run_game();
}
//判断是否有Windows消息
if (PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{
if (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
break ;
}
}
}
finalize();
return msg.wParam;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。