創建win32控制臺應用程序,選擇空項目。
創建完畢后,右擊頭文件新建一個頭文件(.h)。名字寫main.h
然后在源文件新建一個c++文件(.cpp)。名字寫main.cpp
如圖:
然后在main.h里面寫上如下代碼:
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
|
#include <stdio.h> #include <graphics.h> #include <time.h> #include <stdlib.h> #include <math.h> #include <mmsystem.h> #pragma comment(lib,"winmm.lib") #define pi 3.1415926535 #define num 13 //煙花彈 struct jet { int x, y; //坐標 int hx,hy; //最高點坐標 bool shoot; //判斷煙花彈是不是發射狀態 dword t1, t2, dt; //煙花彈發射的速度 image img[2]; //圖片2張 一明一暗的兩張 byte n : 1; //結構體 位段 變量 n 占用內存的一個位! }jet[num]; //煙花 struct fire { int r; //半徑 int maxr; //最大半徑 int x, y; //相對于窗口 中心點的坐標 int cenx, ceny; //相對于圖片本身 中心點的坐標 int width, height; int xy[240][240]; //圖片 保存到一個像素矩陣。 bool show; //是否綻放 bool draw; //是否繪制 dword t1, t2, dt; //爆炸的速度 }fire[num]; void init( int i); void load(); void chose(dword& t1); void shoot(); void show(dword* pem); 在main.cpp寫上如下代碼: /**************************************************** ***************************************************** ****************表白浪漫煙花項目開發******************** ****************主講人:三黑之毅************************ ****************最后修改:2019年3月20日**************** ***************************************************** ****************************************************/ #include "main.h" int main() { //打開一個窗口 1200 800 initgraph(1200, 800); srand ((unsigned int ) time (null)); mcisendstring(l "open ./fire/千年.mp3 alias bgm" , 0, 0, 0); mcisendstring(l "play bgm repeat" , 0, 0, 0); dword t1 = gettickcount(); dword* pem = getimagebuffer(); for ( int i = 0; i < num; i++) { init(i); } load(); beginbatchdraw(); while (1) { sleep(10); for ( int clr = 0; clr < 1000; clr++) { for ( int j = 0; j < 2; j++) { int px1 = rand () % 1200; int py1 = rand () % 800; if (py1 < 799) { pem[py1 * 1200 + px1] = pem[py1 * 1200 + px1 + 1] = black; } } } chose(t1); shoot(); show(pem); flushbatchdraw(); } return 0; } //初始化 13個 void init( int i) { // 分別為:煙花中心到圖片邊緣的最遠距離、煙花中心到圖片左上角的距離 (x、y) 兩個分量 int r[15] = { 120, 120, 155, 123, 130, 147, 138, 138, 130, 135, 140, 132, 155, 145, 124 }; int x[15] = { 120, 120, 110, 117, 110, 93, 102, 102, 110, 105, 100, 108, 110, 145, 124 }; int y[15] = { 120, 120, 85, 118, 120, 103, 105, 110, 110, 120, 120, 104, 85, 145, 124 }; fire[i].width = 500; fire[i].height = 500; fire[i].maxr = r[i]; fire[i].cenx = x[i]; fire[i].ceny = y[i]; fire[i].show = false ; fire[i].t1 = gettickcount(); fire[i].dt = 5; fire[i].r = 0; //初始化煙花彈的部分數據 jet[i].t1 = gettickcount(); //定個時間點 jet[i].dt = rand () % 10; //0-9 ms jet[i].n = 0; jet[i].shoot = false ; //都是沒有發射的。 } //加載圖片 void load() //加載到哪里去? { image fm, gm; loadimage(&fm, l "./fire/flower.jpg" , 3120, 240); for ( int i = 0; i < num; i++) { setworkingimage(&fm); //fm去拿 getimage(&gm,i*240, 0, 240, 240); //gm setworkingimage(&gm); //gm去拿 for ( int a = 0; a < 240; a++) { for ( int b = 0; b < 240; b++) { fire[i].xy[a][b] = getpixel(a, b); } } } //文件夾 image sm; loadimage(&sm, l "./fire/shoot.jpg" , 200, 50); //保存在sm for ( int i = 0; i < num; i++) { setworkingimage(&sm); //sm去拿 int n = rand () % 5; //0-4 getimage(&jet[i].img[0], n*20,0,20,50); //暗的 getimage(&jet[i].img[1], (n + 5) * 20, 0, 20, 50); //亮的 } setworkingimage(null); //sm去拿 } //選擇煙花彈 煙花彈產生的速度!!! void chose(dword& t1) { dword t2 = gettickcount(); //13個 if (t2 - t1 > 1) //每隔1個ms 產生一個煙花彈 { int n = rand () % num; //0-12 if (jet[n].shoot == false &&fire[n].show == false ) //非發射狀態 { jet[n].x = rand ()%1200; jet[n].y = rand () % 100 + 600; //600-699之間 jet[n].hx = jet[n].x; jet[n].hy = rand ()%400; //0-399 jet[n].shoot = true ; //發射狀態 putimage(jet[n].x, jet[n].y, &jet[n].img[jet[n].n], srcinvert); } t1 = t2; } } void shoot() { for ( int i = 0; i < num; i++) { jet[i].t2 = gettickcount(); if (jet[i].t2 - jet[i].t1 >= jet[i].dt&&jet[i].shoot== true ) { putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], srcinvert); if (jet[i].y >= jet[i].hy) { jet[i].n++; //一閃一閃 jet[i].y -= 5; //上升 } //putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], srcinvert); if (jet[i].y <= jet[i].hy) //到達最高點了 { //擦除煙花彈 putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], srcinvert); //煙花彈最高點的 hx hy -----》煙花 fire[i].x = jet[i].hx; fire[i].y = jet[i].hy; //改變煙花彈的狀態 jet[i].shoot = false ; fire[i].show = true ; } putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], srcinvert); jet[i].t1 = jet[i].t2; } } } void show(dword* pem) { int dir[16] = { 5, 5, 5, 5, 5, 8, 25, 25, 25, 55, 55, 55, 55, 65, 65, 66 }; for ( int i = 0; i < num; i++) { fire[i].t2 = gettickcount(); if (fire[i].t2 - fire[i].t1>fire[i].dt&&fire[i].show == true ) { if (fire[i].r < fire[i].maxr) { fire[i].r++; fire[i].dt = dir[fire[i].r / 10]; fire[i].draw = true ; } if (fire[i].r >= fire[i].maxr - 1) { fire[i].draw = false ; init(i); } fire[i].t1 = fire[i].t2; } if (fire[i].draw == true ) { //圓 pi for ( double a = 0; a <= 6.28; a += 0.01) { int x1 = ( int )(fire[i].cenx + fire[i].r* cos (a)); int y1 = ( int )(fire[i].ceny - fire[i].r* sin (a)); if (x1 > 0 && x1<fire[i].width&&y1>0 && y1 < fire[i].height) { int b = fire[i].xy[x1][y1] & 0xff; int g = (fire[i].xy[x1][y1] >> 8) & 0xff; int r = fire[i].xy[x1][y1] >> 16; int xx = ( int )(fire[i].x + fire[i].r* cos (a)); int yy = ( int )(fire[i].y - fire[i].r* sin (a)); if (r>0x20 && g > 0x20 && b > 0x20 && xx > 0 && xx<1200 && yy>0 && yy < 800) pem[yy * 1200 + xx] = bgr(fire[i].xy[x1][y1]); } } fire[i].draw = false ; } } } |
注:音樂:千年.mp3,自己去網上下載,然后放到程序fire目錄下。
注:圖片素材就是這兩張:
自己下載一下這兩圖,也放到fire目錄下。
學習c語言:
幼兒園英語水平 初中數學水平 高中語文水平
也就是說只要會記單詞就行!!!
業務邏輯
寫程序的順序
這是最主要的,而并不是代碼
會了所有代碼,腦子里沒有業務邏輯,還是沒用,被稱為“碼農”
還是不懂的聯系q:2060164008或進q群:706625210。
今天直播到此結束!!!三黑之毅!