本文实例为大家分享了C/C++实现推箱子小游戏的具体代码,供大家参考,具体内容如下
效果演示
实现功能
如上图所示。按键控制小猪的运动,推箱子到达目的地。
如何实现
1.首先思考要保存箱子,小猪等信息,添加多个map可以用到三维数组。
2.定义小猪,箱子,墙,空地等信息在三维数组里的数值。
空地 0
墙 1
目的地 2
箱子 3
猪 4 2+4=6 猪到达目的地也显示猪
箱子到达目的地 2+3=5
3.按键实现推箱子运动。
考虑多种情况:
①.小猪的运动(空地,目的在)
②.小猪推箱子运动(空地,目的地)
用三维数组保存map信息:这里我只做了两种图,可自行添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
int backgrand[MAP][ROW][COL] = //map图
{
{
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 2, 0, 0, 1,
1, 1, 0, 0, 3, 1, 1, 1,
1, 2, 3, 0, 4, 0, 0, 1,
1, 0, 1, 0, 3, 3, 2, 1,
1, 1, 0, 3, 2, 0, 0, 1,
1, 0, 0, 2, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1
},
{
0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 2, 0, 0, 0, 1,
1, 1, 0, 0, 3, 0, 1, 1,
1, 2, 3, 0, 4, 0, 0, 1,
1, 0, 1, 0, 0, 0, 1, 1,
1, 1, 0, 3, 2, 3, 0, 1,
1, 0, 0, 0, 0, 2, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
},
};
|
绘图用到了easyx库,加载图片和绘制窗口只用到了三条语句:很简单
1
2
3
|
initgraph(); //绘制窗口大小
loadimage(); //加载图片
putinmage(); //放出图片
|
具体可看代码!
代码块
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
|
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<graphics.h>
#define ROW 8
#define COL 8
#define MAP 2//map个可以自己添加数
int count = 0; //哪个map图
/*
空地 0
墙 1
目的地 2
箱子 3
猪 4 2+4=6 猪到达目的地也显示猪
箱子到达目的地 2+3=5
*/
IMAGE image1, image2, image3, image4, image5, image6;
int backgrand[MAP][ROW][COL] = //map图
{
{
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 2, 0, 0, 1,
1, 1, 0, 0, 3, 1, 1, 1,
1, 2, 3, 0, 4, 0, 0, 1,
1, 0, 1, 0, 3, 3, 2, 1,
1, 1, 0, 3, 2, 0, 0, 1,
1, 0, 0, 2, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1
},
{
0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 2, 0, 0, 0, 1,
1, 1, 0, 0, 3, 0, 1, 1,
1, 2, 3, 0, 4, 0, 0, 1,
1, 0, 1, 0, 0, 0, 1, 1,
1, 1, 0, 3, 2, 3, 0, 1,
1, 0, 0, 0, 0, 2, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
},
};
int Judge(){ //p判断是否结束一局
int i = 0;
for (i = 0; i < ROW; i++){
int j = 0;
for (j = 0; j < COL; j++){
if (backgrand[count][i][j] == 3 || backgrand[count][i][j] == 2){ //判断是否还有箱子或者目的地
return 0;
}
}
}
return 1;
}
void Backgrand(){ //绘制地图
int i = 0;
for (; i < ROW; i++){
int j = 0;
for (; j < COL; j++){
int x = 80 * j; //横轴是y(列),数轴是x(行)。
int y = 80 * i;
switch (backgrand[count][i][j]){
case 0:
putimage(x, y, &image3);
break ;
case 1:
putimage(x, y, &image1);
break ;
case 2:
putimage(x, y, &image6);
break ;
case 3:
putimage(x, y, &image4);
break ;
case 4:
case 6:
putimage(x, y, &image2);
break ;
case 5:
putimage(x, y, &image5);
break ;
default :
break ;
}
}
}
}
void Keymove(){ //按键控制猪的运动
int i = 0;
int j = 0; //j定义出来了,里面for循环要初始化
for (i = 0; i < ROW; i++){ //找到猪的坐标
for (j = 0; j < COL; j++){
if (backgrand[count][i][j] == 4 || backgrand[count][i][j] == 6){
break ;
}
}
if (backgrand[count][i][j] == 4 || backgrand[count][i][j] == 6){
break ;
}
}
char key = _getch(); //获得按键
switch (key){
case 'w' :
case 'W' :
case 72:
if (backgrand[count][i - 1][j] == 0 ||\
backgrand[count][i - 1][j] == 2)
{
backgrand[count][i][j] = backgrand[count][i][j] - 4; //猪运动,猪旁边是空地或者是目的地
backgrand[count][i - 1][j] = backgrand[count][i - 1][j] + 4;
}
if (backgrand[count][i - 1][j] == 3 && (backgrand[count][i - 2][j] == 0 || \
backgrand[count][i - 2][j] == 2)) //猪推箱子运动条件,猪旁边是箱子与上箱子旁边是空地。
{ //后面或的条件要括号括起来
backgrand[count][i][j] = backgrand[count][i][j] - 4;
backgrand[count][i - 1][j] = backgrand[count][i - 1][j] + 4;
backgrand[count][i - 1][j] -= 3;
backgrand[count][i - 2][j] += 3;
}
if (backgrand[count][i - 1][j] == 5 &&\
backgrand[count][i - 2][j] == 0) //箱子到达目的地,后面还有空的情况
{
backgrand[count][i][j] -= 4;
backgrand[count][i - 1][j] += 4;
backgrand[count][i - 1][j] -= 3;
backgrand[count][i - 2][j] += 3;
}
break ;
case 's' :
case 'S' :
case 80:
if (backgrand[count][i + 1][j] == 0 || \
backgrand[count][i + 1][j] == 2)
{ //猪运动
backgrand[count][i][j] = backgrand[count][i][j] - 4;
backgrand[count][i + 1][j] = backgrand[count][i + 1][j] + 4;
}
if (backgrand[count][i + 1][j] == 3 && (backgrand[count][i + 2][j] == 0 ||\
backgrand[count][i + 2][j] == 2))
{
backgrand[count][i][j] = backgrand[count][i][j] - 4;
backgrand[count][i + 1][j] = backgrand[count][i + 1][j] + 4;
backgrand[count][i + 1][j] -= 3;
backgrand[count][i + 2][j] += 3;
}
if (backgrand[count][i + 1][j] == 5 && \
backgrand[count][i + 2][j] == 0)
{
backgrand[count][i][j] -= 4;
backgrand[count][i + 1][j] += 4;
backgrand[count][i + 1][j] -= 3;
backgrand[count][i + 2][j] += 3;
}
break ;
case 'a' :
case 'A' :
case 75:
if (backgrand[count][i][j - 1] == 0 || \
backgrand[count][i][j - 1] == 2)
{ //猪运动
backgrand[count][i][j] = backgrand[count][i][j] - 4;
backgrand[count][i][j - 1] = backgrand[count][i][j - 1] + 4;
}
if (backgrand[count][i][j - 1] == 3 &&\
(backgrand[count][i][j - 2] == 0 ||\
backgrand[count][i][j - 2] == 2))
{
backgrand[count][i][j] = backgrand[count][i][j] - 4;
backgrand[count][i][j - 1] = backgrand[count][i][j - 1] + 4;
backgrand[count][i][j - 1] -= 3;
backgrand[count][i][j - 2] += 3;
}
if (backgrand[count][i][j - 1] == 5 && \
backgrand[count][i][j - 2] == 0)
{
backgrand[count][i][j] -= 4;
backgrand[count][i][j - 1] += 4;
backgrand[count][i][j - 1] -= 3;
backgrand[count][i][j - 2] += 3;
}
break ;
case 'd' :
case 'D' :
case 77:
if (backgrand[count][i][j + 1] == 0 || \
backgrand[count][i][j + 1] == 2)
{ //猪运动
backgrand[count][i][j] = backgrand[count][i][j] - 4;
backgrand[count][i][j + 1] = backgrand[count][i][j + 1] + 4;
}
if (backgrand[count][i][j + 1] == 3 && (backgrand[count][i][j + 2] == 0 ||\
backgrand[count][i][j + 2] == 2))
{
backgrand[count][i][j] -= 4;
backgrand[count][i][j + 1] += 4;
backgrand[count][i][j + 1] -= 3;
backgrand[count][i][j + 2] += 3;
}
if (backgrand[count][i][j + 1] == 5 && backgrand[count][i][j + 2] == 0){
backgrand[count][i][j] -= 4;
backgrand[count][i][j + 1] += 4;
backgrand[count][i][j + 1] -= 3;
backgrand[count][i][j + 2] += 3;
}
break ;
default :
break ;
}
}
void Initgraph(){
initgraph(80 * ROW, 80 * COL);
loadimage(&image1, "1.jpg" ); //墙
loadimage(&image2, "2.jpg" ); //猪
loadimage(&image3, "3.jpg" ); //空地
loadimage(&image4, "4.jpg" ); //箱子
loadimage(&image6, "6.jpg" ); //目的地
loadimage(&image5, "5.jpg" ); //箱子到目的地
}
int main(){
Initgraph();
while (1){
Backgrand();
Keymove();
if (Judge()){
Backgrand();
Sleep(500);
count++;
}
if (count == MAP){
break ;
}
}
getchar ();
closegraph();
system ( "pause" );
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_57023347/article/details/117435142