本文实例为大家分享了C++实现彩色飞机大战的具体代码,供大家参考,具体内容如下
1.基本的能够实现键盘按上下左右进行控制飞机,击杀敌人飞机,记录得分,(缺点:死亡后不能从新玩,需要重新启动程序,),缺点将在2中解决
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
|
/*隐藏光标的代码
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
getchar();
}*/
/*
明白两个事实,
敌人飞机和自己的飞机的横坐标和纵坐标相同时 表示死亡
敌人飞机和自己的子弹碰撞即 子弹坐标和自己子弹的坐标相同时,飞机死亡并且加分,
*/
#include "stdio.h"
#include <windows.h>
#include <conio.h>
#include <time.h>
#define Esc 27 //退出
#define Up 72 //上,下,左,右
#define Down 80
#define Left 75
#define Right 77
#define Kong 32 //发射子弹
int x = 10; //飞机坐标
int y = 18;
int d2 = 10; //敌机坐标
int d1 = 10;
int d = 10; //d 和r 用来进行碰撞检测
int r = 1;
int r1 = 1;
int r2 = 1;
int t = 1; // 游戏结束
int f = 0; // 计分数
int m = 5; // 敌机数
int j = 0; // 歼敌数
char p; // 接受按键
void kongzhi( int bx, int by); //声明函数
void huatu();
void gotoxy( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);
}
void gotoxy_red( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);
}
void gotoxy_blue( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1);
}
void gotoxy_green( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2);
}
void hidden() //隐藏光标,不让光标显示
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0; //赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut, &cci);
}
//**************************************************************************************
//说明
void shuoming()
{
printf ( "\t\t\t\n\n\n\n" );
gotoxy_blue(0, 0);
printf ( "\t\t\t\t\t\t\tPlane Control\n\n"
"\t\t\t\t\t\t\t\tUP\n\n"
"\t\t\t\t\t\t\tDown\n\n"
"\t\t\t\t\t\t\tLeft \n\n"
"\t\t\t\t\t\t\tRight \n\n"
"\t\t\t\t\t\t\t bullet space\n\n\n"
"\t\t\t\t\t\t\tQuit Esc\n" );
gotoxy_red(0, 0);
}
//****************************************************************************************
//判断我机死没死/游戏结束
void byebye()
{
if (((x == d && y == r) || (x == d1 && y == r1) || (x == d2 && y == r2))||( (d>=19||r>=19)||(d1>=19||r1>=19)||(d1>=19||r1>=19) ))
{
gotoxy(1, 3);
printf ( " !!! 游戏结束 !!!\n"
"*******************\n"
" 您的总得分: %d\n\n"
" 敌机数: %d\n"
" 歼敌数: %d\n"
" 命中率: %.0f %%\n"
"*******************\n" , f, m, j, (( float )j / ( float )m) * 100);
while (!_kbhit())
{
Sleep(500);
gotoxy(1, 12);
printf ( " 继续请按任意键...\n\n\n" );
Sleep(900);
gotoxy(1, 12);
printf ( " " );
}
gotoxy_red(0, 0);
huatu();
f = 0; m = 0; j = 0;
if (x >= 18) x--;
else x++;
gotoxy(x, y);
printf ( "A" );
}
}
// 计分/更新敌机
void jifan()
{ //x,y是子弹的坐标
if (x == d && y == r) // d=10, r=1, d,d1,d2 是敌机的x轴, 为10 ,r为敌机的纵坐标
{
gotoxy(d, r); printf ( "3" );
Sleep(200);
gotoxy(d, r); printf ( " " ); f += 2; r = 0; j++; //让r=0,即是让敌人的飞机消失
}
if (x == d1 && y == r1)
{
gotoxy(d1, r1); printf ( "1" );
Sleep(200);
gotoxy(d1, r1); printf ( " " ); f += 3; r1 = 0; j++;
}
if (x == d2 && y == r2)
{
gotoxy(d2, r2); printf ( "0" );
Sleep(200);
gotoxy(d2, r2); printf ( " " ); f += 1; r2 = 0; j++;
}
gotoxy(57, 2);
printf ( "%d\n" , f);
}
//画图
void huatu()
{
int i, n;
for (i = 0; i <= 20; i++)
{
for (n = 0; n <= 20; n++)
{
printf ( "*" );
}
printf ( "\n" );
}
for (i = 1; i <= 19; i++)
{
for (n = 1; n <= 19; n++)
{
gotoxy_red(i, n);
printf ( " " );
}
}
}
//随机产生敌机
void dfeiji()
{
while (t)
{
if (!r) { d = rand () % 17 + 1; m++; } //r,r1,r2 初始值都为1,当变为0的时候开始产生随机数
if (!r1) { d1 = rand () % 17 + 1; m++; }
if (!r2) { d2 = rand () % 17 + 1; m++; }
while (t)
{
r=r+2; r1=r1+2; r2=r2+2;
gotoxy(d, r); printf ( "b" ); //d,d1, d2 为敌机产生的位置,都为10
gotoxy(d1, r1); printf ( "c" );
gotoxy(d2, r2); printf ( "d" );
Sleep(900);
gotoxy(d, r); printf ( " " );
gotoxy(d1, r1); printf ( " " );
gotoxy(d2, r2); printf ( " " );
kongzhi(0, 0); //控制飞机后,要立即进行判断
byebye(); //判断飞机有没有死亡
if (r == 19) r = 0;
if (r1 == 19) r1 = 0;
if (r2 == 19) r2 = 0;
if (r == 0 || r1 == 0 || r2 == 0) break ;
}
}
}
//操控飞机
void kongzhi( int bx, int by) //调用的时候传入了 0, 0
{
int a;
while (_kbhit())
{
if ((p = _getch()) == -32) p = _getch();
a = p;
gotoxy(22, 5);
switch (a)
{ //控制方向
case Up: if (y != 1)
{
gotoxy(x, y); printf ( " " );
y--;
gotoxy(x, y); printf ( "A" );
} break ;
case Down: if (y != 18)
{
gotoxy(x, y); printf ( " " );
y++;
gotoxy(x, y); printf ( "A" );
} break ;
case Left: if (x != 1)
{
gotoxy(x, y); printf ( " " );
x--;
gotoxy(x, y); printf ( "A" );
} break ;
case Right: if (x != 18)
{
gotoxy(x, y); printf ( " " );
x++;
gotoxy(x, y); printf ( "A" );
} break ;
case Kong: { bx = y; //先把y的值存起来,存到bx
for (by = y; by > 1;) //发射子弹, y轴坐标一直减减,打印 |
{
by--; //y的坐标
gotoxy(x, by); printf ( "|" );
Sleep(10);
gotoxy(x, by); printf ( " " );
y = by; //记录子弹打到哪了,好进行碰撞检测
jifan(); //计分数
if (r == 0 || r1 == 0 || r2 == 0) break ;
}
y = bx; //恢复y的值
} break ;
case Esc:t = 0; break ; //退出
default : break ;
}
}
}
int main()
{
srand ( time (NULL));
shuoming(); //打印游戏说明,之后让光标进入0,0
hidden(); //隐藏光标,不让光标显示
huatu(); //画出墙壁
gotoxy(x, y); //x=10,y=8, x 和y 是自己飞机的坐标,是全局变量
printf ( "A" );
gotoxy(50, 2);
printf ( "Score:" );
while (t) //t是一个全局变量 初始值为1
{
kongzhi(0, 0); //调用控制飞机函数, (操作飞机后并记分数)
if (t) //如果游戏没有结束,则 产生敌机
dfeiji(); //产生敌机 ,并判断飞机有没有死亡
}
}
|
2.(封装了一个函数)结束游戏后能够重新开始进行下一局
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
|
/*隐藏光标的代码
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
getchar();
}*/
/*
明白两个事实,
敌人飞机和自己的飞机的横坐标和纵坐标相同时 表示死亡
敌人飞机和自己的子弹碰撞即 子弹坐标和自己子弹的坐标相同时,飞机死亡并且加分,
*/
#include "stdio.h"
#include <windows.h>
#include <conio.h>
#include <time.h>
#define Esc 27 //退出
#define Up 72 //上,下,左,右
#define Down 80
#define Left 75
#define Right 77
#define Kong 32 //发射子弹
int x = 10; //飞机坐标
int y = 18;
int d2 = 10; //敌机坐标
int d1 = 10;
int d = 10; //d 和r 用来进行碰撞检测
int r = 1;
int r1 = 1;
int r2 = 1;
int t = 1; // 游戏结束
int f = 0; // 计分数
int m = 5; // 敌机数
int j = 0; // 歼敌数
char p; // 接受按键
void kongzhi( int bx, int by); //声明函数
void huatu();
void gotoxy( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);
}
void gotoxy_red( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);
}
void gotoxy_blue( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1);
}
void gotoxy_green( int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2);
}
void hidden() //隐藏光标,不让光标显示
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0; //赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut, &cci);
}
//**************************************************************************************
//说明
void shuoming()
{
printf ( "\t\t\t\n\n\n\n" );
gotoxy_blue(0, 0);
printf ( "\t\t\t\t\t\t\tPlane Control\n\n"
"\t\t\t\t\t\t\t\tUP\n\n"
"\t\t\t\t\t\t\tDown\n\n"
"\t\t\t\t\t\t\tLeft \n\n"
"\t\t\t\t\t\t\tRight \n\n"
"\t\t\t\t\t\t\t bullet space\n\n\n"
"\t\t\t\t\t\t\tQuit Esc\n" );
gotoxy_red(0, 0);
}
//****************************************************************************************
//判断我机死没死/游戏结束
void byebye()
{
if (((x == d && y == r) || (x == d1 && y == r1) || (x == d2 && y == r2))||( (d>=19||r>=19)||(d1>=19||r1>=19)||(d1>=19||r1>=19) ))
{
gotoxy(1, 3);
printf ( " !!! game over !!!\n"
"*******************\n"
" score: %d\n\n"
" di ji shu: %d\n"
" jian di shu: %d\n"
" ming zhong lv: %.0f %%\n"
"*******************\n" , f, m, j, (( float )j / ( float )m) * 100);
t=0;
}
}
// 计分/更新敌机
void jifan()
{ //x,y是子弹的坐标
if (x == d && y == r) // d=10, r=1, d,d1,d2 是敌机的x轴, 为10 ,r为敌机的纵坐标
{
gotoxy(d, r); printf ( "3" );
Sleep(200);
gotoxy(d, r); printf ( " " ); f += 2; r = 0; j++; //让r=0,即是让敌人的飞机消失
}
if (x == d1 && y == r1)
{
gotoxy(d1, r1); printf ( "1" );
Sleep(200);
gotoxy(d1, r1); printf ( " " ); f += 3; r1 = 0; j++;
}
if (x == d2 && y == r2)
{
gotoxy(d2, r2); printf ( "0" );
Sleep(200);
gotoxy(d2, r2); printf ( " " ); f += 1; r2 = 0; j++;
}
gotoxy(57, 2);
printf ( "%d\n" , f);
}
//画图
void huatu()
{
int i, n;
for (i = 0; i <= 20; i++)
{
for (n = 0; n <= 20; n++)
{
printf ( "*" );
}
printf ( "\n" );
}
for (i = 1; i <= 19; i++)
{
for (n = 1; n <= 19; n++)
{
gotoxy_red(i, n);
printf ( " " );
}
}
}
//随机产生敌机
void dfeiji()
{
while (t)
{
if (!r) { d = rand () % 17 + 1; m++; } //r,r1,r2 初始值都为1,当变为0的时候开始产生随机数
if (!r1) { d1 = rand () % 17 + 1; m++; }
if (!r2) { d2 = rand () % 17 + 1; m++; }
while (t)
{
r=r+2; r1=r1+2; r2=r2+2;
gotoxy(d, r); printf ( "b" ); //d,d1, d2 为敌机产生的位置,都为10
gotoxy(d1, r1); printf ( "c" );
gotoxy(d2, r2); printf ( "d" );
Sleep(900);
gotoxy(d, r); printf ( " " );
gotoxy(d1, r1); printf ( " " );
gotoxy(d2, r2); printf ( " " );
kongzhi(0, 0); //控制飞机后,要立即进行判断
byebye(); //判断飞机有没有死亡
if (r == 19) r = 0;
if (r1 == 19) r1 = 0;
if (r2 == 19) r2 = 0;
if (r == 0 || r1 == 0 || r2 == 0) break ;
}
}
}
//操控飞机
void kongzhi( int bx, int by) //调用的时候传入了 0, 0
{
int a;
while (_kbhit())
{
if ((p = _getch()) == -32) p = _getch();
a = p;
gotoxy(22, 5);
switch (a)
{ //控制方向
case Up: if (y != 1)
{
gotoxy(x, y); printf ( " " );
y--;
gotoxy(x, y); printf ( "A" );
} break ;
case Down: if (y != 18)
{
gotoxy(x, y); printf ( " " );
y++;
gotoxy(x, y); printf ( "A" );
} break ;
case Left: if (x != 1)
{
gotoxy(x, y); printf ( " " );
x--;
gotoxy(x, y); printf ( "A" );
} break ;
case Right: if (x != 18)
{
gotoxy(x, y); printf ( " " );
x++;
gotoxy(x, y); printf ( "A" );
} break ;
case Kong: { bx = y; //先把y的值存起来,存到bx
for (by = y; by > 1;) //发射子弹, y轴坐标一直减减,打印 |
{
by--; //y的坐标
gotoxy(x, by); printf ( "|" );
Sleep(10);
gotoxy(x, by); printf ( " " );
y = by; //记录子弹打到哪了,好进行碰撞检测
jifan(); //计分数
if (r == 0 || r1 == 0 || r2 == 0) break ;
}
y = bx; //恢复y的值
} break ;
case Esc:t = 0; break ; //退出
default : break ;
}
}
}
void zuzhong(){
x = 10; //飞机坐标
y = 18;
d2 = 10; //敌机坐标
d1 = 10;
d = 10; //d 和r 用来进行碰撞检测
r = 1;
r1 = 1;
r2 = 1;
t = 1; // 游戏结束
f = 0; // 计分数
m = 5; // 敌机数
j = 0; // 歼敌数
char p; // 接受按键
srand ( time (NULL));
shuoming(); //打印游戏说明,之后让光标进入0,0
hidden(); //隐藏光标,不让光标显示
huatu(); //画出墙壁
gotoxy(x, y); //x=10,y=8, x 和y 是自己飞机的坐标,是全局变量
printf ( "A" );
gotoxy(50, 2);
printf ( "Score:" );
while (t) //t是一个全局变量 初始值为1
{
kongzhi(0, 0); //调用控制飞机函数, (操作飞机后并记分数)
if (t) //如果游戏没有结束,则 产生敌机
dfeiji(); //产生敌机 ,并判断飞机有没有死亡
}
}
int main()
{
while (1){
system ( "cls" );
zuzhong();
printf ( "please enter Enter key contine" );
getchar ();
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_43225966/article/details/109344543