C语言+EasyX 鼠标控制角色移动

昨天的角色移动是键盘控制角色,今天做了改进,用鼠标控制角色移动
#include
#include
#include
#include
int num = 0;
double x0 = 0;
double y2 = 450;
double x1;
double y3;
double term;
double mx;
double my;
IMAGE background, boy1, boy2;
MOUSEMSG m,m1;
void InitInstance()
{
	loadimage(&background, "background.jpg");//加载游戏背景
	loadimage(&boy1, "boy1.bmp");//背景图
	loadimage(&boy2, "boy2.bmp");//屏蔽图
}
void Interface()//初始化基本数据
{
	initgraph(1024, 768);//初始化画布大小
	putimage(0, 0, &background);
	putimage(double (x0), double (y2), 70, 124, &boy2, 70 * num, 124 * 2, SRCAND);//角色初始位置
	putimage(double (x0), double (y2), 70, 124, &boy1, 70 * num, 124 * 2, SRCPAINT);
}
//左
void Left()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
		//往左走
		while (1)
		{
			//switch (m1.uMsg)
			//{
			//case WM_LBUTTONDOWN:
			//	m.x = m1.x;//想改变mx,my的值,重新给角色一个坐标,达到中途改变路线的目的
			//	m.y = m1.y;
			//	Key_Down();
			//	break;
			//}
			if (x0 <= (mx - 35) )//当角色走到目的地时跳出死循环
			{
				break;
			}
			putimage(0, 0, &background);//贴背景
			putimage(x0, y2, 70, 124, &boy2, 70 * num, 124 * 1, SRCAND);//屏蔽图
			putimage(x0, y2, 70, 124, &boy1, 70 * num, 124 * 1, SRCPAINT);//背景图
																		  //当角色走完一套动作时,回到初始动作
			num++;
			if (num == 4)
				num = 0;
			Sleep(70);//调整角色跨步时间
					  //跨步距离
			x0 -= 7.5;
		}
		break;
	}
}
void Right()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
						//在鼠标左键按下位置记录坐标每项,mx,my
						//往右下角走
		while (1)
		{
			//switch (m1.uMsg)
			//{
			//	m.x = m1.x;
			//	m.y = m1.y;
			//case WM_LBUTTONDOWN:
			//	Key_Down();
			//	break;
			//}
			if (x0 >= (mx - 35) )
			{
				break;
			}
			putimage(0, 0, &background);
			putimage(x0, y2, 70, 124, &boy2, 70 * num, 124 * 2, SRCAND);
			putimage(x0, y2, 70, 124, &boy1, 70 * num, 124 * 2, SRCPAINT);
			num++;
			if (num == 4)
				num = 0;
			Sleep(70);
			x0 +=7.5;
		}
		break;
	}
}
void Up()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
						//在鼠标左键按下位置记录坐标每项,mx,my
						//往右下角走
		while (1)
		{
			/*switch (m1.uMsg)
			{
				m.x = m1.x;
				m.y = m1.y;
			case WM_LBUTTONDOWN:
				Key_Down();
				break;
			}*/
			if (  y2 <= (my - 110))
			{
				break;
			}
			putimage(0, 0, &background);
			putimage(x0, y2, 70, 124, &boy2, 70 * num, 124 * 3, SRCAND);
			putimage(x0, y2, 70, 124, &boy1, 70 * num, 124 * 3, SRCPAINT);
			num++;
			if (num == 4)
				num = 0;
			Sleep(70);
			y2 -= 7.5;
		}
		break;
	}
}
void Down()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
						//在鼠标左键按下位置记录坐标每项,mx,my
						//往左下角走
		while (1)
		{
		/*	switch (m1.uMsg)
			{
				m.x = m1.x;
				m.y = m1.y;
			case WM_LBUTTONDOWN:
				Key_Down();
				break;
			}*/
			if ( y2 >= (my - 110))//当角色走到目的地时跳出死循环
			{
				break;
			}
			putimage(0, 0, &background);//贴背景
			putimage(x0, y2, 70, 124, &boy2, 70 * num, 0, SRCAND);//屏蔽图
			putimage(x0, y2, 70, 124, &boy1, 70 * num,0, SRCPAINT);//背景图
																		  //当角色走完一套动作时,回到初始动作
			num++;
			if (num == 4)
				num = 0;
			Sleep(70);//调整角色跨步时间
					  //跨步距离
			y2 += 7.5;
		}
		break;
	}
}
void LeftDown()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
						//在鼠标左键按下位置记录坐标每项,mx,my
		//往左下角走
			while (1)
			{
			/*	switch (m1.uMsg)
				{
					m.x = m1.x;
					m.y = m1.y;
				case WM_LBUTTONDOWN:
					Key_Down();
					break;
				}*/
				if (x0 <= (mx - 35)&&y2>=(my-110))//当角色走到目的地时跳出死循环
				{
					break;
				}
				//用勾股定理算出角色所需走的x,y
				term = 1 + ((y2 - (my - 110)) / (x0 - (mx - 35)))*((y2 - (my - 110)) / (x0 - (mx - 35)));
				x1 = 1.5*fabs(sqrt(25 / term));//角色到达目的地的x轴距离
				y3 = 1.5*fabs((y2 - (my - 110)) / (x0 - (mx - 35)))*x1;//角色到达目的地的Y轴距离
				putimage(0, 0, &background);//贴背景
				putimage(x0, y2, 70, 124, &boy2, 70 * num, 124 * 4, SRCAND);//屏蔽图
				putimage(x0, y2, 70, 124, &boy1, 70 * num, 124 * 4, SRCPAINT);//背景图
				num++;//当角色走完一套动作时,回到初始动作
				if (num == 4)
					num = 0;
				Sleep(70);//调整角色跨步时间
						  //跨步距离
				x0 -= x1;
				y2 += y3;
			}
		break;
	}
}
void RightUp()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
						//在鼠标左键按下位置记录坐标每项,mx,my
		//往右上角走
			while (1)
			{
	/*			switch (m1.uMsg)
				{
					m.x = m1.x;
					m.y = m1.y;
				case WM_LBUTTONDOWN:
					Key_Down();
					break;
				}*/
				if (x0 >= (mx - 35) && y2 <= (my - 110))
				{
					break;
				}
				term = 1 + ((y2 - (my - 110)) / (x0 - (mx - 35)))*((y2 - (my - 110)) / (x0 - (mx - 35)));
				x1 = 1.5*fabs(sqrt(25 / term));
				y3 = 1.5*fabs((y2 - (my - 110)) / (x0 - (mx - 35)))*x1;
				putimage(0, 0, &background);
				putimage(x0, y2, 70, 124, &boy2, 70 * num, 124 * 6, SRCAND);
				putimage(x0, y2, 70, 124, &boy1, 70 * num, 124 * 6, SRCPAINT);
				num++;
				if (num == 4)
					num = 0;
				Sleep(70);
				x0 += x1;
				y2 -= y3;
			}
		break;
	}
}
void RightDown()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
						//在鼠标左键按下位置记录坐标每项,mx,my
		//往右下角走
			while (1)
			{
	/*			switch (m1.uMsg)
				{
					m.x = m1.x;
					m.y = m1.y;
				case WM_LBUTTONDOWN:
					Key_Down();
					break;
				}*/
				if (x0 >= (mx - 35) && y2 >= (my - 110))
				{
					break;
				}
				term = 1 + ((y2 - (my - 110)) / (x0 - (mx - 35)))*((y2 - (my - 110)) / (x0 - (mx - 35)));
				x1 = 1.5*fabs(sqrt(25 / term));
				y3 = 1.5*fabs((y2 - (my - 110)) / (x0 - (mx - 35)))*x1;
				putimage(0, 0, &background);
				putimage(x0, y2, 70, 124, &boy2, 70 * num, 124 * 5, SRCAND);
				putimage(x0, y2, 70, 124, &boy1, 70 * num, 124 * 5, SRCPAINT);
				num++;
				if (num == 4)
					num = 0;
				Sleep(70);
				x0 += x1;
				y2 += y3;
			}
		break;
	}
}
void LeftUp()
{
	void Key_Down();
	switch (m.uMsg)
	{
	case WM_LBUTTONDOWN://判断鼠标左键按下
		//在鼠标左键按下位置记录坐标每项,mx,my
		//往左上角走
			while (1)
			{
				/*switch(m1.uMsg)
				{
					m.x = m1.x;
					m.y = m1.y;
					case WM_LBUTTONDOWN:
						Key_Down();
						break;
				}*/
				if (x0 <= (mx-35) && y2 <= (my-110))//当角色走到目的地时跳出死循环
				{
					break;
				}
				//用勾股定理算出角色所需走的x,y
				term = 1 + ((y2 - (my-110)) / (x0 - (mx-35)))*((y2 - (my-110)) / (x0 - (mx-35)));
				x1 = 1.5*fabs(sqrt(25 / term));//角色到达目的地的x轴距离
				y3 = 1.5*fabs((y2 - (my-110)) / (x0 - (mx-35)))*x1;//角色到达目的地的Y轴距离
				putimage(0, 0, &background);//贴背景
				putimage(x0, y2, 70, 124, &boy2, 70 * num, 124 * 7, SRCAND);//屏蔽图
				putimage(x0,y2, 70, 124, &boy1, 70 * num, 124 * 7, SRCPAINT);//背景图
				//当角色走完一套动作时,回到初始动作
				num++;
				if (num == 4)
					num = 0;
				Sleep(70);//调整角色跨步时间
				//跨步距离
				x0 -= x1;
				y2 -= y3;
			}
		break;
	}
}
void Key_Down()
{
	while (1)
	{
		m = GetMouseMsg();
		//m1= GetMouseMsg();
		//在鼠标左键按下位置记录坐标每项,mx,my
		mx = m.x;
		my = m.y;
		//左
		if (mx(y2 + 100) && my < (y2 + 112))
		{
			Left();
		}
		//右
		else if (mx > x0 && my > (y2 + 100) && my < (y2 + 112))
		{
			Right();
		}
		//上
		else if (mx > (x0 + 20) && mx < (x0 + 50) && my < (y2 + 20))
		{
			Up();
		}
		//下
		else if (mx > (x0 + 20) && mx<(x0 + 50) && my>(y2 + 110))
		{
			Down();
		}
		//左上
		else if (mx < (x0 + 35) && my < (y2 + 100))//判断鼠标左键按下位置是否为角色左上位置
		{
			LeftUp();
		}
		//左下
		else if (mx < (x0 + 35) && my >(y2 + 112))//判断鼠标左键按下位置是否为角色左下位置
		{
			LeftDown();
		}
		//右下
		else if (mx > (x0 + 38) && my > (y2 + 120))
		{
			RightDown();
		}
		//右上
		else if (x0<(mx - 38) && y2>(my - 105))
		{
			RightUp();
		}
	}
}
int main()
{
	InitInstance();
	Interface();
	Key_Down();
	getchar();
	closegraph();
	return 0;
}


你可能感兴趣的:(C语言+EasyX 鼠标控制角色移动)