推箱子

2007年10月

初次尝试Windows控制台程序,花了两个晚上写的。

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
/*
推箱子
作者:MasterRay
时间:2007-10-18
请用Visual C++编译
*/
#include <windows.h>
#include <conio.h>

#define ENTER 13
#define ESC 27
#define SPACE 32
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define ARROW 224

HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出句柄
COORD TopLeft={0, 0}, MainPos={0, 5};
FILE *f;
char map[720], FileName[256]={"box\\"};
int x, y, //推箱者的坐标
DirectionX[20], DirectionY[20], //目标的坐标
maxx, maxy, level=1;

char *DecToStr(char *str, int dec)
{
char ch, *left=str, *right=str, *p;
do
{
*right++=dec%10|'0';
}while (dec/=10);
p=right--;
while (left<right)
{
ch=*left;
*left++=*right;
*right--=ch;
}
*p++='.';
*p++='t';
*p++='x';
*p++='t';
*p='\0';
return str;
}

//清除地图
void ClearMap()
{
FillConsoleOutputCharacter(hOut, ' ', 1600, MainPos, 0);
}

//画地图
void DrawMap()
{
COORD pos={0, 7};
short i=0, j, p=0;
char ch;
ClearMap();
SetConsoleCursorPosition(hOut, pos);
for (; i<maxy; i++)
{
for (j=0; j<maxx; j++)
{
switch (map[i*maxx+j])
{
case 'B': //箱子
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE|FOREGROUND_INTENSITY); //蓝色
WriteConsoleA(hOut, "■", 2, 0, 0);
break;
case 'D': //目标
DirectionX[p]=j;
DirectionY[p++]=i;
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); //黄色
WriteConsoleA(hOut, "◆", 2, 0, 0);
break;
case 'F': //目标+箱子
map[i*maxx+j]='B';
DirectionX[p]=j;
DirectionY[p++]=i;
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE|FOREGROUND_INTENSITY); //蓝色
WriteConsoleA(hOut, "■", 2, 0, 0);
break;
case 'P': //推箱者
x=j;
y=i;
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_INTENSITY); //红色
WriteConsoleA(hOut, "★", 2, 0, 0);
break;
case 'W': //墙
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); //白色
WriteConsoleA(hOut, "■", 2, 0, 0);
break;
default:
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); //白色
WriteConsoleA(hOut, " ", 2, 0, 0);
}
}
pos.Y++;
SetConsoleCursorPosition(hOut, pos);
}
DirectionX[p]=DirectionY[p]=0;
SetConsoleCursorPosition(hOut, MainPos);
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); //白色
}

//新地图
void NewMap()
{
int ch;
char *p=map, flag=1;
DecToStr(FileName+4, level);
if ((f=fopen(FileName, "r"))==0) TerminateProcess(GetCurrentProcess(), 0);
maxy=1;
while ((ch=fgetc(f))!=EOF)
{
if (ch!='\r' && ch!='\n') *p++=ch;
else {maxx=p-map; break;}
}
while ((ch=fgetc(f))!=EOF)
{
if (ch!='\r' && ch!='\n')
{
if (flag)
{
maxy++;
flag=0;
}
*p++=ch;
}
else if (ch=='\n') flag=1;
}
DrawMap();
}

//胜利
void NextMap()
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); //黄色
SetConsoleCursorPosition(hOut, MainPos);
WriteConsoleA(hOut, "你赢了!\n", 8, 0, 0);
level++;
getch();
NewMap();
}

//判断是否胜利
void JudgeMap()
{
char win=1;
short p=0;
COORD pos;
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); //黄色
while (DirectionX[p])
{
if (map[DirectionY[p]*maxx+DirectionX[p]]!='B')
{
win=0;
if (DirectionX[p]!=x || DirectionY[p]!=y)
{
pos.X=DirectionX[p]<<1;
pos.Y=DirectionY[p]+7;
SetConsoleCursorPosition(hOut, pos);
WriteConsoleA(hOut, "◆", 2, 0, 0);
}
}
p++;
}
SetConsoleCursorPosition(hOut, MainPos);
if (win) NextMap();
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
}

