2021年春季版本

没啥新奇的主要就是一个队列结构,然后遍历输出在屏幕上,可以作为大作业?想要的话给我留个言拿走,或者你悄悄拿走别让我发现。

我中传hxd拿这玩意当c++作业交了,作业满分

我当年居然把这个类名叫做东方,都大学了才开始中二是吧byd

难度随时间增加,但不是线性的,15s,20s,25s有三个坎(好像是)

坚持30s胜利

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
#include <windows.h>
#include <iostream>
#include <ctime>
#include <list>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <string.h>
#include <math.h>

using namespace std;

struct Bullets
{
int x;
int y;
};

//光标移动到指定坐标处
void gotoxy(int x, int y)
{
HANDLE h;//句柄,对象的索引
COORD c;//结构体,坐标值
c.X = x;
c.Y = y;
h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, c);
}

//隐藏光标
void hide_cursor()
{
HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info;
GetConsoleCursorInfo(h_GAME, &cursor_info);
cursor_info.bVisible = false; //不显示光标
SetConsoleCursorInfo(h_GAME, &cursor_info);
}

//显示光标
void show_cursor()
{
HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info;
GetConsoleCursorInfo(h_GAME, &cursor_info);
cursor_info.bVisible = true; //显示光标
SetConsoleCursorInfo(h_GAME, &cursor_info);
}

//设置文本颜色
void color(int a)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}



class touhou
{
private:
clock_t player_move, bullets_above_1, bullets_left_1, bullets_right_1, t1, RAM, create_t;
int player_x, player_y, level, ram;
double n;
list<Bullets> above_1, left_1,right_1;
list<Bullets>::iterator p1, p2, p3;
public:
touhou()
{
level=400;
n = 0;
player_x = 36; player_y = 10;
player_move = clock();
RAM = clock();
bullets_above_1 = clock();
bullets_left_1 = clock();
t1 = clock();
hide_cursor();
}
void erase(int erase_x, int erase_y)
{
gotoxy(erase_x, erase_y);
cout << " ";
}
void player_draw(int draw_x, int draw_y)
{
gotoxy(draw_x, draw_y);
cout << "∧";
}
void bullets_draw_vertical(int bullets_draw_x, int bullets_draw_y)
{
gotoxy(bullets_draw_x, bullets_draw_y);
cout << "┇";
}
void bullets_draw_horizontal(int bullets_draw_x, int bullets_draw_y)
{
gotoxy(bullets_draw_x, bullets_draw_y);
cout << "┅";
}
void time()
{
gotoxy(0, 0);
if (clock() - t1 > 100)
{
n += 0.1;
cout << n;
t1 = clock();
}
if(n>10)
{
level=200;
if(n>20)
{
level=100;
if(n>25)
{
level=50;
if(n>30)
{
system("cls");
gotoxy(20,10);
cout<<"gameclear!";
system("pause");
exit(0);
}
}
}
}

}
void Player_move()
{
if (clock() - player_move > 50)
{
erase(player_x, player_y);
if (GetAsyncKeyState(VK_ESCAPE))exit(0);
if (GetAsyncKeyState(VK_UP))--player_y;
if (GetAsyncKeyState(VK_DOWN))++player_y;
if (GetAsyncKeyState(VK_LEFT))--player_x;
if (GetAsyncKeyState(VK_RIGHT))++player_x;
if (GetAsyncKeyState(VK_LSHIFT))n=30;
if (player_x > 72) player_x = 72;
if (player_y > 24) player_y = 24;
if (player_x < 0) player_x = 0;
if (player_y < 0) player_y = 0;
player_draw(player_x, player_y);
player_move = clock();
}
}
void RAM_t()
{
if (clock() - RAM > 200)
{
ram = rand() % 3 + 1;
RAM = clock();
}
}
void create()
{
if (clock() - create_t > level)
{
int ram_x = rand() % 75 + 1, ram_y = rand() % 24 + 1;
if (ram == 1)
{
Bullets bullet;
bullet.x = ram_x;
bullet.y = 0;
above_1.push_back(bullet);
}
if (ram == 2)
{
Bullets bullet;
bullet.x = 0;
bullet.y = ram_y;
left_1.push_back(bullet);
}
if (ram == 3)
{
Bullets bullet;
bullet.x = 72;
bullet.y = ram_y;
right_1.push_back(bullet);
}
create_t = clock();
}
}
void bullets_above_1_move()
{
if (clock() - bullets_above_1 > 50)
{
for (p1 = above_1.begin(); p1 != above_1.end();)
{
erase(p1->x, p1->y);
++p1->y;
if (p1->y > 24)
{
p1 = above_1.erase(p1);
}
else
{
bullets_draw_vertical(p1->x, p1->y);
if (abs(player_x - p1->x) < 1 && player_y == p1->y)
{
system("cls");
gotoxy(20,10);
cout << "gameover!";
system("pause");
exit(0);
}
++p1;
}
}
bullets_above_1 = clock();
}
}
void bullets_left_1_move()
{
if (clock() - bullets_left_1 > 50)
{
for (p2 = left_1.begin(); p2 != left_1.end();)
{
erase(p2->x, p2->y);
p2->x = p2->x + 2;
if (p2->x > 72)
{
p2 = left_1.erase(p2);
}
else
{
bullets_draw_horizontal(p2->x, p2->y);
if (player_x == p2->x&&abs(player_y - p2->y) < 1)
{
system("cls");
gotoxy(20,10);
cout << "gameover!";
system("pause");
exit(0);
}
++p2;
}
}
bullets_left_1 = clock();
}
}
void bullets_right_1_move()
{
if (clock() - bullets_right_1 > 50)
{
for (p3 = right_1.begin(); p3 != right_1.end();)
{
erase(p3->x, p3->y);
p3->x = p3->x - 2;
if (p3->x <0)
{
p3 = right_1.erase(p3);
}
else
{
bullets_draw_horizontal(p3->x, p3->y);
if (player_x == p3->x&&abs(player_y - p3->y) < 1)
{
system("cls");
gotoxy(20,10);
cout << "gameover!";
system("pause");
exit(0);
}
++p3;
}
}
bullets_right_1 = clock();
}
}
void main_control()
{
while (true)
{
Player_move();
time();
RAM_t();
create();
bullets_above_1_move();
bullets_left_1_move();
bullets_right_1_move();
}
}
~touhou()
{
show_cursor();
}
};

