C语言实现俄罗斯方块

时间:2022-11-27 20:37:52

本文实例为大家分享了C语言俄罗斯方块的具体代码,供大家参考,具体内容如下

本代码运行环境是Windows下的VS2013
首先创建tetris.cpp
然后依次创建view.h以及view.cpp、model.h以及model.cpp。

代码如下:

view.h

?
1
2
3
4
5
6
7
8
9
10
11
#pragma once
 
 
#include <stdio.h>
void ShowBackground();
void ShowBrick();
void ShowGame();
void OnLeft();
void OnRight();
void OnUp();
void OnDown();

view.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
#include <stdlib.h>
#include "view.h"
#include "model.h"
void OnLeft()
{//如果能够左移,则左移
 if (IsCanMove(g_nRow, g_nCol - 1))
 {
 g_nCol--;
 ShowGame();
 }
}
 
void OnRight()
{
 if (IsCanMove(g_nRow, g_nCol + 1))
 {
 g_nCol++;
 ShowGame();
 }
}
 
void OnUp()
{
 if (IsCanRotate())
 {
 Rotate();
 ShowGame();
 }
}
 
void OnDown()
{
 if (IsCanMove(g_nRow+1, g_nCol))
 {
 g_nRow++;
 ShowGame();
 }
 else
 {
 //固定方块至背景,并且产生新方块
 CombineBgBrick();
 GetNewBrick();
 //判断游戏是否结束,并给出对应提示
 }
}
 
void ShowGame()
{
 system("cls");
 CombineBgBrick();
 ShowBackground();
 DetachBgBrick();
}
void ShowBrick()
{
 for (size_t i = 0; i < 4; i++)
 {
 for (size_t j = 0; j < 4; j++)
 {
 if (g_chBrick[i][j] == 1)
 {
 printf("■");
 }
 }
 printf("\r\n");
 }
}
 
void ShowBackground()
{
 for (size_t nRow = 0; nRow < GAME_ROWS; nRow++)
 {
 for (size_t nCol = 0; nCol < GAME_COLS; nCol++)
 {
 if (g_chBackground[nRow][nCol] == 1)
 {
 printf("■");
 }
 else
 {
 printf("□");
 }
 }
 printf("\r\n");
 }
}

model.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "model.h"
 
 
char g_chBackground[GAME_ROWS][GAME_COLS];
char g_chBrick[4][4];
int g_nShape = 0; //是长条还是方块,系数为16
int g_nRotate = 0; //朝向,系数为4
int g_nRow = 0;
int g_nCol = 0;
char g_chBrickPool[][4] = {
// 长条
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
 
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
 
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
 
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
 
// T形
1, 1, 1, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
 
0, 1, 0, 0,
1, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
 
0, 1, 0, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,
 
1, 0, 0, 0,
1, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,
 
//L形状
1, 0, 0, 0,
1, 0, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,
 
1, 1, 1, 0,
1, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
 
1, 1, 0, 0,
0, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
 
0, 0, 1, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,
};
 
int IsCanRotate()
{
 char chNextShape[4][4] = { 0 };
 int nNextRotate = (g_nRotate + 1) % 4;
 int nPoolRows = g_nShape * 16 + nNextRotate * 4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 chNextShape[nRow][nCol] = g_chBrickPool[nRow + nPoolRows][nCol];
 }
 }
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (chNextShape[nRow][nCol] == 1)
 {
 if (g_chBackground[nRow + g_nRow][nCol + g_nCol] == 1)
 {
  return 0; //不能移动
 }
 }
 }
 }
 return 1;
}
 
void Rotate()
{
 g_nRotate = (g_nRotate + 1) % 4;
 int nPoolRows = g_nShape * 16 + g_nRotate*4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 g_chBrick[nRow][nCol] = g_chBrickPool[nRow + nPoolRows][nCol];
 }
 }
}
 
int IsCanMove(int nToRow, int nToCol)
{
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (g_chBrick[nRow][nCol] == 1)
 {
 if (g_chBackground[nRow + nToRow][nCol + nToCol] == 1)
 {
  return 0; //不能移动
 }
 }
 }
 }
 return 1;
}
 
void GetNewBrick()
{
 srand((unsigned)time(NULL));
 g_nRow = 0;
 g_nCol = GAME_COLS / 2 - 1;
 int nShapeCount = sizeof(g_chBrickPool) / sizeof(g_chBrickPool[0]) /16;
 g_nShape = rand() % nShapeCount;
 g_nRotate = rand() % 4;
 int nPoolRows = g_nShape * 16 + g_nRotate * 4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 g_chBrick[nRow][nCol] = g_chBrickPool[nRow+nPoolRows][nCol];
 }
 }
}
 
void DetachBgBrick()
{
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (g_chBrick[nRow][nCol] == 1)
 {
 g_chBackground[nRow + g_nRow][nCol + g_nCol] = 0;
 }
 }
 }
}
 
void CombineBgBrick()
{//组合块
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (g_chBrick[nRow][nCol] == 1)
 {
 g_chBackground[nRow+g_nRow][nCol+g_nCol] = 1;
 }
 }
 }
}
 
void InitBackground()
{//初始化背景
 for (size_t nRow = 0; nRow < GAME_ROWS; nRow++)
 {
 for (size_t nCol = 0; nCol < GAME_COLS; nCol++)
 {
 if (nRow == GAME_ROWS - 1
 || nCol == 0
 || nCol == GAME_COLS - 1)
 {
 g_chBackground[nRow][nCol] = 1;
 }
 else
 {
 g_chBackground[nRow][nCol] = 0;
 }
 }
 }
}

model.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
 
#define GAME_ROWS 20
#define GAME_COLS 12
 
extern char g_chBackground[GAME_ROWS][GAME_COLS];
extern char g_chBrick[4][4];
extern int g_nRow;
extern int g_nCol;
 
void InitBackground();
void GetNewBrick();
void CombineBgBrick();
void DetachBgBrick();
int IsCanMove(int nToRow, int nToCol);
void Rotate();
int IsCanRotate();

tetris.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
#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include "model.h"
#include "view.h"
 
 
int main(int argc, char* argv[])
{
 InitBackground();
 GetNewBrick();
 CombineBgBrick();
 ShowBackground();
 DetachBgBrick();
 char chInput = 0;
 clock_t clkStart = clock();
 clock_t clkEnd = clock();
 while (1)
 {
 clkEnd = clock();
 if (clkEnd - clkStart > 1000)
 {
 clkStart = clkEnd;
 OnDown();
 }
 if (_kbhit() != 0)
 {
 chInput = _getch();
 }
 switch (chInput)
 {
 case 'a':
 OnLeft();
 break;
 case 'w':
 OnUp();
 break;
 case 's':
 OnDown();
 break;
 case 'd':
 OnRight();
 break;
 default:
 break;
 }
 chInput = 0;
 }
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43365952/article/details/103222598