//选择地图
void SelectMap()
{
int i, n=3;
char szLevel[1];
SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); //黄色
SetConsoleCursorPosition(hOut, MainPos);
WriteConsoleA(hOut, "关卡 \n", 6, 0, 0);
level=0;
while ((i=getch())!='\r')
{
if (i>='0' && i<='9')
{
level*=10;
level+=i&15;
szLevel[0]=i;
WriteConsoleA(hOut, szLevel, 1, 0, 0);
if (n--==0) break;
}
}
NewMap();
}

//开始游戏
void StartGame()
{
SetConsoleTextAttribute(hOut, BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_INTENSITY);
SetConsoleCursorPosition(hOut, TopLeft);
WriteConsoleA(hOut, "推箱子      \n", 18, 0, 0);
SetConsoleTextAttribute(hOut, BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
WriteConsoleA(hOut, "ENTER——选关  \nSPACE——重新开始\nESC———退出   ", 53, 0, 0);
}

//控制推箱者移动
void move(short heading)
{
COORD pos={x<<1 ,y+7};
SMALL_RECT rc={x<<1, y+7, (x<<1)+1, y+7};
CHAR_INFO chFill;
chFill.Attributes=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
chFill.Char.AsciiChar=' ';
switch (heading)
{
case UP:
if (y>0)
{
if (map[(y-1)*maxx+x]!='B' && map[(y-1)*maxx+x]!='W')
{
y--;
pos.Y--;
}
else if (y>1 && map[(y-1)*maxx+x]=='B' && map[(y-2)*maxx+x]!='B' && map[(y-2)*maxx+x]!='W')
{
map[(y-1)*maxx+x]='P';
map[(y-2)*maxx+x]='B';
y--;
pos.Y-=2;
rc.Top--;
}
}
else return;
break;
case LEFT:
if (x>0)
{
if (map[y*maxx+x-1]!='B' && map[y*maxx+x-1]!='W')
{
x--;
pos.X-=2;
}
else if (x>1 && map[y*maxx+x-1]=='B' && map[y*maxx+x-2]!='B' && map[y*maxx+x-2]!='W')
{
map[y*maxx+x-1]='P';
map[y*maxx+x-2]='B';
x--;
pos.X-=4;
rc.Left-=2;
}
}
else return;
break;
case RIGHT:
if (x<maxx-1)
{
if (map[y*maxx+x+1]!='B' && map[y*maxx+x+1]!='W')
{
x++;
pos.X+=2;
}
else if (x<maxx-2 && map[y*maxx+x+1]=='B' && map[y*maxx+x+2]!='B' && map[y*maxx+x+2]!='W')
{
map[y*maxx+x+1]='P';
map[y*maxx+x+2]='B';
x++;
pos.X+=2;
rc.Right+=2;
}
}
else return;
break;
case DOWN:
if (y<maxy-1)
{
if (y<maxy-1 && map[(y+1)*maxx+x]!='B' && map[(y+1)*maxx+x]!='W')
{
y++;
pos.Y++;
}
else if (y<maxy-2 && map[(y+1)*maxx+x]=='B' && map[(y+2)*maxx+x]!='B' && map[(y+2)*maxx+x]!='W')
{
map[(y+1)*maxx+x]='P';
map[(y+2)*maxx+x]='B';
y++;
pos.Y++;
rc.Bottom++;
}
}
else return;
break;
default:
return;
}
ScrollConsoleScreenBuffer(hOut, &rc, 0, pos, &chFill);
JudgeMap();
}

//获取按键
void GetKey()
{
switch (getch())
{
case ENTER: SelectMap(); break;
case ESC: CloseHandle(hOut); TerminateProcess(GetCurrentProcess(), 0);
case SPACE: NewMap(); break;
case ARROW: move(getch());
}
}

int main()
{
//设置窗口大小
COORD size={80, 25};
SetConsoleScreenBufferSize(hOut, size);

SetConsoleOutputCP(936); //代码页设置为简体中文
SetConsoleTitleA("推箱子——MasterRay"); //设置窗口标题

StartGame();
NewMap();
while (1)
GetKey();
return 0;
}