服务器之家

服务器之家 > 正文

C++实现推箱子小游戏源码

时间:2021-07-31 15:25     来源/作者:小水瓶和大水瓶

本文实例为大家分享了C++实现推箱子小游戏的具体代码,供大家参考,具体内容如下

功能尚为完善。

?
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
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include<iostream>
#include<windows.h>
#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0
using namespace std;
 
void Map();
void PlayerMove();
void Menu();
void BoxMove();
 
//extern int Array[15][20];
//定义菜单状况
int nMenuState = 1;
//定义选关设置
int nSetState = 1;
//定义关卡状态
int PassState = 1;
//定义选关情况
bool bslect = false;
 
int Array1[15][20] = {
 { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,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,1,1,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0,0,0,0,0,1,0,0,0,0,0,0,0,1 },
 { 1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1 },
 { 1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1 },
 { 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1 },
 { 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 },
 { 1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1 },
 { 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 },
 { 1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1 },
 { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
};
int Array[15][20] = {};
 
 
struct Player {
 int P_row;
 int P_col;
 int P_rowBk;
 int P_colBk;
 Player() {
 P_row = 0;
 P_col = 0;
 P_rowBk = 0;
 P_colBk = 0;
 }
 Player(int x = 10, int y = 10) {
 P_row = x;
 P_col = y;
 P_rowBk = x;
 P_colBk = y;
 }
 void BackUp() {
 P_rowBk = P_row;
 P_colBk = P_col;
 }
 void restore() {
 P_row = P_rowBk;
 P_col = P_colBk;
 }
};
//Player splayer(1, 1); //初始化玩家坐标
Player splayer = {1,1};
 
struct Box {
 int box_x;
 int box_y;
 int box_xx;//用来备份坐标
 int box_yy;
 Box() {
 box_x = 0;
 box_y = 0;
 box_xx = 0;
 box_yy = 0;
 }
 Box(int x = 10, int y = 10) {
 box_x = x;
 box_y = y;
 box_xx = box_x;
 box_yy = box_y;
 }
 void BackUp() {
 box_xx = box_x;
 box_yy = box_y;
 }
 void restore() {
 box_x = box_xx;
 box_y = box_yy;
 }
};
//第一关箱子坐标初始化
Box sbox[3] = {
 { 5,5 },
 { 6,6 },
 { 7,7 }
};
 
//坑结构体
struct Destinate {
 int Des_x;
 int Des_y;
 Destinate(int a, int b) {
 Des_x = a;
 Des_y = b;
 }
};
Destinate sdestinate[3] = {
 { 8,8 },
 { 9,9 },
 { 10,10 }
};
 
//选关设置
void SetChoise() {
 while (1) {
 system("cls");
 if (KEY_DOWN(VK_UP)) {
 //cout << "向上被按下" << endl;
 --nSetState;
 if (nSetState < 1) { nSetState = 3; }
 }
 if (KEY_DOWN(VK_DOWN)) {
 //cout << "向下被按下" << endl;
 ++nSetState;
 if (nSetState > 3) { nSetState = 1; }
 }
 
 switch (nSetState) {
 case 1: cout << "********游戏选关设置********" << endl;
 cout << " -> 1、关卡一" << endl;
 cout << " 2、关卡二" << endl;
 cout << " 3、关卡三" << endl;
 system("pause");
 break;
 case 2: cout << "********游戏选关设置********" << endl;
 cout << " 1、关卡一" << endl;
 cout << " -> 2、关卡二" << endl;
 cout << " 3、关卡三" << endl;
 system("pause");
 break;
 case 3: cout << "********游戏选关设置********" << endl;
 cout << " 1、关卡一" << endl;
 cout << " 2、关卡二" << endl;
 cout << " -> 3、关卡三" << endl;
 system("pause");
 break;
 }
 if (KEY_DOWN(VK_RETURN)) {
 if (1 == nSetState) {
 memcpy(Array, Array1, sizeof(Array));
 splayer.P_row = 1;
 splayer.P_col = 1;
 PassState = 1;
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 5 + i;
 sbox[i].box_y = 5 + i;
 sdestinate[i].Des_x = 8 + i;
 sdestinate[i].Des_y = 8 + i;
 }
 }
 else if (2 == nSetState) {
 memcpy(Array, Array2, sizeof(Array));
 splayer.P_row = 1;
 splayer.P_col = 1;
 PassState = 2;
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 5 + i;
 sbox[i].box_y = 5 + i;
 sdestinate[i].Des_x = 3 + i;
 sdestinate[i].Des_y = 3;
 }
 }
 else if (3 == nSetState) {
 memcpy(Array, Array3, sizeof(Array));
 splayer.P_row = 1;
 splayer.P_col = 1;
 PassState = 3;
 memcpy(Array, Array3, sizeof(Array));
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 10;
 sbox[i].box_y = 7 + i;
 sdestinate[i].Des_x = 2 + i;
 sdestinate[i].Des_y = 2;
 }
 }
 bslect = true;
 break;
 }
 }
}
//判断玩家移动
void PlayerMove() {
 splayer.BackUp();
 if (KEY_DOWN(VK_UP)) {
 --splayer.P_row;
 }
 if (KEY_DOWN(VK_DOWN)) {
 ++splayer.P_row;
 }
 if (KEY_DOWN(VK_LEFT)) {
 --splayer.P_col;
 }
 if (KEY_DOWN(VK_RIGHT)) {
 ++splayer.P_col;
 }
 if (1 == Array[splayer.P_row][splayer.P_col]) {//判断玩家是否越墙
 splayer.restore();
 }
}
//判断箱子移动
void BoxMove() {
 for (int i = 0; i < 3; i++) {
 sbox[i].BackUp();
 }
 int skew_x = 0; //临时偏移量
 int skew_y = 0;
 for (int s = 0; s < 3; s++) { //利用坐标偏移量求箱子新位置
 if (splayer.P_row == sbox[s].box_x && splayer.P_col == sbox[s].box_y) {
 skew_x = sbox[s].box_x - splayer.P_rowBk;
 skew_y = sbox[s].box_y - splayer.P_colBk;
 sbox[s].box_x = sbox[s].box_x + skew_x;
 sbox[s].box_y = sbox[s].box_y + skew_y;
 /*sbox[s].restore();*/
 }
 }
 for (int a = 0; a < 3; a++) {
 for (int b = 0; b < 3; b++) {
 if (a != b && (sbox[a].box_x == sbox[b].box_x && sbox[a].box_y == sbox[b].box_y)) {
 splayer.restore();
 sbox[a].restore();
 }
 }
 }
 for (int s = 0; s < 3; s++) { //判断箱子是否越界
 if (1 == Array[sbox[s].box_x][sbox[s].box_y]) {
 splayer.restore();
 sbox[s].restore();
 }
 }
}
 
