使用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
|
#include <easyx.h>
#include <time.h>
#include <conio.h>
class Bullet;
class Tank;
class E_Bullet;
class Boss;
bool dead = false ;
bool wined = false ;
struct pos //坐标类
{
int a;
int b;
};
class E_Bullet //敌人打出的子弹
{
public :
clock_t d;
int x;
int y;
bool on = false ;
pos show() //画出新的位置
{
setfillcolor(RGB(255, 180, 20));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos del() //覆盖原来的位置
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos move() //左移
{
x -= 3;
return pos{ x,y };
}
};
class Bullet //玩家打出的子弹,同上
{
public :
clock_t d;
int x;
int y;
bool on = false ;
pos show()
{
setfillcolor(RGB(150, 180, 210));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos move() //右移
{
x += 3;
return pos{ x,y };
}
};
class Boss //敌人
{
public :
bool hurting = false ;
clock_t d_hurt;
COLORREF clr = RGB(0, 130, 125);
int x;
int y;
int hp = 100; //生命
clock_t d; //判断举例上一次执行某一函数过了多久
clock_t att_d;
bool angle = false ; //方向
pos show()
{
setfillcolor(clr);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
rectangle(x - 20, y - 40, x + 20, y + 40);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos{ x,y };
}
void fire(E_Bullet& but) //攻击
{
but.on = true ; //放置一个子弹
but.x = x - 20;
but.y = y;
but.d = clock ();
}
void move() //上上下下得移动
{
if (angle == true )
y -= 5;
if (angle == false )
y += 5;
if (y >= 440)
angle = true ;
if (y <= 40)
angle = false ;
}
void hurt() //受伤
{
hp -= 4;
d_hurt = clock ();
setfillcolor(0);
setlinecolor(WHITE);
fillrectangle(160, 485, 560, 510); //更新血条
rectangle(160, 485, 160 + hp * 4, 510);
setfillcolor(RGB(230, 0, 1));
setlinecolor(RGB(255, 255, 255));
fillrectangle(160, 485, 160 + hp * 4, 510);
rectangle(160, 485, 160 + hp * 4, 510);
hurting = true ;
if (hp <= 0) //死亡
{
wined = true ;
}
}
};
class Tank //玩家类,同上
{
public :
bool hurting = false ;
int hp = 100;
int x;
COLORREF clr = RGB(150, 180, 210);
int y;
clock_t d_hurt;
Tank() {}
Tank( int _x, int _y) { x = _x; y = _y; }
Tank operator=(pos p) { x = p.a; y = p.a; }
pos show()
{
setfillcolor(clr);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
setfillcolor(RGB(100, 200, 180));
fillrectangle(x, y + 5, x + 40, y - 5);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
rectangle(x - 25, y - 25, x + 25, y + 25);
fillrectangle(x, y + 5, x + 40, y - 5);
rectangle(x, y + 5, x + 40, y - 5);
return pos{ x,y };
}
void fire(Bullet& but)
{
but.on = true ;
but.x = x + 45;
but.y = y;
but.d = clock ();
but.show();
}
void hurt()
{
hp -= 2;
d_hurt = clock ();
setfillcolor(0);
setlinecolor(WHITE);
fillrectangle(160, 515, 560, 540);
rectangle(160, 515, 560, 540);
rectangle(160, 515, 160 + hp * 4, 540);
setfillcolor(RGB(0, 255, 1));
setlinecolor(RGB(255, 255, 255));
fillrectangle(160, 515, 160 + hp * 4, 540);
rectangle(160, 515, 160 + hp * 4, 540);
hurting = true ;
if (hp <= 0)
dead = true ;
}
};
#define BT_MAX 8
int main()
{
initgraph(640, 550, 4); //初始化屏幕
settextcolor(RGB(0, 254, 0));
settextstyle(35, 0, _T( "黑体" ));
outtextxy(150, 200, _T( "W,S移动,K攻击" ));
Sleep(3000);
setlinecolor(0);
setfillcolor(0);
rectangle(0, 0, 640, 550);
fillrectangle(0, 0, 640, 550);
setlinecolor(RGB(255, 255, 255));
setfillcolor(RGB(255, 255, 255));
clock_t delay = clock (); //玩家移动的延时
clock_t d_f = clock (); //玩家开火的延时
line(0, 481, 640, 481); //分割画面与血条
Bullet bt[BT_MAX]; //玩家的子弹
Tank tk(30, 30); //玩家
Boss bo; //敌人
bo.x = 580;
bo.y = 240;
E_Bullet ebt[BT_MAX]; //敌人的子弹
bo.d = clock (); //初始化延时
bo.att_d = clock ();
tk.show();
settextstyle(20, 0, _T( "黑体" ));
outtextxy(10, 485, _T( "BOSS的生命值:" ));
setfillcolor(RGB(230, 0, 1));
fillrectangle(160, 485, 560, 510); //敌人血条
outtextxy(10, 520, _T( "玩家的生命值:" ));
setfillcolor(RGB(0, 255, 1));
fillrectangle(160, 515, 560, 540); //玩家血条
while (1) //主循环
{
if (wined || dead) //玩家死了或者敌人死了
break ;
if (GetAsyncKeyState( 'W' ) & 0x8000) //玩家移动
{
if (tk.y > 28 && ( clock () - delay) >= 40)
{
tk.del(); tk.y -= 3; tk.show(); delay = clock ();
}
}
if (GetAsyncKeyState( 'w' ) & 0x8000) //玩家移动
{
if (tk.y > 28 && ( clock () - delay) >= 40)
{
tk.del(); tk.y -= 3; tk.show(); delay = clock ();
}
}
if (GetAsyncKeyState( 'k' ) & 0x8000) //玩家开火
{
for ( int i = 0; i < BT_MAX; i++)
{
if (bt[i].on == false && ( clock () - d_f) > 800)
{
bt[i].on = true ;
tk.fire(bt[i]);
d_f = clock ();
break ;
}
}
}
if (GetAsyncKeyState( 'K' ) & 0x8000) //玩家开火
{
for ( int i = 0; i < BT_MAX; i++)
{
if (bt[i].on == false && ( clock () - d_f) > 800)
{
tk.fire(bt[i]);
d_f = clock ();
break ;
}
}
}
if (GetAsyncKeyState( 'S' ) & 0x8000) //玩家移动
{
if (tk.y < 452 && ( clock () - delay) >= 40)
{
tk.del(); tk.y += 3; tk.show(); delay = clock ();
}
}
if (GetAsyncKeyState( 's' ) & 0x8000) //玩家移动
if (tk.y < 452 && ( clock () - delay) >= 40)
{
tk.del(); tk.y += 3; tk.show(); delay = clock ();
}
for ( int i = 0; i < BT_MAX; i++) //遍历子弹,使子弹刷新
{
if (bt[i].on == true && ( clock () - bt[i].d) > 20)
{
bt[i].del();
bt[i].move();
bt[i].show();
bt[i].d = clock ();
if (bt[i].x >= 635)
bt[i].on = false , bt[i].del(); //到达了屏幕最右端
if ((bt[i].x + 5 >= bo.x - 20 && bt[i].x - 5 <= bo.x + 20) && (bt[i].y - 5 < bo.y + 40 && bt[i].y + 5 > bo.y - 40))
//击中敌人
bt[i].on = false , bo.hurt(), bt[i].del();
}
}
if ( clock () - bo.att_d > 700) //敌人自动开火
{
for ( int i = 0; i < BT_MAX; i++)
{
if (ebt[i].on == false )
{
bo.fire(ebt[i]); break ;
}
}
bo.att_d = clock ();
}
for ( int i = 0; i < BT_MAX; i++) //敌人子弹刷新,同上
{
if (ebt[i].on == true && ( clock () - ebt[i].d > 20))
{
ebt[i].del();
ebt[i].move();
ebt[i].show();
ebt[i].d = clock ();
if (ebt[i].x < 5)
ebt[i].del(), ebt[i].on = false ;
if (ebt[i].x - 5 < tk.x + 25 && ebt[i].x + 5 > tk.x - 25 && ebt[i].y - 5 < tk.y + 25 && ebt[i].y + 5 > tk.y - 25)
{
ebt[i].on = false , tk.hurt(), ebt[i].del();
}
}
}
if (tk.hurting == true ) //玩家受伤闪烁0.1秒
if ( clock () - tk.d_hurt > 100)
{
tk.clr = RGB(150, 180, 210), tk.show(), tk.hurting = false ;
}
else
tk.clr = RGB(255, 0, 0), tk.show();
if (bo.hurting == true ) //敌人受伤闪烁0.1秒
if ( clock () - bo.d_hurt > 100)
{
bo.clr = RGB(0, 130, 125), bo.show(), bo.hurting = false ;
}
else
bo.clr = RGB(0, 255, 0), bo.show();
if ( clock () - bo.d > 50) //敌人移动延时;
bo.del(), bo.move(), bo.show(), bo.d = clock ();
}
if (wined) //胜负已分
{
settextcolor(RGB(0, 254, 0));
settextstyle(35, 0, _T( "黑体" ));
outtextxy(150, 200, _T( "你打败了boss!你赢了!!" ));
}
else
{
settextcolor(RGB(254, 0, 0));
settextstyle(35, 0, _T( "黑体" ));
outtextxy(140, 200, _T( "你被boss打败了!" ));
}
Sleep(5000);
closegraph();
return 0;
}
|
游戏截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/han_lu_/article/details/100994969