int main()
{
srand(time(0));
touhou play;
play.main_control();
return 0;
}

2021年秋季 VS版本弹幕游戏

这个主要逻辑跟上面那个版本一样,但是这个我是真拿他当大作业交了

用了vs的一个MSF库好像叫,一个图形界面的库,但是让我强行高刷当游戏界面使了(悲)

下面是vs代码最主要的结构,资源啥的我不知道有没有,大概直接把代码拷过去再稍微调试一下就行

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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

// 弹幕游戏View.cpp: C弹幕游戏View 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "弹幕游戏.h"
#endif

#include "弹幕游戏Doc.h"
#include "弹幕游戏View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

const int tt = 5;

// C弹幕游戏View

IMPLEMENT_DYNCREATE(C弹幕游戏View, CView)

BEGIN_MESSAGE_MAP(C弹幕游戏View, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_WM_KEYUP()
ON_COMMAND(ID_start, &C弹幕游戏View::Onstart)
ON_WM_CREATE()
ON_COMMAND(ID_stop, &C弹幕游戏View::Onstop)
END_MESSAGE_MAP()

// C弹幕游戏View 构造/析构

C弹幕游戏View::C弹幕游戏View() noexcept
{
// TODO: 在此处添加构造代码
level = 150;
n = 0;
player_x = 300; player_y = 200;
}

C弹幕游戏View::~C弹幕游戏View()
{
}

BOOL C弹幕游戏View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式

return CView::PreCreateWindow(cs);
}

// C弹幕游戏View 绘图

void C弹幕游戏View::OnDraw(CDC* /*pDC*/)
{
C弹幕游戏Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
player_draw();
// TODO: 在此处为本机数据添加绘制代码
}


// C弹幕游戏View 打印

BOOL C弹幕游戏View::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}

void C弹幕游戏View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}

void C弹幕游戏View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}


// C弹幕游戏View 诊断

#ifdef _DEBUG
void C弹幕游戏View::AssertValid() const
{
CView::AssertValid();
}

void C弹幕游戏View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

C弹幕游戏Doc* C弹幕游戏View::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(C弹幕游戏Doc)));
return (C弹幕游戏Doc*)m_pDocument;
}
#endif //_DEBUG


// C弹幕游戏View 消息处理程序


void C弹幕游戏View::player_draw()
{
CClientDC dc(this);
CPen* pen = new CPen(PS_SOLID, 0, RGB(255, 255, 255));
CBrush* brush = new CBrush(RGB(255, 0, 0));
dc.SelectObject(pen);
dc.SelectObject(brush);
dc.Ellipse(player_x-5, player_y-10, player_x + 5, player_y + 10);
}




void C弹幕游戏View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_ESCAPE:ESC = 1; break;
case VK_UP:UP = 1; break;
case VK_DOWN:DOWN = 1; break;
case VK_LEFT:LEFT = 1; break;
case VK_RIGHT:RIGHT = 1; break;
case VK_SPACE :SPACE = 1; break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}


