c++实现推箱子游戏
标签:send 输出 size 代码实现 osi 结果 函数 switch 点击
代码实现
1 #include 2 #include 3 #include 4 char* playerPosition, *boxPosition[2];
5 /*初始化地图
6 map:地图数组
7 n: 地图的行数
8 m: 地图的列数*/
9 void csh(char map[15][15], int n, int m)
10 {
11
12 for (int i = 0;i )
13 for (int j = 0;j )
14 map[i][j] = ‘ ‘;
15
16 }
17 /*控制角色向上移动
18 map:地图数组*/
19 void move(int moveX, int moveY)
20 {
21 int i = 0;
22
23 moveY = moveY * 15;
24 if (*(playerPosition + (moveX + moveY)) != ‘@‘)
25 {
26 if (*(playerPosition + (moveX + moveY)) == ‘*‘ )
27 {
28 if (*(playerPosition + 2* (moveX + moveY)) != ‘@‘)
29 {
30 if (playerPosition + (moveX + moveY) == boxPosition[0])
31 i = 0;
32 else
33 i = 1;
34 *boxPosition[i] = ‘ ‘;
35 boxPosition[i] += (moveX + moveY);
36 *boxPosition[i] = ‘*‘;
37 *playerPosition = ‘ ‘;
38 playerPosition += (moveX + moveY);
39 *playerPosition = ‘8‘;
40 }
41 }
42 else
43 {
44 *playerPosition = ‘ ‘;
45 playerPosition += (moveX+moveY);
46 *playerPosition = ‘8‘;
47 }
48 }
49
50 }
51 /*输出结果
52 map:地图数组
53 n: 地图的行数
54 m: 地图的列数*/
55 void sc(char map[15][15],int n,int m)
56 {
57 for (int i = 0;i )
58 {
59 for (int j = 0;j )
60 printf("%c ", map[i][j]);
61 printf("\n");
62 }
63 }
64 /*进行重开、跳关、移动的判断,进入相应的函数,并传参给相应函数
65 map:地图数组*/
66 char pd(char map[15][15])
67 {
68 char pd;
69
70 pd = _getch();
71 switch (pd)
72 {
73 case ‘w‘:move(0,-1);break;
74 case ‘s‘:move(0, 1);break;
75 case ‘a‘:move(-1, 0);break;
76 case ‘d‘:move(1, 0);break;
77 case 13:return 1;break;
78 case 32:return 2;
79 }
80 return 0;
81 }
82
83 /*第一关*/
84 void diyiguan()
85 {
86 int cnt = 0;
87 char map[15][15],cz;
88 boolean isEnd = false, isAgain = false, isNext = false;
89
90 csh(map, 8, 6);
91 //生成地图
92 map[0][1] = map[0][2] = map[0][0] = map[1][0] = map[2][0] = map[3][0] = map[4][0] = map[5][0] = map[6][0] = map[7][0] = map[7][1] = map[7][2] = map[7][3] = map[6][3] = map[5][3] = map[4][3] = map[4][4] = map[4][5] = map[2][3] = map[2][4] = map[2][5] = map[3][5] = map[1][2] = map[2][2] = ‘@‘;
93 map[6][2] = ‘O‘;
94 playerPosition = &map[3][1];
95 *playerPosition = ‘8‘;
96 boxPosition[0] = &map[4][2];
97 *boxPosition[0] = ‘*‘;
98
99
100 //开始游戏
101 sc(map, 8, 6);
102 printf("第一关");
103 while (!isEnd && !isAgain && !isNext)
104 {
105 cz = pd(map);
106 cnt++;
107 if (map[6][2] == ‘ ‘)
108 map[6][2] = ‘O‘;
109 if (cz == 1) isAgain = true;
110 if (cz == 2) isNext = true;
111 system("cls");
112 sc(map, 8, 6);
113 printf("已走 % d步\n第二关, 点击回车重开本关,点击空格跳过本关",cnt);
114 if (map[6][2] == ‘*‘)
115 isEnd = true;
116 }
117 system("cls");
118 if (cz == 1)
119 diyiguan();
120
121
122
123 }
124 /*第二关*/
125 void dierguan()
126 {
127 int cnt = 0;
128 char map[15][15], cz;
129 boolean isEnd = false, isAgain = false, isNext = false;
130
131 csh(map, 8, 6);
132 //生成地图
133 for (int i = 0;i 5; i++)
134 map[0][i] = map[7][i]=‘@‘;
135 for (int i = 0;i 7; i++)
136 map[i][0] = map[i][5] = ‘@‘;
137 map[4][4]=‘@‘;
138 map[6][2] = ‘O‘;
139 map[6][4] = ‘O‘;
140 playerPosition = &map[3][1];
141 *playerPosition = ‘8‘;
142 boxPosition[0] = &map[4][2];
143 *boxPosition[0] = ‘*‘;
144 boxPosition[1] = &map[3][3];
145 *boxPosition[1] = ‘*‘;
146
147
148 //开始游戏
149 sc(map, 8, 6);
150 printf("第二关");
151 while (!isEnd && !isAgain && !isNext)
152 {
153 cz = pd(map);
154 cnt++;
155 if (map[6][2] == ‘ ‘)map[6][2] = ‘O‘;
156 if (map[6][4] == ‘ ‘)map[6][4] = ‘O‘;
157 if (cz == 1) isAgain = true;
158 if (cz == 2) isNext = true;
159 system("cls");
160 sc(map, 8, 6);
161 printf("已走%d步\n第二关,点击回车重开本关,点击空格跳过本关",cnt);
162 if (map[6][2] == ‘*‘&&map[6][4]==‘*‘)
163 isEnd = true;
164 }
165 system("cls");
166 if (cz == 1)
167 dierguan();
168 }
169
170 void main()
171 {
172 diyiguan();
173 dierguan();
174 printf("\n恭喜你成功通关(这么简单的关卡真的有什么好恭喜的吗?)\n");
175 }
c++实现推箱子游戏
标签:send 输出 size 代码实现 osi 结果 函数 switch 点击
原文地址:https://www.cnblogs.com/hbsblog/p/12992362.html
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
c++实现推箱子游戏
文章链接:http://soscw.com/index.php/essay/39676.html
评论