-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize2.c
More file actions
449 lines (361 loc) · 10.6 KB
/
initialize2.c
File metadata and controls
449 lines (361 loc) · 10.6 KB
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
//
// initialize.c
// LittleBee
//
// Created by 罗瑞笛 on 2019/2/26.
// Copyright © 2019年 罗瑞笛. All rights reserved.
//
#include "judgeLogic.h"
#include "initialize.h"
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
#include <SDL2_ttf/SDL_ttf.h>
// #include <SDL2_ttf/SDL_ttf.h>
#define WINDOW_WIDTH 704 // 11X 64
#define WINDOW_HEIGHT 704// 11X 64
int counter = 1;
//extern bool socre;
extern int count ;
extern int fenshu;
int enemynumber = 1;
int pos[99];
bool direct = true ;
//总共七个分数
char *word0 ="Score:0";
char *word1 ="Score:1";
char *word2 ="Score:2";
char *word3 ="Score:3";
char *word4 ="Score:4";
char *word5 ="Score:5";
char *word6 ="Score:6";
char *word7 ="Score:7";
//第六个 【5】
//存不存在字符串数组?
//game window
SDL_Window* window =NULL;
// 颜色
SDL_Color textColor ;
// 图像们
SDL_Surface* background = NULL ;
SDL_Surface* Bee = NULL;
SDL_Surface* bullet = NULL ;
SDL_Surface* enemybullet = NULL ; //敌人子弹
SDL_Surface* dragon = NULL ;
//文字surface
SDL_Surface* TextSurface = NULL ;
SDL_Surface* TextSurface1 = NULL ;
SDL_Surface* TextSurface2 = NULL ;
SDL_Surface* TextSurface3 = NULL ;
SDL_Surface* TextSurface4 = NULL ;
SDL_Surface* TextSurface5 = NULL ;
SDL_Surface* TextSurface6 = NULL ;
SDL_Surface* TextSurface7 = NULL ;
//文字
TTF_Font *Font = NULL ;
SDL_Texture* forFont = NULL;
SDL_Texture* forbackground = NULL;
SDL_Texture* forBee = NULL;
SDL_Texture* forbullet = NULL;
SDL_Texture* fortext = NULL ;
SDL_Texture* fortext1 = NULL ;
SDL_Texture* fortext2 = NULL ;
SDL_Texture* fortext3 = NULL ;
SDL_Texture* fortext4 = NULL ;
SDL_Texture* fortext5 = NULL ;
SDL_Texture* fortext6 = NULL ;
SDL_Texture* fortext7 = NULL ;
SDL_Texture* fordragon = NULL ;
SDL_Texture* forenemybullet = NULL ;//敌人子弹
//小蜜蜂
SDL_Rect Beesize ;
SDL_Rect Beepos ;
//子弹
SDL_Rect bulletsize ;
SDL_Rect bulletpos;
//记分版
SDL_Rect sco;
//敌人位置
SDL_Rect enemysize ;
SDL_Rect enemy ;
//d
//SDL_Rect buseize;
SDL_Rect pos_bulletEnemy;//敌人子弹出现的位置
SDL_Renderer* render = NULL ;
void init ( void )
{ //初始化。。。
if( SDL_Init( SDL_INIT_VIDEO ) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
TTF_Init() ;
//
{
//初始化 窗口
window = SDL_CreateWindow("little Bee",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL_WINDOW_SHOWN
);
//初始化敌人子弹位置。。
pos_bulletEnemy.x = -64 ;
pos_bulletEnemy.y = -64 ;
pos_bulletEnemy.w = 64;
pos_bulletEnemy.h = 64;
// 初始化一下 图形中蜜蜂的位置
Beesize.x = 64;
Beesize.y = 0;
Beesize.w = 64;
Beesize.h = 64;
//初始化 出现的位置
Beepos.x = 320 ;
Beepos.y = 640 ;
Beepos.w = 64 ;
Beepos.h = 64 ;
//子弹大小
bulletsize.x = 0;
bulletsize.y = 0;
bulletsize.w = 64;
bulletsize.h = 64;
//记分版在右上角 需要文字 score
sco.x = 576; // 下一个640
sco.y = 0;
sco.w = 128;
sco.h = 64;
//敌人位置
enemy.x = 192;
enemy.y = 192;
enemy.w = 64;
enemy.h = 64;
pos [ 0 ] = 192 ;
for (enemynumber = 1 ;enemynumber < 7 ; enemynumber++) //number is 7
{
pos [ enemynumber ] = pos [ (enemynumber-1) ] + 64 ;//初始化位置
}
//敌人大小
enemysize.x =64 ;
enemysize.y =0 ;
enemysize.w =64 ;
enemysize.h =64 ;
/*
buseize.x = 32;
buseize.y = 32;
buseize.w = 32;
buseize.h = 32;
*/
//初始化 渲染器
render = SDL_CreateRenderer( window , -1, SDL_RENDERER_ACCELERATED);
//surface 加载
background = IMG_Load("space.jpg"); // 必须从 surface上r提取到 texture ?
Bee = IMG_Load("bee.png"); //
bullet = IMG_Load("bullet.png");
dragon = IMG_Load("dragon.png");
enemybullet = IMG_Load("Sprite1.png");
//colorkey
// SDL_SetColorKey(<#SDL_Surface *surface#>, <#int flag#>, <#Uint32 key#>)
SDL_SetColorKey( bullet , SDL_TRUE , SDL_MapRGB(bullet -> format , 6, 25, 107) );
//纹理加载(背景 蜜蜂 子弹 龙 敌人子弹 )
forbackground = SDL_CreateTextureFromSurface( render , background);
forBee = SDL_CreateTextureFromSurface( render , Bee );
forbullet = SDL_CreateTextureFromSurface( render , bullet);
fordragon = SDL_CreateTextureFromSurface( render , dragon);
forenemybullet = SDL_CreateTextureFromSurface(render , enemybullet);
//字体加载
Font = TTF_OpenFont("lazy.ttf", 18);
SDL_Color textColor = {255 , 255 , 255};
TextSurface = TTF_RenderText_Solid( Font , word0, textColor);//
TextSurface1 = TTF_RenderText_Solid( Font , word1, textColor);
TextSurface2 = TTF_RenderText_Solid( Font , word2, textColor);
TextSurface3 = TTF_RenderText_Solid( Font , word3, textColor);
TextSurface4 = TTF_RenderText_Solid( Font , word4, textColor);
TextSurface5 = TTF_RenderText_Solid( Font , word5, textColor);
TextSurface6 = TTF_RenderText_Solid( Font , word6, textColor);
TextSurface6 = TTF_RenderText_Solid( Font , word7, textColor);
fortext = SDL_CreateTextureFromSurface (render,TextSurface);// 为什么每个字符被赋值过后就不能再动了?
fortext1 = SDL_CreateTextureFromSurface (render,TextSurface1);
fortext2 = SDL_CreateTextureFromSurface (render,TextSurface2);
fortext3 = SDL_CreateTextureFromSurface (render,TextSurface3);
fortext4 = SDL_CreateTextureFromSurface (render,TextSurface4);
fortext5 = SDL_CreateTextureFromSurface (render,TextSurface5);
fortext6 = SDL_CreateTextureFromSurface (render,TextSurface6);
fortext7 = SDL_CreateTextureFromSurface (render,TextSurface7);
// TTF_RenderText_Solid(<#TTF_Font *font#>, <#const char *text#>, <#SDL_Color fg#>) ;//这是个const MMP
//小蜜蜂初始化完成了。
// SDL_RenderCopy(render, forbackground, NULL , NULL );
// SDL_RenderCopy(render, forBee, &Beesize , &Beepos);
// SDL_RenderPresent( render );
}
}
void animation ( void )
{
if( counter == 1 )
{
Beesize.x = 64;
Beesize.y = 0;
Beesize.w = 64;
Beesize.h = 64;
}
if( counter == 2 )
{
Beesize.x = 128;
Beesize.y = 0;
Beesize.w = 64;
Beesize.h = 64;
}
if( counter == 3 )
{
Beesize.x = 0;
Beesize.y = 0;
Beesize.w = 64;
Beesize.h = 64;
}
counter++;
if( counter == 4 )
{
counter = 0;
}
}
void rendpart ( void )//这个函数 实现 动画效果 同时 应该在 更新完新位置之后调用
{
SDL_RenderClear(render);
SDL_RenderCopy(render, forbackground, NULL , NULL );
SDL_RenderCopy(render, forBee, &Beesize , &Beepos); //这个函数 要实现Bee 的动画效果 同时
//所有敌人渲染
motion();// 调整第一个位置
//new_pos( ); // 把每个新位置赋值
// 碰撞测试
for( int i=0 ;i < enemynumber; i++ )
{
enemy.x = pos[i];
SDL_RenderCopy(render , fordragon, &enemysize, &enemy);
}
// changescore( word );
SDL_RenderSetViewport( render , &sco ) ; //限制位置右上角
if ( fenshu == 0 )
{
SDL_RenderCopy(render, fortext , NULL , NULL );//渲染文字
}
if ( fenshu == 1 )
{
SDL_RenderCopy(render, fortext1 , NULL , NULL );
}
if ( fenshu == 2 )
{
SDL_RenderCopy(render, fortext2 , NULL , NULL );
}
if ( fenshu == 3 )
{
SDL_RenderCopy(render, fortext3 , NULL , NULL );
}
if ( fenshu == 4 )
{
SDL_RenderCopy(render, fortext4 , NULL , NULL );
}
if ( fenshu == 5 )
{
SDL_RenderCopy(render, fortext5 , NULL , NULL );
}
if ( fenshu == 6 )
{
SDL_RenderCopy(render, fortext6 , NULL , NULL );
}
SDL_RenderSetViewport( render , NULL ) ;
//子弹数量 也是靠 函数实现
//SDL_RenderCopy(render , fordragon, &enemysize, &enemy);//敌人渲染
}
void motion ( void )// 要不手动修改?
{
if( direct )
{
for ( int i = 0 ; i < 7 ; i++)
{
pos[i] = pos[i] + 64;
}
if ( pos[ (enemynumber-1) ] == 640 )
{
direct = false;
}
if (pos[ (enemynumber-1) ] < 0 )
{
direct = false;
}
}
else
{
for ( int i = 0 ; i < 7 ; i ++)
{
pos[i] = pos[i] - 64;
}
if ( pos[0] == 0 )//当第一个击中的时候 这个永远都不会为0了。。
{
direct = true ;
}
if ( pos[0] < 0 )
{
direct = true ;
}
}
}
//创建一个函数每次传入特殊位置
/*
if( direct )
{
pos[0] = pos[0] + 64 ;
if ( pos[ (enemynumber-1) ] == 640 )// 末尾上的 enemynumber is 7
{
direct = false ;
}
}
else
{
pos[0] = pos[0] - 64 ;
if ( pos[0] == 0 )
{
direct = true ;
}// motion 函数需要修改
}
*/
/*
void motion2 ( void )// 要不手动修改?
{
if( direct )
{
for ( int i = 0 ; i < 7 ; i++)
{
pos[i] = pos[i] + 64;
}
while( pos[ (enemynumber-1) ] == 640 )
{
direct = false;
}
}
else
{
for ( int i = 0 ; i < 7 ; i ++)
{
pos[i] = pos[i] - 64;
}
while ( pos[0] == 0 )
{
direct = true ;
}
}
}
*/
/*
void changescore ( char* sc)
{
}
if ( socre )
{
word[6] =word[6]+1 ;
TextSurface = TTF_RenderText_Solid( Font , word, textColor);//这个位置上还需要传递字符串
fortext = SDL_CreateTextureFromSurface (render,TextSurface);
printf("%s", word );
socre = false ;
}
//敌人射击 随机数生成在 所剩的 龙中。%a(这里面是个参数)。龙的 x 记录下来 子弹渲染一样
*/