void C弹幕游戏View::OnTimer(UINT_PTR nIDEvent)
{
CClientDC dc(this);
CPen* pen = new CPen(PS_SOLID, 0, RGB(255, 255, 255));
CBrush* brush = new CBrush(RGB(0, 255, 0));
dc.SelectObject(pen);
dc.SelectObject(brush);
CString c;
if (nIDEvent == 1)
{
if (ESC == 1)
{
exit(0);
}
if (UP == 1)
{
player_y = player_y - 5; if (player_y < 0)player_y = 0;
}
if (DOWN == 1)
{
player_y = player_y + 5; if (player_y > 400)player_y = 400;
}
if (LEFT == 1)
{
player_x = player_x - 5; if (player_x < 0)player_x = 0;
}
if (RIGHT == 1)
{
player_x = player_x + 5; if (player_x > 600)player_x = 600;
}
if (SPACE == 1)
{
dc.TextOutW(700, 200, _T("gameclear!"));
exit(0);
}
}
if (nIDEvent == 2)
{
int ram_x = rand() % 600 + 1, ram_y = rand() % 400 + 1;
if (ram == 1)
{
bullets bullet;
bullet.x = ram_x;
bullet.y = 0;
above_1.push_back(bullet);
}
if (ram == 2)
{
bullets bullet;
bullet.x = 0;
bullet.y = ram_y;
left_1.push_back(bullet);
}
if (ram == 3)
{
bullets bullet;
bullet.x = 600;
bullet.y = ram_y;
right_1.push_back(bullet);
}
}
if (nIDEvent == 3)
{
for (p1 = above_1.begin(); p1 != above_1.end();)
{
p1->y = p1->y + 5;
dc.Ellipse(p1->x - 5, p1->y - 10, p1->x + 5, p1->y + 10);
if (p1->y > 400)
{
p1 = above_1.erase(p1);
}
else
{
if (abs(player_x - p1->x)<5 && abs(player_y - p1->y)<10)
{
dc.TextOutW(700, 200, _T("gameover!"));
Sleep(1000);
exit(0);
}
++p1;
}
}
}
if (nIDEvent == 4)
{
for (p2 = left_1.begin(); p2 != left_1.end();)
{
p2->x = p2->x + 7;
dc.Ellipse(p2->x - 10, p2->y - 5, p2->x + 10, p2->y + 5);
if (p2->x > 600)
{
p2 = left_1.erase(p2);
}
else
{
if (abs(player_x - p2->x) < 10 && abs(player_y - p2->y) < 5)
{
dc.TextOutW(700, 200, _T("gameover!"));
Sleep(1000);
exit(0);
}
++p2;
}
}
}
if (nIDEvent == 5)
{
for (p3 = right_1.begin(); p3 != right_1.end();)
{
p3->x = p3->x - 7;
dc.Ellipse(p3->x - 10, p3->y - 5, p3->x + 10, p3->y + 5);
if (p3->x < 0)
{
p3 = right_1.erase(p3);
}
else
{
if (abs(player_x - p3->x) < 10 && abs(player_y - p3->y) < 5)
{
dc.TextOutW(700, 200, _T("gameover!"));
Sleep(1000);
exit(0);
}
++p3;
}
}
}
if (nIDEvent == 6)
{
ram = rand() % 3 + 1;
}
if (nIDEvent == 7)
{
n += 1;
}
if (nIDEvent == 8)
{
Invalidate();
}
if (nIDEvent == 9)
{
CPen pen(PS_DOT, 1, RGB(0, 0, 0));
CPen *pOldPen = dc.SelectObject(&pen);
dc.MoveTo(600,0);
dc.LineTo(600,400);
dc.MoveTo(600, 400);
dc.LineTo(0, 400);
dc.SelectObject(pOldPen);
n1 = n;
c.Format(_T("%d"), n1);
dc.TextOutW(700, 50, c);
dc.TextOutW(700, 100, _T("坚持十秒即可胜利!"));
if (n >= 9.9)
{
dc.TextOutW(700, 200, _T("gameclear!"));
}
if (n > 10)
{
Sleep(1000);
exit(0);
}
}
CView::OnTimer(nIDEvent);
}


void C弹幕游戏View::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_UP:UP=0; break;
case VK_DOWN:DOWN=0; break;
case VK_LEFT:LEFT=0; break;
case VK_RIGHT:RIGHT=0; break;
}

CView::OnKeyUp(nChar, nRepCnt, nFlags);
}



void C弹幕游戏View::Onstart()
{
CClientDC dc(this);
dc.TextOutW(700, 100, _T("坚持十秒即可胜利!"));
SetTimer(1, tt, NULL);
SetTimer(2, level, NULL);
SetTimer(3, tt, NULL);
SetTimer(4, tt, NULL);
SetTimer(5, tt, NULL);
SetTimer(6, 200, NULL);
SetTimer(7, 1000, NULL);
SetTimer(8, 20, NULL);
SetTimer(9, tt, NULL);
}