//判断胜利
void JudgeWin() {
 int nCorrectNum = 0; //判断箱子推到正确位置的数量
 for (int i = 0; i < 3; i++) {
 for (int j = 0; j < 3; j++) {
 if (sdestinate[i].Des_x == sbox[j].box_x && sdestinate[i].Des_y == sbox[j].box_y) {
 ++nCorrectNum;
 break;
 }
 }
 if (3 <= nCorrectNum) {
 //system("cls");
 cout << "You Win!" << endl;
 ++PassState;
 if (2 == PassState) {
 memcpy(Array, Array2, sizeof(Array));
 //更新玩家位置
 splayer.P_row = 1;
 splayer.P_col = 1;
 //更新箱子和坑状态
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 5 + i;
 sbox[i].box_y = 5 + i;
 sdestinate[i].Des_x = 3 + i;
 sdestinate[i].Des_y = 3;
 }
 }
 if (3 == PassState) {
 splayer.P_row = 1;
 splayer.P_col = 1;
 memcpy(Array, Array3, sizeof(Array));
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 10;
 sbox[i].box_y = 7 + i;
 sdestinate[i].Des_x = 2 + i;
 sdestinate[i].Des_y = 2;
 }
 }
 
 //system("pause");
 break;
 }
 /*system("pause");*/
 }
 
}
 
//打印游戏菜单
void Menu() {
 if (KEY_DOWN(VK_UP)) {
 //cout << "向上被按下" << endl;
 --nMenuState;
 if (nMenuState < 1) { nMenuState = 3; }
 }
 
 if (KEY_DOWN(VK_DOWN)) {
 //cout << "向下被按下" << endl;
 ++nMenuState;
 if (nMenuState > 3) { nMenuState = 1; }
 }
 
 switch (nMenuState) {
 case 1: system("cls");
 cout << "********游戏界面菜单********" << endl;
 cout << " -> 1、游戏开始" << endl;
 cout << " 2、游戏设置" << endl;
 cout << " 3、退出游戏" << endl << endl;
 cout << " ->按小键盘↑↓←→移动<- " << endl;
 cout << " ->按F1 F2 F3可切换关卡<- " << endl;
 system("pause");
 break;
 case 2: system("cls");
 cout << "********游戏界面菜单********" << endl;
 cout << " 1、游戏开始" << endl;
 cout << " -> 2、游戏设置" << endl;
 cout << " 3、退出游戏" << endl << endl;
 cout << " ->按小键盘↑↓←→移动<- " << endl;
 cout << " ->按F1 F2 F3可切换关卡<- " << endl;
 system("pause");
 break;
 case 3: system("cls");
 cout << "********游戏界面菜单********" << endl;
 cout << " 1、游戏开始" << endl;
 cout << " 2、游戏设置" << endl;
 cout << " -> 3、退出游戏" << endl << endl;
 cout << " ->按小键盘↑↓←→移动<- " << endl;
 cout << " ->按F1 F2 F3可切换关卡<- " << endl;
 system("pause");
 break;
 }
}
 
