简易游戏制作--飞机大战(c语言)

#include<stdio.h>
#include <graphics.h>
#include<conio.h>
#include<time.h>

IMAGE stat, bk, boss, img_gamer[2], img_bull[2], img_enemy[2];
int i = 0, n = 0, kill = 0;
double WIDTH = 637.0, HEIGHT = 796.0;
#define BULL_NUM  3
#define ENEMY_NUM 3

struct BULL //创建子弹属性
{
	double x, y;
	bool live;
}bull[BULL_NUM], bossbull[BULL_NUM];
struct BIG //boss属性
{
	double x, y;
	bool live;
	double height;
	double width;
	int hp;
} bs;
struct SMALL //小兵属性
{
	double x, y;
	bool live;
	int height;
	int width;
	int hp;
}enemy[ENEMY_NUM];
struct GAMER //玩家属性
{
	double x, y;
	bool live;
	int height;
	int width;
	int hp;
}p1;
//定义移动
void right(double speed)
{
	bs.x += speed;
}
void left(double speed)
{
	bs.x -= speed;
}
void(*move)(double x);
//加载图片
void loadImage()
{
	loadimage(&bk, "./images/background.jpg");
	loadimage(&img_gamer[0], "./images/gamer.jpg");
	loadimage(&img_gamer[1], "./images/gamer_.jpg");
	loadimage(&img_bull[0], "./images/bull.jpg");
	loadimage(&img_bull[1], "./images/bull_.jpg");
	loadimage(&boss, "./images/BOSS.jpg");
	loadimage(&img_enemy[0], "./images/d1.jpg");
	loadimage(&img_enemy[1], "./images/d2.jpg");
}
//初始化游戏
void gameinit()
{
	p1.x = 300.0;
	p1.y = 600.0;
	p1.width = 59.0;
	p1.height = 76.0;
	p1.live = true;
	p1.hp = 3;
	bs.x = 300.0;
	bs.y = 0.0;
	bs.width = 80.0;
	bs.height = 120.0;
	bs.live = true;
	bs.hp = 3;
	for (i = 0; i < ENEMY_NUM; i++)
	{
		bull[i].live = false;
		bull[i].x = 0;
		bull[i].y = 0;
	}
	for (i = 0; i < ENEMY_NUM; i++)
	{
		bossbull[i].live = false;
		bossbull[i].x = 0;
		bossbull[i].y = 0;
	}
	for (i = 0; i < ENEMY_NUM; i++)
	{
		enemy[i].live = false;
		enemy[i].x = 0;
		enemy[i].y = 0;
	}
}
//加载图像
void gamedraw()
{
	putimage(0.0, 0.0, &bk);
	//printf("1\n");
	//system("pause");

	if (p1.live)
	{
		putimage(p1.x, p1.y, &img_gamer[1], NOTSRCERASE);
		//printf("2\n");
		//system("pause");

		putimage(p1.x, p1.y, &img_gamer[0], SRCINVERT);
		//printf("3\n");
		//system("pause");
	}

	if (bs.live)
	{
		putimage(bs.x, bs.y, &boss);
	}
	//printf("4\n");
	//system("pause");

	for (i = 0; i < 3; i++)
	{
		if (bull[i].live)
		{
			putimage(bull[i].x, bull[i].y, &img_bull[1], NOTSRCERASE);
			putimage(bull[i].x, bull[i].y, &img_bull[0], SRCINVERT);
		}
	}
	for (i = 0; i < 3; i++)
	{
		if (bossbull[i].live)
		{
			putimage(bossbull[i].x, bossbull[i].y, &img_bull[1], NOTSRCERASE);
			putimage(bossbull[i].x, bossbull[i].y, &img_bull[0], SRCINVERT);
		}
	}
	//printf("5\n");
	//system("pause");

	for (i = 0; i < 3; i++)
	{
		if (enemy[i].live)
		{
			putimage(enemy[i].x, enemy[i].y, &img_enemy[1], NOTSRCERASE);
			putimage(enemy[i].x, enemy[i].y, &img_enemy[0], SRCINVERT);
		}
	}
	//printf("6\n");

	//system("pause");
}
//创造子弹
void creatbull()
{
	if (p1.live)
	{
		for (i = 0; i < BULL_NUM; i++)
		{
			if (!bull[i].live)
			{
				bull[i].x = p1.x + 20.0;
				bull[i].y = p1.y;
				bull[i].live = true;
				break;
			}

		}
	}
}
void creatbossbull()
{
	if (bs.live)
	{
		for (i = 0; i < BULL_NUM; i++)
		{
			if (!bossbull[i].live)
			{
				bossbull[i].x = bs.x + 40.0;
				bossbull[i].y = bs.y + 60.0;
				bossbull[i].live = true;
				break;
			}

		}
	}
}
//子弹移动
void bullmove()
{

	for (i = 0; i < BULL_NUM; i++)
	{
		if (bull[i].live)
		{
			bull[i].y--;
		}
		if (bull[i].y < 0)
		{
			bull[i].live = false;
		}
	}

}
//boss移动
void bossbullmove()
{
	for (i = 0; i < BULL_NUM; i++)
	{
		if (bossbull[i].live)
		{
			bossbull[i].y += 0.1;
		}
		if (bossbull[i].y > 796)
		{
			bossbull[i].live = false;
		}
	}

}
//玩家移动j即攻击
void gamermove(double speed)
{
//wasd控制
#if(0)
	if (_kbhit())
	{
		char key = _getch();
		switch (key)
		{
		case'w':
		case'W':
			p1.y -= speed;
			break;
		case's':
		case'S':
			p1.y += speed;
			break;
		case'd':
		case'D':
			p1.x += speed;
			break;
		case'a':
		case'A':
			p1.x -= speed;
			break;
		}
	}
#elif (1)
	{
		//上下左右控制
		if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W'))
		{
			if (p1.y >= 0.0)
				p1.y -= speed;
		}
		if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S'))
		{
			if (p1.y <= 720.0)
				p1.y += speed;
		}
		if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D'))
		{
			if (p1.x <= 578.0)
				p1.x += speed;
		}
		if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A'))
		{
			if (p1.x >= 0.0)
				p1.x -= speed;
		}
	}
#endif
	//对于子弹发射的间隔控制及响应时间
	static DWORD x1 = 0, x2 = 0;
	if (GetAsyncKeyState(VK_SPACE) && x2 - x1 > 1000)
	{
		creatbull();
		x1 = x2;
	}
	x2 = clock();
}
//创造敌机
void createnemy()
{
	for (i = 0; i < ENEMY_NUM; i++)
	{
		if (!enemy[i].live)
		{
			enemy[i].width = 50.0;
			enemy[i].height = 120.0;
			enemy[i].hp = 1;
			enemy[i].x = (float)(rand() % 600);
			enemy[i].y = -100.0;
			enemy[i].live = true;
			break;
		}
	}
}
//敌人移动
void enemymove(double speed)
{
	for (i = 0; i < ENEMY_NUM; i++)
	{
		if (enemy[i].live)
		{
			enemy[i].y += speed;
		}
		if (enemy[i].y > 1500)
		{
			enemy[i].live = false;
		}
	}
}
//boss移动
void bossmove(double speed)
{
	if (bs.x >= 557.0)
	{
		move = left;
	}
	if (bs.x <= 0.0)
	{
		move = right;
	}
	move(speed);

}
//计时器
int timer()
{
	static DWORD t1 = 0, t2 = 0;
	if ((t2 - t1) > 3000)
	{
		t1 = t2;
		return 1;
	}
	t2 = clock();
	return 0;
}
//玩家攻击判定(小兵)
void play()