void C弹幕游戏View::gotoxy(int x, int y)
{
HANDLE h;//句柄,对象的索引
COORD c;//结构体,坐标值
c.X = x;
c.Y = y;
h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, c);
}

int C弹幕游戏View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
CClientDC dc(this);


return 0;
}


void C弹幕游戏View::Onstop()
{
// TODO: 在此添加命令处理程序代码
exit(0);
}

3.2022最终版本 C++实现

这个版本有画面了,我称之为维度升级(但其实真实维度上并没有升级(悲))

直接上源码,但是没有资源,完事我再把exe给扔到github里,项目源码给我发消息可以给

https://github.com/Nonbliss/touhou-with-C-/tree/%E5%BC%B9%E5%B9%95%E6%B8%B8%E6%88%8F

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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
#include <graphics.h>
#include <conio.h>
#include <windows.h>
#include <iostream>
#include <ctime>
#include <list>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <string.h>
#include <math.h>
#include <Windows.h>
#include "resource.h"
#include <mmsystem.h>
# pragma comment (lib, "Winmm.lib")

#define Stagenumber 3

//抽象数据类型
//队列
typedef struct Qnode {
int x;
int y;
int hit;
struct Qnode* next;
}*bullet;

typedef struct Queue {
bullet front;
bullet rear;
}bullets;

//栈
typedef struct Snode {
int health;
struct Snode* next;
}Snode,*Stack;

//图
typedef struct arccell {
int through;
}arc1,arcs[Stagenumber][Stagenumber];

typedef struct vexs {
int stage;
int boss_length, boss_width;
Stack boss_health;
int bullet_above_judge, bullet_left_judge, bullet_right_judge;
int boss_movement;
}Stage;

typedef struct {
Stage Stages[Stagenumber];
arcs Stage_through;
}Mgraph;

//全局变量
int player_x=320, player_y=280,boss_x=320,boss_y=100,spawnlimit=1000,bullet_above_speed=50,
bullet_left_speed=50, bullet_right_speed=50, bullet_player_speed=50,player_shoot_speed=400,
way=0,levelup_t=0, levelup_s = 0,explode_s=0,explode_t=0,explode_x,explode_y;
clock_t time1_t,player_move_t,draw_t,bullet_spawn1,bullet_spawn2,bullet_spawn3,
bullet_move1, bullet_move2, bullet_move3, bullet_move4, player_shoot_t,boss_move_s,boss_move_t,boss_move_q,stage_pass_t,time_draw_t,time_draw_s;
IMAGE background, plane, bullet_up, bullet_above, bullet_left, bullet_right, boss1, boss2, boss3, explode, explode_small, title,start_button;
bullets bullet_above_q, bullet_left_q, bullet_right_q,bullet_player_q;
Mgraph map;
//用户可交互参数
Stack player_health;
int clear=0,stagenum,difficulty,test1,score,level=0;
int clear_num=1;

int InitQueue(bullets& q)
{
q.front = q.rear = (bullet)malloc(sizeof(Qnode));
q.front->next = NULL;
if (!q.front)
return 0;
else
return 1;
}

void push(Stack& s, int e)
{
Snode* p;
p = (Snode*)malloc(sizeof(Snode));
p->health = e;
p->next = s->next;
s->next = p;
}

int InitStack_boss(Stack& s)
{
s = (Snode*)malloc(sizeof(Snode));
if (!s)
{
outtextxy(280, 280, "ERROR OCCURED!");
Sleep(2000);
exit(0);
}
else
s->next = NULL;
}

void InitGraph(Mgraph& m)
{
int i = rand() % 2,j=1-i;
m.Stage_through[i][Stagenumber - 1].through = 1;
m.Stage_through[j][i].through = 1;
stagenum = j;
m.Stages[0].stage = 0;
m.Stages[0].boss_movement = 0;
m.Stages[0].boss_length = 90;
m.Stages[0].boss_width = 30;
m.Stages[0].bullet_above_judge = 1;
m.Stages[0].bullet_left_judge = 0;
m.Stages[0].bullet_right_judge = 0;
InitStack_boss(m.Stages[0].boss_health);
push(m.Stages[0].boss_health, 1);
for (i = 0; i < 20; ++i)
push(m.Stages[0].boss_health, 2);
for (i = 0; i < 20; ++i)
push(m.Stages[0].boss_health, 1);
m.Stages[1].stage = 1;
m.Stages[1].boss_movement = 1;
m.Stages[1].boss_length = 30;
m.Stages[1].boss_width = 40;
m.Stages[1].bullet_above_judge = 0;
m.Stages[1].bullet_left_judge = 1;
m.Stages[1].bullet_right_judge = 1;
InitStack_boss(m.Stages[1].boss_health);
push(m.Stages[1].boss_health, 1);
for (i = 0; i < 7; ++i)
push(m.Stages[1].boss_health, 2);
for (i = 0; i < 4; ++i)
push(m.Stages[1].boss_health, 1);
m.Stages[2].stage = 2;
m.Stages[2].boss_movement = 3;
m.Stages[2].boss_length = 50;
m.Stages[2].boss_width = 50;
m.Stages[2].bullet_above_judge = 1;
m.Stages[2].bullet_left_judge = 1;
m.Stages[2].bullet_right_judge = 1;
InitStack_boss(m.Stages[2].boss_health);
for (i = 0; i < 15; ++i)
push(m.Stages[2].boss_health, 3);
for (i = 0; i < 15; ++i)
push(m.Stages[2].boss_health, 2);
}

