• 周六. 4 月 25th, 2026

物嫩软件资讯网

软件资讯来物嫩

射击小游戏

admin@wunen

5 月 14, 2025



利用vs 2019和easyx图形库完成射击小游戏



需要的工具:
  1. Win 10
  2. VS 2019
  3. EasyX


main.cpp
#include<stdio.h>
#include<math.h>
#include<graphics.h>//easyx
#define WIDTH 960
#define HEIGHT 640
#define BULL_NUM 20 //子弹数量
struct Battery//炮台结构
{
	int x;
	int y;
	int endx;
	int endy;
	int len;
	int speed;
	double radian;//弧度
}bat;
struct Bullet //子弹结构
{
	int x;
	int y;
	int vx;		//速度分量
	int vy;
	int r;		//子弹半径
	bool falg;	//子弹是否存在
	DWORD color;//子弹的颜色
}shot[BULL_NUM];
struct Balloon//气球结构
{
	int x;
	int y;
	bool flag;//气球是否存在
	DWORD color;
}ball[BULL_NUM];
void GameInit()
{
	//创建一个窗口
	initgraph(WIDTH, HEIGHT, SHOWCONSOLE);
	//设置随机数种子
	srand(GetTickCount());

	bat.x = WIDTH / 2;
	bat.y = HEIGHT - 10;
	bat.endx = bat.x;
	bat.endy = bat.y - 70;
	bat.len = 70;
	bat.radian = 0;
	bat.speed = 5;;

	//初始化子弹
	for (int i = 0; i < BULL_NUM; i++)
	{
		shot[i].falg = false;//不存在
		shot[i].r = 5;
	}
	//初始化气球
	for (int i = 0; i < BULL_NUM; i++)
	{
		ball[i].x = rand() % (WIDTH - 30);
		ball[i].y = rand() % (HEIGHT - 60);
		ball[i].flag = true;
		ball[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
	}

}
void GameDraw()
{
	BeginBatchDraw();
	setbkcolor(RGB(114, 191, 207));
	cleardevice();
	for (int i = 0; i < BULL_NUM; i++)
	{
		if (ball[i].flag)
		{
			//绘制气球
			setfillcolor(ball[i].color);
			solidellipse(ball[i].x, ball[i].y, ball[i].x + 30, ball[i].y + 50);
			setlinestyle(PS_SOLID, 1);
			setlinecolor(WHITE);
			//画圆弧
			arc(ball[i].x + 5, ball[i].y + 5, ball[i].x + 25, ball[i].y + 45, 0, 1.4);
			//画尾巴
			arc(ball[i].x, ball[i].y + 50, ball[i].x + 20, ball[i].y + 80, 0, 1.4);
		}
	}
	//画炮台
	setlinestyle(PS_SOLID, 2);
	circle(bat.x, bat.y, 60);
	setfillcolor(BLACK);
	solidcircle(bat.x, bat.y, 5);
	//炮管
	setlinestyle(PS_SOLID, 5);
	setlinecolor(BLACK);
	line(bat.x, bat.y, bat.endx, bat.endy);

	//绘制子弹
	for (int i = 0; i < BULL_NUM; i++)
	{
		if (shot[i].falg)
		{
			setfillcolor(shot[i].color);
			solidcircle(shot[i].x, shot[i].y, shot[i].r);
		}
	}

	EndBatchDraw();

}
//产生子弹
void CreatBullet()
{
	for (int i = 0; i < BULL_NUM; i++)
	{
		if (!shot[i].falg)
		{
			shot[i].falg = true;
			shot[i].x = bat.endx;
			shot[i].y = bat.endy;
			shot[i].vx = bat.speed * cos(bat.radian);
			shot[i].vy = bat.speed * sin(bat.radian);
			shot[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
			break;
		}
	}
}
//移动子弹
void BulletMove()
{
	for (int i = 0; i < BULL_NUM; i++)
	{
		if (shot[i].falg)
		{
			shot[i].x += shot[i].vx;
			shot[i].y -= shot[i].vy;
			//判断是否消失,超出边界
			if (shot[i].x<0 || shot[i].x>WIDTH || shot[i].y<0 || shot[i].y>HEIGHT)
			{
				shot[i].falg = false;
			}
		}
	}
}
void MouseEvent()
{
	if (MouseHit())
	{
		MOUSEMSG msg = GetMouseMsg();
		//printf("鼠标坐标为%d,%d\n", msg.x, msg.y);
		bat.radian = atan2((double)bat.y - msg.y, msg.x - bat.x);
		bat.endx = bat.x + cos(bat.radian) * bat.len;
		bat.endy = bat.y - sin(bat.radian) * bat.len;
		if (msg.uMsg == WM_LBUTTONDOWN)//鼠标左键点击
		{
			CreatBullet();//发射子弹
		}
	}
}
//产生一个气球
void CreatBall()
{
	for (int i = 0; i < BULL_NUM; i++)
	{
		if (!ball[i].flag)
		{
			ball[i].x = rand() % WIDTH;
			ball[i].y = (rand() % 200) + HEIGHT;
			ball[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
			ball[i].flag = true;
			break;
		}
	}
}
//气球移动
void BalloonMove(int speed)
{
	for (int i = 0; i < BULL_NUM; i++)
	{
		if (ball[i].flag)
		{
			if (rand() % 2 == 0)
			{
				ball[i].x += 1;
			}
			else
			{
				ball[i].x -= 1;
			}
			ball[i].y -= speed;
			if (ball[i].x<0 || ball[i].x>WIDTH || ball[i].y + 60 < 0 || ball[i].y > HEIGHT)
			{
				ball[i].flag = false;
				CreatBall();
			}
		}
	}
}
//打气球
void PlayBalloon()
{
	for (int i = 0; i < BULL_NUM; i++)//遍历气球
	{
		if (!ball[i].flag)
			continue;
		for (int j = 0; j < BULL_NUM; j++)
		{
			if (!shot[j].falg)
				continue;
			//开始判断
			if (shot[j].x > ball[i].x && shot[j].x<ball[i].x + 30 &&
				shot[j].y>ball[i].y && shot[j].y < ball[i].y + 60)
			{
				shot[j].falg = false;
				ball[i].flag = false;
				CreatBall();
			}
		}

	}
}
int main()
{
	GameInit();
	while (1)
	{
		GameDraw();
		MouseEvent();
		BulletMove();
		BalloonMove(1);
		PlayBalloon();
	}

	getchar();//防止闪退
	return 0;
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注