本文實例為大家分享了C++實現俄羅斯方塊的具體代碼,供大家參考,具體內容如下
工具:vc++2010,圖庫:EasyX
先看效果圖片
純手寫,沒有面向對象思想,看全部源碼
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
|
#include <stdio.h> #include <graphics.h> #include <time.h> #include <conio.h> #define BLOCK_COUNT 5 #define BLOCK_WIDTH 5 #define BLOCK_HEIGHT 5 #define UNIT_SIZE 20 #define START_X 130 #define START_Y 30 #define KEY_UP 72 #define KEY_RIGHT 77 #define KEY_LEFT 75 #define KEY_SPACE 32 #define KEY_DOWN 76 typedef enum { BLOCK_UP, BLOCK_RIGHT, BLOCK_DOWN, BLOCK_LEFT }block_dir_t; typedef enum { MOVE_DOWN, MOVE_LEFT, MOVE_RIGHT }move_dir_t; int speed = 500; int NextIndex = -1; //下一個方塊種類 int BlockIndex = -1; //當前方塊種類 int score = 0; //分數 int rank = 0; //等級 int visit[30][15]; //訪問數組 int markcolor[30][15]; //表示顏色 int minX = 30; int minY = 30; int color[BLOCK_COUNT]={ GREEN,CYAN,MAGENTA,BROWN,YELLOW }; int block[BLOCK_COUNT*4][BLOCK_HEIGHT][BLOCK_WIDTH] = { //條形方塊 { 0,0,0,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,0,0, 0,0,0,0,0}, //L形方塊 { 0,0,0,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,1,1,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,0,0,0, 0,1,1,1,0, 0,1,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,1,1,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,0,1,0, 0,1,1,1,0, 0,0,0,0,0, 0,0,0,0,0}, //田字型 { 0,0,0,0,0, 0,1,1,0,0, 0,1,1,0,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,1,1,0,0, 0,1,1,0,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,1,1,0,0, 0,1,1,0,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,1,1,0,0, 0,1,1,0,0, 0,0,0,0,0, 0,0,0,0,0}, //T字形方塊 { 0,0,0,0,0, 0,1,1,1,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,0, 0,0,0,1,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,1,0,0, 0,1,1,1,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,1,0,0, 0,0,1,1,0, 0,0,1,0,0, 0,0,0,0,0}, //Z字形方塊 { 0,0,0,0,0, 0,1,1,0,0, 0,0,1,1,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,0, 0,0,1,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,1,1,0,0, 0,0,1,1,0, 0,0,0,0,0, 0,0,0,0,0}, { 0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,0, 0,0,1,0,0, 0,0,0,0,0} }; //歡迎界面 void welcome(){ //初始化畫布 initgraph(550,660); //設置窗口標題 HWND window = GetHWnd(); //獲取窗口 SetWindowText(window,_T( "俄羅斯方塊 GWF" )); //設置窗口標題 //設置文本字體樣式 setfont(0,30,_T( "微軟雅黑" )); setcolor(YELLOW); outtextxy(150,200,_T( "俄羅斯方塊" )); setfont(0,10,_T( "微軟雅黑" )); outtextxy(175,300,_T( "IT編程從俄羅斯方塊開始" )); Sleep(3000); } //初始化游戲屏幕 void initGameScene(){ char str[16]; //清除屏幕 cleardevice(); rectangle(27,27,336,635); rectangle(29,29,334,633); rectangle(370,50,515,195); setfont(24,0,_T( "楷體" )); setcolor(LIGHTGRAY); outtextxy(405,215,_T( "下一個" )); setcolor(RED); outtextxy(405,280,_T( "分數" )); sprintf (str, "%d" ,score); outtextxy(415,310,str); outtextxy(405,375,_T( "等級" )); sprintf (str, "%d" ,rank); outtextxy(415,405,str); setcolor(LIGHTBLUE); outtextxy(390,475, "操作說明" ); outtextxy(390,500, "↑:旋轉" ); outtextxy(390,525, "↓:下降" ); outtextxy(390,550, "→:向右" ); outtextxy(390,575, "←:向左" ); outtextxy(390,600, "空格:暫停" ); } void drawBlock( int x, int y){ //右上角畫出方塊 setcolor(color[NextIndex]); setfont(23,0, "楷體" ); for ( int i= 0;i<BLOCK_HEIGHT;i++){ for ( int j= 0;j<BLOCK_WIDTH;j++){ if (block[NextIndex*4][i][j] == 1){ outtextxy(x+UNIT_SIZE*j,y+UNIT_SIZE*i, "■" ); } } } } void drawBlock( int x, int y, int blockIndex,block_dir_t dir){ //按索引畫出什么方塊指定位置方塊指定方向 setcolor(color[blockIndex]); setfont(23,0, "楷體" ); int id = blockIndex*4+dir; for ( int i= 0;i<BLOCK_HEIGHT;i++){ for ( int j= 0;j<BLOCK_WIDTH;j++){ if (block[id][i][j] == 1){ outtextxy(x+UNIT_SIZE*j,y+UNIT_SIZE*i, "■" ); } } } } void clearBlock( int x, int y){ setcolor(BLACK); setfont(23,0, "楷體" ); for ( int i= 0;i<BLOCK_HEIGHT;i++){ for ( int j= 0;j<BLOCK_WIDTH;j++){ outtextxy(x+UNIT_SIZE*j,y+UNIT_SIZE*i, "■" ); } } } void clearBlock( int x, int y,block_dir_t dir){ setcolor(BLACK); int id = BlockIndex *4+dir; y+=START_Y; for ( int i= 0;i<BLOCK_HEIGHT;i++){ for ( int j= 0;j<BLOCK_WIDTH;j++){ if (block[id][i][j] == 1){ outtextxy(x+UNIT_SIZE*j,y+UNIT_SIZE*i, "■" ); } } } } void nextblock(){ clearBlock(391,71); //清除右上角方塊 //隨機選擇一種方塊 srand ( time (NULL)); //時間函數的返回值產生隨機數種子 NextIndex = rand ()%BLOCK_COUNT; drawBlock(391,71); //畫出方塊 } //如果在指定位置可以向方向移動 int moveable( int x0, int y0,move_dir_t moveDir,block_dir_t blockDir){ int x = (y0 - minY)/UNIT_SIZE; int y = (x0 - minX)/UNIT_SIZE; int id = BlockIndex * 4+blockDir; int ret = 1; if (moveDir == MOVE_DOWN){ for ( int i = 0;i<5;i++){ for ( int j = 0;j<5;j++){ if (block[id][i][j] == 1 &&(x+i+1>=30 || visit[x+i+1][y+j]==1)){ ret=0; } } } } else if (moveDir == MOVE_LEFT){ for ( int i = 0;i<5;i++){ for ( int j = 0;j<5;j++){ if (block[id][i][j] == 1 &&(y+j==0 || visit[x+i][y+j-1]==1)){ ret=0; } } } } else if (moveDir == MOVE_RIGHT){ for ( int i = 0;i<5;i++){ for ( int j = 0;j<5;j++){ if (block[id][i][j] == 1 &&(y+j+1>=15 || visit[x+i][y+j+1]==1)){ ret=0; } } } } return ret; } void failCheck(){ //游戲是否結束 if (!moveable(START_X,START_Y,MOVE_DOWN,BLOCK_UP)){ setcolor(WHITE); setfont(45,0, "隸體" ); outtextxy(75,300, "GAME OVER!" ); Sleep(1000); system ( "pause" ); closegraph(); exit (0); } } int wait( int interval){ //等待 int count = interval/5; for ( int i = 0;i<count;i++){ Sleep(5); if (kbhit()){ return 0; } } } //判斷當前方向是否可以轉到指定方向 int rotatable( int x, int y,block_dir_t dir){ int id= BlockIndex * 4 +dir; int xIndex = (y-minY)/20; int yIndex = (x-minX)/20; if (!moveable(x,y,MOVE_DOWN,dir)){ return 0 ; } for ( int i = 0;i<5;i++){ for ( int j = 0;j<5;j++){ if (block[id][i][j] ==1&&(yIndex+j<0||yIndex+j>=15||visit[xIndex+i][yIndex+j] ==1)){ return 0 ; } } } return 1; } void mark( int x, int y, int blockIndex,block_dir_t dir){ int id = blockIndex*4+dir; int x2 = (y-minY)/20; int y2 = (x-minX)/20; for ( int i=0;i<5;i++){ for ( int j=0;j<5;j++){ if (block[id][i][j] ==1){ visit[x2+i][y2+j]=1; markcolor[x2+i][y2+j]=color[blockIndex]; } } } } void clear_down( int x){ //消除第x行,并把上面的行都下移 for ( int i = x;i>0;i--){ for ( int j=0;j<15;j++){ if (visit[i-1][j]){ //上面有東西 visit[i][j]=1; markcolor[i][j]=markcolor[i-1][j]; setcolor(markcolor[i][j]); outtextxy(20*j+minX,20*i+minY, "■" ); } else { visit[i][j] =0; setcolor(BLACK); outtextxy(20*j+minX,20*i+minY, "■" ); } } } //清除最頂層那一行,就是行標位0的哪一行 setcolor(BLACK); for ( int j = 0;j<15;j++){ visit[0][j] = 0; outtextxy(20*j+minX,minY, "■" ); } } void updataGrade(){ //更新等級提示 //假設:50分一級 char str[10]; rank = score/50; sprintf (str, "%d" ,rank); outtextxy(425,405,str); //更新速度,等級越高速度越快,speed越小 //最慢是500,最快是50 speed = 500-rank*50; if (speed<=0){ speed = 50; } } void addScore( int lines){ //更新分數,line表示消除的行數 char str[32]; setcolor(RED); score+=lines*10; sprintf (str, "%d" ,score); outtextxy(415,310,str); } void check(){ //消去方塊 int i,j; int clearLines =0; for (i=29;i>=0;i--){ for (j=0;j<15 && visit[i][j];j++); //執行到此處有兩種情況, //1.第I行沒有滿,即表示有空位,此時j<15 //2.第i行已經滿了,j就大于等于15 if (j>=15){ //此時第i行已經滿了,就需要消除第i行 clear_down(i); //清除第i行,并把上面的行都下移動 i++; clearLines++; } } //更新分數 addScore(clearLines); //更新等級 updataGrade(); } void move(){ int x = START_X; int y = START_Y; int k = 0; int curSpeed = speed; block_dir_t blockDir = BLOCK_UP; //檢查游戲是否結束 failCheck(); while (1){ if (kbhit()){ int key = getch(); if (KEY_SPACE == key){ getch(); } } //清除當前方塊 clearBlock(x,k,blockDir); if (kbhit()){ int key = getch(); if (KEY_UP == key){ //變形 block_dir_t nextDir = (block_dir_t)((blockDir+1)%4); if (rotatable(x,y+k,nextDir)){ blockDir= nextDir; } } else if (KEY_DOWN == key){ //鄉下加速 curSpeed = 50; } else if (KEY_RIGHT == key){ //右移動 if (moveable(x,y+k+20,MOVE_RIGHT,blockDir)){ x+=20; } } else if (KEY_LEFT == key){ //左移動 if (moveable(x,y+k+20,MOVE_LEFT,blockDir)){ x-=20; } } } k+=20; //繪制當前方塊 drawBlock(x,y+k,BlockIndex,blockDir); wait(curSpeed); //方塊降落到底層的固化處理 if (!moveable(x,y+k,MOVE_DOWN,blockDir)){ mark(x,y+k,BlockIndex,blockDir); break ; } } } void newblock(){ //確定即將使用的方塊 BlockIndex = NextIndex; //繪制方塊從頂部掉下來 drawBlock(START_X,START_Y); //新出現的方塊等一下 Sleep(100); //右上角繪制下一個方塊 nextblock(); //向下降落的動作 move(); } int main ( void ){ welcome(); initGameScene(); //產生新方塊 nextblock(); Sleep(500); memset (visit,0, sizeof (visit)); while (1){ newblock(); //消除滿行,并更新分數和速度 check(); } system ( "pause" ); closegraph(); return 0; } |
分析項目:
1.必須要有歡迎界面
2.搭建合理的邊界,就是游戲范圍
3.邏輯1:先出現右上方的方塊樣式,等待一段時間,將右上方的樣式在游戲區打印出
4.邏輯2,方塊降落,要擦除原先印記,將1改為0
5.邏輯3,熱鍵控制移動方向,暫停及變形,且不能移動出界,判斷方塊是否還能移動
6.邏輯4,方塊凝固在下方不出界
7.邏輯5,最下面一行方塊疊滿了,消去它并把上面的行都下移(分兩種情況),并且再次檢查這一行
8.邏輯6,計算消除行數次數,統計分數,控制休眠時間長度
9.邏輯7,每次移動先判斷是否能移動,默認結束程序,在合理游戲區返回false,結束界面
總結:從界面開始到完整的架構,功能后實現,一步一步,邏輯嚴謹,思路清晰,注釋在代碼里。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/acararduino/article/details/103947960