void push_player(Stack& s)
{
Snode* p;
p = (Snode*)malloc(sizeof(Snode));
if (s->next == NULL)
{
p->health = 1;
p->next = s->next;
s->next = p;
}
else
{
p->health = s->next->health+1;
p->next = s->next;
s->next = p;
}
}

void player_pop(Stack& s)
{
if (s->next != NULL)
{
Snode* p;
p = s->next;
s->next = p->next;
free(p);
}
}

void gameover()
{
PlaySound("EXPLODE", NULL, SND_FILENAME | SND_ASYNC);
putimage(0, 0, &background);
putimage(player_x - 50, player_y - 50, &explode);
outtextxy(270, 210, "GAMEOVER!");
Sleep(3000);
exit(0);
}

void gameclear()
{
putimage(0, 0, &background);
outtextxy(270, 210, "GAMECLEAR!");
outtextxy(250, 230, "CONGRATULATIONS!");
Sleep(3000);
exit(0);
}

void stage_pass()
{
stage_pass_t = clock();
while (clock() - stage_pass_t < 2000)
{
putimage(boss_x-50, boss_y-50, &explode);
outtextxy(290, 210, "stage");
char num[20];
sprintf(num, "%d", clear_num);
outtextxy(300, 230, num);
outtextxy(290, 250, "clear");
}
stage_pass_t = clock();
clear_num++;
clearrectangle(290, 210, 330, 320);
if (clear_num == 4)
gameclear();
while (clock() - stage_pass_t < 2000)
{
outtextxy(280, 220, "stage");
char num[20];
sprintf(num, "%d", clear_num);
outtextxy(340, 220, num);
}
}

void next_stage()
{
if (clear == 1)
{
clear = 0;
stage_pass();
int i;
for (i = 0; i < Stagenumber; ++i)
{
if (map.Stage_through[stagenum][i].through == 1)
{
stagenum = i;
break;
}
}
}
}

void boss_pop(Stack& s)
{
if (s->next == NULL)
{
clear = 1;
next_stage();
}
else
{
Snode* p;
p = s->next;
if (p->health == 1)
{
spawnlimit = 1000; bullet_above_speed = 50; bullet_left_speed = 50; bullet_right_speed = 50;
}
if (p->health == 2)
{
spawnlimit = 500; bullet_above_speed = 40; bullet_left_speed = 40; bullet_right_speed = 40;
}
if (p->health == 3)
{
spawnlimit = 500; bullet_above_speed = 40; bullet_left_speed = 40; bullet_right_speed = 40;
}
s->next = p->next;
free(p);
}
}

void bullet_explode()
{
if (explode_s == 1)
{
putimage(explode_x - 10, explode_y - 10, &explode_small);
explode_t++;
}
if (explode_t > 10)
{
explode_s = 0;
}
}

int boss_hit(int x,int y,int n)
{
if (abs(x - boss_x) < map.Stages[stagenum].boss_length / 2 && abs(y - boss_y) < map.Stages[stagenum].boss_width / 2 && n == 0)
{
PlaySound("EXPLODE", NULL, SND_FILENAME | SND_ASYNC);
explode_x = x;
explode_y = y;
explode_s = 1;
boss_pop(map.Stages[stagenum].boss_health);
score += (score % 3 + 1) * (rand() % 50 + 50);
return 1;
}
else if (n == 1)
{
return 1;
}
else
{
return 0;
}
}

int player_hit(int x, int y,int n)
{
if (abs(x-player_x)<8 && abs(y-player_y)<8 && n==0)
{
PlaySound("EXPLODE", NULL, SND_FILENAME | SND_ASYNC);
explode_x = x;
explode_y = y;
explode_s = 1;
player_pop(player_health);
return 1;
}
else if (n == 1)
{
return 1;
}
else
{
return 0;
}
}

int InitStack_player(Stack& s)
{
s = (Snode*)malloc(sizeof(Snode));
if (!s)
{
outtextxy(280, 280, "ERROR OCCURED!");
Sleep(2000);
exit(0);
}
else
{
s->next = NULL;
push_player(s);
push_player(s);
push_player(s);
}
}