{
	for (i = 0; i < ENEMY_NUM; i++)
	{
		if (enemy[i].live)
		{
			for (int n = 0; n < BULL_NUM; n++)
			{
				if (bull[n].live)
				{
					if ((bull[n].x + 10.0) > enemy[i].x && (bull[n].x + 10.0) < (enemy[i].x + enemy[i].width) && bull[n].y > enemy[i].y && bull[n].y < (enemy[i].y + enemy[i].height))
					{
						bull[n].live = false;
						enemy[i].hp--;
					}
					if (enemy[i].hp <= 0)
					{
						enemy[i].live = false;
						kill++;
						printf("击杀数为 % d\n", kill);
					}
				}
			}

		}
	}
}
//boss受伤判定
void pl()
{
	if (bs.live)
	{
		for (n = 0; n < BULL_NUM; n++)
		{
			if (bull[n].live)
			{
				if (bull[n].x > bs.x && bull[n].x < (bs.x + bs.width) && bull[n].y>bs.y && bull[n].y < (bs.y + bs.height))
				{
					bs.hp -= 1;
					bull[n].live = false;
					printf("boss血量为%d\n", bs.hp);
				}
				if (bs.hp <= 0)
				{
					bs.live = false;
					printf("boss已被击杀\n");
				}
			}
		}
	}


}
//玩家受伤判定
void die()
{
	if (p1.live)
	{
		for (i = 0; i < BULL_NUM; i++)
		{
			if (bossbull[i].live)
			{
				if ((bossbull[i].x + 10.0) > p1.x && bossbull[i].x<(p1.x + p1.width) && (bossbull[i].y + 21.0)>p1.y && (bossbull[i].y + 21.0) < (p1.y + p1.height))
				{
					p1.hp -= 1;
					bossbull[i].live = false;
					printf("你的生命为%d\n", p1.hp);
				}
				if (p1.hp <= 0)
				{
					p1.live = false;
					printf("你死了");
					exit(0);
				}
			}
		}
	}
}

int main()
{
	loadImage();
	initgraph(WIDTH, HEIGHT, SHOWCONSOLE);//使画面更加流畅
	gameinit();
	BeginBatchDraw();
	move = right;
	while (1)
	{
		gamedraw();
		FlushBatchDraw();
		gamermove(0.3);
		//对于敌机出现和boss攻击计时
		if (timer())
		{
			createnemy();
			creatbossbull();
		}
		bossmove(0.1);
		bullmove();
		bossbullmove();
		enemymove(0.1);
		play();
		pl();
		die();
	}
	//结束绘制
	EndBatchDraw();
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值