当前位置:   article > 正文

C语言课程设计——飞机大战(无注释)_程序设计飞机大战

程序设计飞机大战

#include"acllib.h"
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
//定义变量
const int winwidth = 640, winheight = 500;
ACL_Image airplane, airenemy,airzidan ;//载入图片
ACL_Sound snd1,snd2;//载入音频
const char* a = "airplane.jpeg";//选择图片
const char* b = "enemy.jpg";//选择图片
const char* c = "a.bmp";//选择图片
const int max = 999999;
const int max1 = 999999;
int num = 0;
int num1 = 0;
int moveid = 0, movetimerinterval = 40;//定时器
int createid = 1, createtimerinterval = 1000;//定时器
int zidanid1 = 2, zidantimerinterval1 = 20;//定时器
int zidanid2 = 3, zidantimerinterval2 = 700;//定时器
int hp = 5;//初始化血量
int score = 0;//初始化分数
int difficulty = 1;
struct sprite
{
	int width, height;
	int x, y;
	int distx, disty;
};
struct sprite plane, * enemy[max] = { NULL }, * zidan1[max1] = { NULL }, * zidan2[max1] = { NULL };//参数数据结构
typedef struct rect
{
	int x, y;
	int w, h;
}rect;
//制定函数
void paint();
void keyEvent(int key, int e);
void timeEvent(int id);
void fault();
int collision(rect r1, rect r2);
int Setup()
{
	srand((unsigned)time(NULL));
	initWindow("airplane", DEFAULT, DEFAULT, winwidth, winheight);
	loadImage(a, &airplane);//载入友机图片
	loadImage(b, &airenemy);//载入敌机图片
	loadImage(c, &airzidan);//载入子弹图片
	loadSound("bumb1.wav", &snd1);//载入爆炸声效
	loadSound("bumb2.wav", &snd2);//载入爆炸声效
	//定义我方飞机的参数
	plane.width = 100;
	plane.height = 100;
	plane.distx= 4;
	plane.x = 250;
	plane.y = 400;
	//定义敌方飞机的参数
	if (enemy==NULL)
	{
			enemy[num]->width = 50;
			enemy[num]->height = 50;
			switch (difficulty)
			{
			case 1:
				enemy[num]->disty = 2;
				break;
			case 2:
				enemy[num]->disty = 3;
				break;
			case 3:
				enemy[num]->disty = 4;
				break;
			case 4:
				enemy[num]->disty = 5;
				break;
			case 5:
				enemy[num]->disty = 6;
				break;
			default:
				break;
			}
			enemy[num]->x = rand() % (winwidth );
			enemy[num]->y = 10;
		
	}
	//定义左子弹的参数
	if (zidan1 == NULL)
	{
		zidan1[num1]->width = 10;
		zidan1[num1]->height = 10;
		zidan1[num1]->disty = 2;
		zidan1[num1]->x = plane.x+12.5;
		zidan1[num1]->y = 400;
	}
	//定义右子弹的参数
	if (zidan2 == NULL)
	{
		zidan2[num1]->width = 10;
		zidan2[num1]->height = 10;
		zidan2[num1]->disty = 2;
		zidan2[num1]->x = plane.x + 87.5;
		zidan2[num1]->y = 400;
	}
	registerTimerEvent(timeEvent);
	//定义定时器
	startTimer(moveid, movetimerinterval);
	startTimer(createid, createtimerinterval);
	startTimer(zidanid1, zidantimerinterval1);
	startTimer(zidanid2, zidantimerinterval2);
	registerKeyboardEvent(keyEvent);
	paint();
	return 0;
}
//绘制图形
void paint()
{
	if (hp <= 0)
	{
		fault();
	}
	else
	{
		beginPaint();
		clearDevice();
		putImageScale(&airplane, plane.x, plane.y, plane.width, plane.height);
		for (int i = 0; i < num - 1; i++)
		{
			if (enemy[i])
				putImageScale(&airenemy, enemy[i]->x, enemy[i]->y, enemy[i]->width, enemy[i]->height);

		}
		for (int j = 0; j < num1 - 1; j++)
		{
			if (zidan1[j])
				putImageScale(&airzidan, zidan1[j]->x, zidan1[j]->y, zidan1[j]->width, zidan1[j]->height);

		}
		for (int m = 0; m < num1 - 1; m++)
		{
			if (zidan2[m])
				putImageScale(&airzidan, zidan2[m]->x, zidan2[m]->y, zidan2[m]->width, zidan2[m]->height);

		}
		char str1[50];
		sprintf_s(str1, "HP:%d", hp);
		setTextSize(30);
		paintText(10, 10, str1);
		char str2[50];
		sprintf_s(str2, "score:%d", score);
		setTextSize(30);
		paintText(500, 10, str2);
		char str3[50];
		sprintf_s(str3, "difficulty:%d", difficulty);
		setTextSize(30);
		paintText(200, 10, str3);
	}
	endPaint();		
}
//键盘控制
void keyEvent(int key, int e)
{
	if (e != KEY_DOWN)
		return;
	switch (key)
	{
	case VK_LEFT:
		plane.x -= plane.distx;
		if (plane.x <= 0)
			plane.x = 0;
		break;
	case VK_RIGHT:
		plane.x += plane.distx;
		if (plane.x >= winwidth - plane.width)
			plane.x = winwidth - plane.width;
		break;
	default:
		break;
	}
	paint();
}
//自动移动
void timeEvent(int id)
{
	int i,j;
	if (id == 0)
	{
		for (i = 0; i < num; i++)
		{
			if (enemy[i])
			{
				enemy[i]->y += enemy[i]->disty;
				if (enemy[i]->y > plane.y - enemy[i]->height )
				{
					playSound(snd1, 0);
					hp--;
					delete(enemy[i]);
					enemy[i] = NULL;
					if (hp == 0)
						fault();
				}
			}
		}
	}
	if (id == 1)
	{
		if (num < max)
		{
			num++;
			if (enemy[num] == NULL)
			{
				enemy[num] = (struct sprite*)malloc(sizeof(struct sprite));
					enemy[num]->width = 50;
					enemy[num]->height = 50;
				switch (difficulty)
				{
				case 1:
					enemy[num]->disty = 2;
					break;
				case 2:
					enemy[num]->disty = 3;
					break;
				case 3:
					enemy[num]->disty = 4;
					break;
				case 4:
					enemy[num]->disty = 5;
					break;
				case 5:
					enemy[num]->disty = 6;
					break;
				default:
					break;
				}
				enemy[num]->y = 10;
				enemy[num]->x = rand() % (winheight);
			}
			if (enemy[num]->y > plane.y - enemy[num]->height)
			{
				playSound(snd1, 0);
				hp--;
				delete(enemy[num]);
				enemy[num] = NULL;
				if (hp == 0)
				fault();
			}
		}
	}
	if (id == 2)
	{
		for (j = 0; j < num1; j++)
		{
			if (zidan1[j])
			{
				zidan1[j]->y -= zidan1[j]->disty;
			}
		}
	}
	if (id == 3)
	{
		if (num1 < max1)
		{
			num1++;
			if (zidan1[num1] == NULL)
			{
				zidan1[num1] = (struct sprite*)malloc(sizeof(struct sprite));
				zidan1[num1]->width = 10;
				zidan1[num1]->height = 10;
				zidan1[num1]->disty = 2;
				zidan1[num1]->y = 400;
				zidan1[num1]->x = plane.x + 87.5;
			}
		}
	}
		if (id == 2)
		{
			for (j = 0; j < num1; j++)
			{
				if (zidan2[j])
				{
					zidan2[j]->y -= zidan2[j]->disty;
				}
			}
		}
		if (id == 3)
		{
			if (num1 < max1)
			{
				num1++;
				if (zidan2[num1] == NULL)
				{
					zidan2[num1] = (struct sprite*)malloc(sizeof(struct sprite));
					zidan2[num1]->width = 10;
					zidan2[num1]->height = 10;
					zidan2[num1]->disty = 2;
					zidan2[num1]->y = 400;
					zidan2[num1]->x = plane.x + 12.5;
				}
			}
		}

		rect r1 = { 0 }, r2 = { 0 }, r3 = { 0 };
	for (int i = 0; i < num1; i++)
	{
		for (int j = 0; j < num; j++)
		{
			if (enemy[j])
			{
				r1.x = enemy[j]->x;
				r1.y = enemy[j]->y;
				r1.w = enemy[j]->width;
				r1.h = enemy[j]->height;
			}
		
			if (zidan1[i])
			{
				r2.x = zidan1[i]->x;
				r2.y = zidan1[i]->y;
				r2.w = zidan1[i]->width;
				r2.h = zidan1[i]->height;
				int c = collision(r1, r2);
				if (c)
				{
					score++;
					delete(zidan1[i]);
					zidan1[i] = NULL;
					delete(enemy[j]);
					enemy[j] = NULL;
					playSound(snd2, 0);
					if (score % 50 == 0)
						difficulty++;
				}
			}
		}
	}
	for (int i = 0; i < num1; i++)
	{
		for (int j = 0; j < num; j++)
		{
			if (enemy[j])
			{
				r1.x = enemy[j]->x;
				r1.y = enemy[j]->y;
				r1.w = enemy[j]->width;
				r1.h = enemy[j]->height;
			}
		
		if (zidan2[i])
		{
			r3.x = zidan2[i]->x;
			r3.y = zidan2[i]->y;
			r3.w = zidan2[i]->width;
			r3.h = zidan2[i]->height;
			int d = collision(r1, r3);
			if (d)
			{
				score++;
				delete(zidan2[i]);
				zidan2[i] = NULL;
				delete(enemy[j]);
				enemy[j] = NULL;
				playSound(snd2, 0);
				if (score % 50 == 0)
					difficulty++;
			}
		}
		}
	}
	paint();
}
//失败界面
void fault()
{
	beginPaint();
	clearDevice();
	char str[50];
	setTextColor(RED);
	sprintf_s(str, "Game Over");
	setTextSize(100);
	paintText(90, 200, str);
	stopSound(snd1);
	stopSound(snd2);
	endPaint();
}
//碰撞检测
int collision(rect r1, rect r2)
{
	int c = 1;
	if (r1.x<r2.x && r1.x + r1.w>r2.x)
	{
		if (r1.y > r2.y && r2.y + r2.h > r1.y)
			return c;
		if (r1.y < r2.y && r1.y + r1.h > r2.y)
			return c;
	}
	else
	{
		if (r1.x > r2.x && r2.x + r2.w > r1.x)
		{
			if (r1.y > r2.y && r2.y + r2.h > r1.y)
				return c;
			if (r1.y < r2.y && r1.y + r1.h > r2.y)
				return c;
		}
	}
	c = 0;
	return 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
  • 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

  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/358901
推荐阅读
相关标签
  

闽ICP备14008679号