void player_draw(int x, int y)
{
clearrectangle(x - 10, y - 10, x + 10, y + 10);
putimage(x-10, y-10, &plane);
}

void boss_draw()
{
switch (stagenum)
{
case 0:
putimage(boss_x - 45, boss_y - 15, &boss1);
break;
case 1:
putimage(boss_x - 15, boss_y - 20, &boss2);
break;
case 2:
putimage(boss_x - 25, boss_y - 25, &boss3);
break;
}
}

void time_draw()
{
int time_ = 180-time_draw_t / 1000;
char num[20];
sprintf(num, "%d", time_);
outtextxy(600, 5, num);
outtextxy(520, 5, "TIME LEFT:");
if (time_ == 0)
{
gameover();
}
time_draw_t = clock()- time_draw_s;
}

void health_draw()
{
if (player_health->next != NULL)
{
int n = player_health->next->health;
switch (n)
{
case 5:
fillrectangle(10, 330, 30, 350);
case 4:
fillrectangle(10, 360, 30, 380);
case 3:
fillrectangle(10, 390, 30, 410);
case 2:
fillrectangle(10, 420, 30, 440);
case 1:
fillrectangle(10, 450, 30, 470);
}
}
}

void boss_move()
{
if (clock() - boss_move_s > 2000 && (map.Stages[stagenum].boss_movement == 0|| map.Stages[stagenum].boss_movement == 3))
{
way = rand() % 4;
boss_move_s = clock();
}
if (clock() - boss_move_t > 50 && (map.Stages[stagenum].boss_movement == 0 || map.Stages[stagenum].boss_movement == 3))
{
switch (way)
{
case 0:
boss_x = boss_x - 3;
if (boss_x < 100)
way = 2;
break;
case 1:
boss_y = boss_y - 3;
if (boss_y < 50)
way = 3;
break;
case 2:
boss_x = boss_x + 3;
if (boss_x > 540)
way = 0;
break;
case 3:
boss_y = boss_y + 3;
if (boss_y > 200)
way = 1;
break;
}
boss_move_t = clock();
}
if (clock() - boss_move_q > 3000 && (map.Stages[stagenum].boss_movement == 1 || map.Stages[stagenum].boss_movement == 3))
{
boss_x = rand() % 440 + 100;
boss_y = rand() % 150 + 50;
boss_move_q = clock();
}
}

void bullet_above_spawn()
{
if (clock() - bullet_spawn1 > spawnlimit )
{
bullet p;
p = (bullet)malloc(sizeof(Qnode));
if (!p)
{
outtextxy(280, 280, "ERROR OCCURED!");
Sleep(2000);
exit(0);
}
p->y = 0;
p->x = rand() % 640;
p->hit = 0;
p->next = NULL;
bullet_above_q.rear->next = p;
bullet_above_q.rear = p;
bullet_spawn1 = clock();
}
}

void bullet_left_spawn()
{
if (clock() - bullet_spawn2 > spawnlimit)
{
bullet p;
p = (bullet)malloc(sizeof(Qnode));
if (!p)
{
outtextxy(280, 280, "ERROR OCCURED!");
Sleep(2000);
exit(0);
}
p->x = 0;
p->y = rand() % 480;
p->hit = 0;
p->next = NULL;
bullet_left_q.rear->next = p;
bullet_left_q.rear = p;
bullet_spawn2 = clock();
}
}

void bullet_right_spawn()
{
if (clock() - bullet_spawn3 > spawnlimit )
{
bullet p;
p = (bullet)malloc(sizeof(Qnode));
if (!p)
{
outtextxy(280, 280, "ERROR OCCURED!");
Sleep(2000);
exit(0);
}
p->x = 680;
p->y = rand() % 480;
p->hit = 0;
p->next = NULL;
bullet_right_q.rear->next = p;
bullet_right_q.rear = p;
bullet_spawn3 = clock();
}
}

void bullet_player_spawn()
{
bullet p;
p = (bullet)malloc(sizeof(Qnode));
if (!p)
{
outtextxy(280, 280, "ERROR OCCURED!");
Sleep(2000);
exit(0);
}
p->x = player_x;
p->y = player_y-10;
p->hit = 0;
p->next = NULL;
bullet_player_q.rear->next = p;
bullet_player_q.rear = p;
}

void bullet_above_move()
{
if (clock() - bullet_move1 > bullet_above_speed)
{
bullet p;
p = bullet_above_q.front->next;
while(p && bullet_above_q.front != bullet_above_q.rear)
{
p->y = p->y + 10;
p->hit=player_hit(p->x, p->y,p->hit);
if (p->y > 480)
{
bullet_above_q.front->next = p->next;
bullet q=p;
p = p->next;
free(q);
}
else
p = p->next;
}
bullet_move1 = clock();
}
}

