当前位置 博文首页 > 吴斌_qM的博客:射击小游戏

    吴斌_qM的博客:射击小游戏

    作者:[db:作者] 时间:2021-07-13 21:44

    利用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();//发射子弹
    		}
    	}
    }
    
    
    下一篇:没有了