easyX实现鼠标人物移动(可视化编程)

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<mmsystem.h>
#pragma comment(lib,"WINMM.lib")


struct
{
	int x, y;//人物坐标
	int _x, _y;//目的地坐标
	int dir;//人物移动方向
	/*

	0  下  ,1  左  2,右  3,下
	4  左下  5 右下 6,左上  7  右上 
	*/
	int num;//人物动作 
}person = {0,0,250,250,2,1};
IMAGE people[3];//图片  背景图  人物图  掩码图




void init()
{
	initgraph(640, 480);
	mciSendString(L"open 123.mp3 alias bgm", 0, 0, 0);
	//播放mp3格式的音乐
	mciSendString(L"play bgm", 0, 0, 0);

	IMAGE img;
	loadimage(&people[0], L"背景.jpg", 640, 480);
	loadimage(&people[1], L"人物图.bmp");//不加后两个数据就是,以图片像素加载
	loadimage(&people[2], L"掩码图.bmp");

}

void draw()
{
	BeginBatchDraw();
	putimage(0, 0, &people[0]);

	//贴掩码图
	putimage(person.x, person.y, 70, 124, &people[2], 70 * person.num, 124 * person.dir, SRCAND);
	putimage(person.x, person.y, 70, 124, &people[1], 70 * person.num, 124 * person.dir,SRCPAINT);
	EndBatchDraw();
	if (person.num == 3)
	{
		person.num = 0;
	}
	else
	{
		person.num++;
	}

	Sleep(10);

}

/*

0  下  ,1  左  2,右  3,上
4  左下  5 右下 6,左上  7  右上
*/

void walk()
{
	if (person.x > person._x&&person.y > person._y) { person.x -= 1, person.y -= 1,person.dir=6; return; }//左上方
	if (person.x < person._x&&person.y > person._y) { person.x += 1, person.y -= 1, person.dir = 7; return; }//右上方
	if (person.x > person._x&&person.y < person._y) { person.x -= 1, person.y += 1, person.dir = 4; return; }//左下方
	if (person.x < person._x&&person.y < person._y) { person.x += 1, person.y += 1, person.dir = 5; return; }//右下方
	if (person.x > person._x) { person.x -= 1, person.dir = 1; return; }//左
	if (person.x < person._x) { person.x += 1, person.dir = 2; return; }//右
	if (person.y > person._y) {  person.y -= 1, person.dir = 3; return; }//上
	if (person.y < person._y) { person.y += 1, person.dir = 0; return; }//下
}

void Changedir()
{
	//得到鼠标消息
	if (MouseHit())
	{
		MOUSEMSG msg = GetMouseMsg();
		switch (msg.uMsg)
		{
		case WM_LBUTTONDOWN://左键按下
			person._x = msg.x-35;
			person._y = msg.y-62;
			break;

		}

	}
}



int main()
{
	init();
	draw();
	while (1)
	{
		Changedir();
		walk();
		draw();

	}

	getchar();
	closegraph();
	system("pause");
	return 0;
}


运行效果


  • 13
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
可以使用EasyX图形库来实现链表的可视化。下面是一个简单的示例代码: ```c #include <graphics.h> #include <stdio.h> #include <stdlib.h> #define NODE_SIZE 40 // 定义节点的大小 #define LINE_WIDTH 5 // 定义连线的宽度 #define SCREEN_WIDTH 800 // 定义屏幕宽度 #define SCREEN_HEIGHT 600 // 定义屏幕高度 typedef struct node { int data; // 数据域 struct node* next; // 指针域 } Node; // 创建节点 Node* CreateNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = NULL; return node; } // 插入节点 void InsertNode(Node* head, int pos, int data) { Node* pre = head; for (int i = 1; i < pos && pre != NULL; i++) { pre = pre->next; } if (pre == NULL) { return; } Node* node = CreateNode(data); node->next = pre->next; pre->next = node; } // 删除节点 void DeleteNode(Node* head, int pos) { Node* pre = head; for (int i = 1; i < pos && pre->next != NULL; i++) { pre = pre->next; } if (pre->next == NULL) { return; } Node* temp = pre->next; pre->next = temp->next; free(temp); } // 绘制链表 void DrawList(Node* head) { cleardevice(); // 清空画布 settextcolor(WHITE); setbkcolor(BLACK); int x = NODE_SIZE / 2, y = NODE_SIZE / 2; char str[10]; while (head != NULL) { sprintf(str, "%d", head->data); // 将整数转换为字符串 outtextxy(x - 10, y - 10, str); // 在画布上输出字符串 circle(x, y, NODE_SIZE / 2); // 绘制圆形 if (head->next != NULL) { line(x + NODE_SIZE / 2, y, x + NODE_SIZE * 3 / 2, y); // 绘制连线 } x += NODE_SIZE * 2; // 计算下一个节点的位置 if (x > SCREEN_WIDTH - NODE_SIZE / 2) { x = NODE_SIZE / 2; y += NODE_SIZE * 2; } head = head->next; } } int main() { Node* head = CreateNode(0); // 创建头节点 initgraph(SCREEN_WIDTH, SCREEN_HEIGHT); while (1) { DrawList(head); printf("请输入要进行的操作(1.插入 2.删除):"); int op; scanf("%d", &op); if (op == 1) { printf("请输入要插入的位置和数据,用空格隔开:"); int pos, data; scanf("%d %d", &pos, &data); InsertNode(head, pos, data); } else if (op == 2) { printf("请输入要删除的位置:"); int pos; scanf("%d", &pos); DeleteNode(head, pos); } } closegraph(); return 0; } ``` 这个程序使用了 `graphics.h` 和 `stdio.h` 头文件,其中 `graphics.h` 是 EasyX 图形库的头文件,`stdio.h` 是标准输入输出头文件。 程序定义了一个 `Node` 结构体,包含一个数据域和一个指针域,分别存储节点的数据和下一个节点的地址。定义了 `CreateNode` 函数用于创建节点,`InsertNode` 函数用于在指定位置插入节点,`DeleteNode` 函数用于删除指定位置的节点,`DrawList` 函数用于绘制链表。 在 `DrawList` 函数中,使用 `outtextxy` 函数在画布上输出每个节点的值,使用 `circle` 函数绘制圆形,使用 `line` 函数绘制连线。程序使用 `scanf` 函数从控制台读取用户输入的操作类型和参数,然后调用相应的函数进行操作,并在控制台输出操作结果。 程序使用了 `initgraph` 函数初始化 EasyX 图形库,使用 `closegraph` 函数关闭图形窗口。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值