void bullet_left_move()
{
if (clock() - bullet_move2 > bullet_left_speed)
{
bullet p;
p = bullet_left_q.front->next;
while (p && bullet_left_q.front != bullet_left_q.rear)
{
p->x = p->x + 10;
p->hit = player_hit(p->x, p->y,p->hit);
if (p->x > 640)
{
bullet_left_q.front->next = p->next;
bullet q = p;
p = p->next;
free(q);
}
else
p = p->next;
}
bullet_move2 = clock();
}
}

void bullet_right_move()
{
if (clock() - bullet_move3 > bullet_right_speed)
{
bullet p;
p = bullet_right_q.front->next;
while (p && bullet_right_q.front != bullet_right_q.rear)
{
p->x = p->x - 10;
p->hit = player_hit(p->x, p->y,p->hit);
if (p->x < 0)
{
bullet_right_q.front->next = p->next;
bullet q = p;
p = p->next;
free(q);
}
else
p = p->next;
}
bullet_move3 = clock();
}
}

void bullet_player_move()
{
if (clock() - bullet_move4 > bullet_player_speed)
{
bullet p;
p = bullet_player_q.front->next;
while (p && bullet_player_q.front != bullet_player_q.rear)
{
p->y = p->y - 10;
p->hit = boss_hit(p->x, p->y, p->hit);
if (p->x < 0)
{
bullet_player_q.front->next = p->next;
bullet q = p;
p = p->next;
free(q);
}
p = p->next;
}
bullet_move4 = clock();
}
}

void bullet_above_draw()
{
bullet p;
p = bullet_above_q.front->next;
while(p && bullet_above_q.front != bullet_above_q.rear)
{
if (p->hit == 0)
{
clearrectangle(p->x - 4, p->y - 8, p->x + 4, p->y + 8);
putimage(p->x - 4, p->y - 8, &bullet_above);
}
p = p->next;
}
}

void bullet_left_draw()
{
bullet p;
p = bullet_left_q.front->next;
while (p && bullet_left_q.front != bullet_left_q.rear)
{
if (p->hit == 0)
{
clearrectangle(p->x - 8, p->y - 4, p->x + 8, p->y + 4);
putimage(p->x - 8, p->y - 4, &bullet_left);
}
p = p->next;
}
}

void bullet_right_draw()
{
bullet p;
p = bullet_right_q.front->next;
while (p && bullet_right_q.front != bullet_right_q.rear)
{
if (p->hit == 0)
{
clearrectangle(p->x - 8, p->y - 4, p->x + 8, p->y + 4);
putimage(p->x - 8, p->y - 4, &bullet_right);
}
p = p->next;
}
}

void bullet_player_draw()
{
bullet p;
p = bullet_player_q.front->next;
while (p && bullet_player_q.front != bullet_player_q.rear)
{
if (p->hit == 0)
{
clearrectangle(p->x - 4, p->y - 8, p->x + 4, p->y + 8);
putimage(p->x - 4, p->y - 8, &bullet_up);
}
p = p->next;
}
}

void score_out()
{
char num[20];
sprintf(num, "%d", score);
outtextxy(10, 10, "SCORE:");
outtextxy(70, 10, num);
}

void levelup_draw()
{
if ( levelup_s ==1)
{
outtextxy(10, 30, "level up!");
levelup_t++;
}
if (levelup_t > 60)
{
levelup_s = 0;
}
}

void levelup()
{
if (score > 8000 && level == 0)
{
level++;
player_shoot_speed = 300;
push_player(player_health);
levelup_s = 1;
}
else if (score > 13000 && level == 1)
{
level++;
player_shoot_speed = 200;
push_player(player_health);
levelup_s = 1;
}
}

void dont_touch_boss()
{
if (abs(player_x - boss_x) < map.Stages[stagenum].boss_length / 2 && abs(player_y - boss_y) < map.Stages[stagenum].boss_width / 2)
{
gameover();
}
}

void test()
{
char num[20];
sprintf(num, "%d", map.Stages[stagenum].bullet_above_judge);
outtextxy(10, 100, num);
sprintf(num, "%d", map.Stages[stagenum].bullet_left_judge);
outtextxy(10, 150, num);
sprintf(num, "%d", map.Stages[stagenum].bullet_right_judge);
outtextxy(10, 200, num);
}

void draw()
{
if (clock() - draw_t > 50)
{
if (player_health->next == NULL)
gameover();
putimage(0, 0, &background);
player_draw(player_x, player_y);
boss_draw();
if (map.Stages[stagenum].bullet_above_judge == 1)
bullet_above_draw();
if (map.Stages[stagenum].bullet_left_judge == 1)
bullet_left_draw();
if (map.Stages[stagenum].bullet_right_judge == 1)
bullet_right_draw();
bullet_player_draw();
time_draw();
health_draw();
score_out();
levelup_draw();
bullet_explode();
//test();
draw_t = clock();
}
}

