- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C++实现俄罗斯方块由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了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 。
最后此篇关于C++实现俄罗斯方块的文章就讲到这里了,如果你想了解更多关于C++实现俄罗斯方块的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!