void Map() {
 while (1) {
 system("cls");
 PlayerMove();
 BoxMove();
 JudgeWin();
 if (4 == PassState) {
 cout << "恭喜通关" << endl;
 PassState = 1;
 memcpy(Array, Array1, sizeof(Array));
 //更新玩家位置
 splayer.P_row = 1;
 splayer.P_col = 1;
 //更新箱子和坑状态
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 5 + i;
 sbox[i].box_y = 5 + i;
 sdestinate[i].Des_x = 8 + i;
 sdestinate[i].Des_y = 8 + i;
 }
 system("pause");
 break;
 }
 if (KEY_DOWN(VK_F1)) {
 memcpy(Array, Array1, sizeof(Array));
 PassState = 1;
 //更新玩家位置
 splayer.P_row = 1;
 splayer.P_col = 1;
 //更新箱子和坑状态
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 5 + i;
 sbox[i].box_y = 5 + i;
 }
 }
 if (KEY_DOWN(VK_F2)) {
 memcpy(Array, Array2, sizeof(Array));
 PassState = 2;
 //更新玩家位置
 splayer.P_row = 1;
 splayer.P_col = 1;
 //更新箱子和坑状态
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 3 + i;
 sbox[i].box_y = 5;
 sdestinate[i].Des_x = 3 + i;
 sdestinate[i].Des_y = 3;
 }
 }
 if (KEY_DOWN(VK_F3)) {
 memcpy(Array, Array3, sizeof(Array));
 PassState = 3;
 //更新玩家位置
 splayer.P_row = 1;
 splayer.P_col = 1;
 //更新箱子和坑状态
 for (int i = 0; i < 3; i++) {
 sbox[i].box_x = 10;
 sbox[i].box_y = 7 + i;
 sdestinate[i].Des_x = 2 + i;
 sdestinate[i].Des_y = 2;
 }
 }
 //打印地图
 for (int i = 0; i < 15; i++) {
 for (int j = 0; j < 20; j++) {
 bool bDrawBox = false; //判断箱子
 for (int m = 0; m < 3; m++) {
 if (i == sbox[m].box_x && j == sbox[m].box_y) {
 bDrawBox = true;
 break;
 }
 }
 bool bDrawDes = false; //判断坑
 for (int m = 0; m < 3; m++) {
 if (i == sdestinate[m].Des_x && j == sdestinate[m].Des_y) {
 bDrawDes = true;
 break;
 }
 }
 if (1 == Array[i][j]) {
 cout << "■";
 }
 else if (i == splayer.P_row && j == splayer.P_col) {
 cout << "♀";
 }
 else if (true == bDrawBox) { //将true放在前面,不然出现全是箱子都不知道怎么错的……
 cout << "箱";
 }
 else if (true == bDrawDes) {
 cout << "坑";
 }
 else {
 cout << " ";
 }
 }
 cout << endl;
 }
 system("pause");
 }
}
 
 
 
int _tmain(int argc, _TCHAR* argv[])
{
 memcpy(Array, Array1, sizeof(Array));
 while (1) {
 //if (KEY_DOWN(VK_RETURN)){
 // if (1 == nMenuState) {
 // Map();
 // }
 // else if (2 == nMenuState) {
 // SetChoise();
 // /* cout << "wwww";
 // system("pause");*/
 // }
 // else if (3 == nMenuState)
 // return 0;
 //}
 if (3 == nMenuState && KEY_DOWN(VK_RETURN)) { //可以
 return 0;
 }
 else if (2 == nMenuState && KEY_DOWN(VK_RETURN)) {
 SetChoise();
 if (bslect) {
 Map();
 }
 }
 else if (1 == nMenuState && KEY_DOWN(VK_RETURN)) {
 Map();
 }
 //if (KEY_DOWN(VK_RETURN) && 3 == nMenuState) { //KEY_DOWN(VK_RETURN)不能进行与或运算 ??
 // return 0;
 //}
 //else if (KEY_DOWN(VK_RETURN) && 1 == nMenuState) {
 // Map();
 //}
 else {
 Menu();
 }
 }
 //Map();
 system("pause");
 return 0;
}

代码效果:

C++实现推箱子小游戏源码

C++实现推箱子小游戏源码

C++实现推箱子小游戏源码

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

原文链接:https://blog.csdn.net/weixin_43689161/article/details/94758226

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部