void player_move()
{
if (clock() - player_move_t > 50)
{
if (GetAsyncKeyState(VK_ESCAPE))exit(0);
if (GetAsyncKeyState(VK_UP))player_y=player_y-5;
if (GetAsyncKeyState(VK_DOWN))player_y=player_y+5;
if (GetAsyncKeyState(VK_LEFT))player_x=player_x-5;
if (GetAsyncKeyState(VK_RIGHT))player_x=player_x+5;
if (player_x > 640) player_x = 640;
if (player_y > 480) player_y = 480;
if (player_x < 0) player_x = 0;
if (player_y < 0) player_y = 0;
player_move_t = clock();
}
if (clock() - player_shoot_t > player_shoot_speed)
{
if (GetAsyncKeyState(VK_SPACE))
{
bullet_player_spawn();
PlaySound("SHOT", NULL, SND_FILENAME | SND_ASYNC);
}
player_shoot_t = clock();
}
}

void touhou()
{
while (true)
{
player_move();
boss_move();
if (map.Stages[stagenum].bullet_above_judge == 1)
{
bullet_above_spawn();
bullet_above_move();
}
if (map.Stages[stagenum].bullet_left_judge == 1)
{
bullet_left_spawn();
bullet_left_move();
}
if (map.Stages[stagenum].bullet_right_judge == 1)
{
bullet_right_spawn();
bullet_right_move();
}
bullet_player_move();
levelup();
dont_touch_boss();
draw();
}
}

int main()
{
initgraph(640, 480);
cleardevice();
setcolor(BLACK);
settextcolor(WHITE);
outtextxy(280, 240, "loading...");
loadimage(&background,_T("IMAGE"), _T("background"), 640, 480, 1);
loadimage(&plane, _T("IMAGE"), _T("plane"), 20, 20, 0);
loadimage(&bullet_up, _T("IMAGE"), _T("bullet_above"), 8, 16, 0);
loadimage(&bullet_above, _T("IMAGE"), _T("bullets_down"), 8, 16, 0);
loadimage(&bullet_left, _T("IMAGE"), _T("bullet_right"), 16, 8, 0);
loadimage(&bullet_right, _T("IMAGE"), _T("bullet_left"), 16, 8, 0);
loadimage(&boss1, _T("IMAGE"), _T("boss1"), 90, 30, 0);
loadimage(&boss2, _T("IMAGE"), _T("boss2"), 30, 40, 0);
loadimage(&boss3, _T("IMAGE"), _T("boss3"), 50, 50, 0);
loadimage(&explode, _T("IMAGE"), _T("explode"), 100, 100, 0);
loadimage(&explode_small, _T("IMAGE"), _T("explode"), 20, 20, 0);
loadimage(&title, _T("IMAGE"), _T("title"), 400, 200, 0);
loadimage(&start_button, _T("IMAGE"), _T("start_button"), 100, 50, 0);
player_move_t = clock();
bullet_spawn1 = clock();
bullet_spawn2 = clock();
bullet_spawn3 = clock();
bullet_move1 = clock();
bullet_move2 = clock();
bullet_move3 = clock();
player_shoot_t = clock();
boss_move_s = clock();
boss_move_t = clock();
boss_move_q = clock();
srand(time(NULL));
if (!InitQueue(bullet_above_q) || !InitQueue(bullet_left_q) || !InitQueue(bullet_right_q) || !InitQueue(bullet_player_q))
{
outtextxy(280, 280, "ERROR OCCURED!");
Sleep(2000);
exit(0);
}
InitStack_player(player_health);
InitGraph(map);
putimage(0, 0, &background);
putimage(120, 60, &title);
putimage(270, 335, &start_button);
mciSendString("play bgm.mp3 repeat", NULL, 0, NULL);
MOUSEMSG m;
while (true)
{
m = GetMouseMsg();
if (m.mkLButton)
{
if (m.x > 270 && m.x < 370 && m.y>335 && m.y < 385)
{
break;
}
}
}
putimage(0, 0, &background);
outtextxy(280, 220, "stage");
char num[20];
sprintf(num, "%d", 1);
outtextxy(340, 220, num);
outtextxy(230, 280, "↑ ↓ ← → 控制移动方向");
outtextxy(265, 310, "Space 发射子弹");
outtextxy(255, 340, "左下角是你的血量");
outtextxy(240, 370, "右上角是你剩余的时间");
Sleep(4000);
time_draw_t = clock();
time_draw_s = clock();
touhou();
return 0;
}