easyx的字符介绍:


下面给出例子:
- #include<stdio.h>
- #include<graphics.h>
- //包含图形库头文件
- int main()
- {
- //创建一个窗口
- //(show console 显示控制台),640,480为窗口大小*
- initgraph(640,480,SHOWCONSOLE);
- //showconsole可以换成NOCLOSE:程序不能关闭
- // NOMINIMIZE不能最小化
- // 都想要用按位或|
- //设置背景颜色(两个步骤)
- setbkcolor(YELLOW);//步骤1
- //其中setbkcolor还可以用qq的截图按c键来识别色号来复制进去
- cleardevice();//清屏//步骤2
- //画一个圆
- setlinestyle(PS_SOLID, 5);//设置边缘线条颜色,5是线条粗细
- setfillcolor(GREEN);//设置填充颜色
- circle(50,50,50);//数字为坐标(x,y,r)
- fillcircle(50, 150, 50);
- solidcircle(50, 250, 50);
-
-
-
- //设置字体颜色
- settextcolor(GREEN);
- //绘制文字
- outtextxy(50, 50,L"easyx绘制文字");
- setbkmode(TRANSPARENT);//背景透明
- //此原函数只能输出单个字符,若想输出字符串,有以下几种方式
- //1、在字符串前加上大写的L
- //2、用TEXT()把字符串包起来如:,TEXT("")
- //3、进项目-->属性-->配置属性-->高级-->字符集-->改为多字节字符集
- getchar();
- closegraph();//关闭窗口
- return 0;
- }
结果如下:

文字居中的设置如下:
- #include<stdio.h>
- #include<graphics.h>
- int main()
- {
- initgraph(640, 480, SHOWCONSOLE);
- //先选定一个矩形
- //200,50;代表矩形左上角的坐标,500,100;代表右下角的坐标
- fillrectangle(200, 50, 500, 100);
- settextcolor(GREEN);
- char arr[] = "我是居中的字符";
- //textwidth:求出字符串的长度
- //textheight:求出字符串的高度
- int width = 300 / 2 - textwidth(arr) / 2;
- int height = 50 / 2 - textheight(arr) / 2;
- outtextxy(width + 200, height + 50, arr);
-
- getchar();
- closegraph();
- return 0;
- }
居中的原理如图:

结果如下:

easyx导入图片:
- #include<stdio.h>
- #include<graphics.h>
- int main()
- {
- initgraph(640, 480, SHOWCONSOLE);
- //输出图片
- IMAGE img;//定义一个变量对象
- //加载图片
- //相对路径:./表示当前文件夹下,../当前文件夹的上一级目录
- //绝对路径:C:\Users\fangzelin\source\repos\easyx\easyx\oo.jpg
- loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg",640,480);
- //后面的数字是图片的长度与宽度
- //注意\要用转义字符\\
- //若地址有空格要用/""
- putimage(0, 0, &img);
-
- getchar();
- closegraph;
- return 0;
-
- }
结果如下:

[旧版]通过鼠标来判定坐标的位置:
- #include<stdio.h>
- #include<graphics.h>
- int main()
- {
- initgraph(640, 480, SHOWCONSOLE);
- //先设置背景颜色,在加图片,不然清屏会清掉
- setbkcolor(WHITE);//背景颜色
- cleardevice();
- //图片
- IMAGE img;
- loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 200, 200);
- putimage(0, 0, &img);//图片输出位置
-
- while (1)//一直循环
- {
- if (MouseHit())
- {
- MOUSEMSG msg = GetMouseMsg();
- //消息分发
- switch (msg.uMsg)
- {
- case WM_LBUTTONDOWN://鼠标左键
- if (msg.x <200 && msg.y <200)
- {
- printf("感谢支持\n");
- }
- outtextxy(400, 400, "鼠标左键按下");//输入字符的位置
- printf("坐标(%d,%d)\n", msg.x, msg.y);
- break;
- case WM_RBUTTONDOWN://鼠标右键
- outtextxy(400, 400, "鼠标右键按下");//输入字符的位置
- printf("坐标(%d,%d)\n", msg.x, msg.y);
- break;
- }
- }
- }
-
- getchar();
- closegraph;
- return 0;
- }
结果如下:

[新版]鼠标指针
- #include<stdio.h>
- #include<easyx.h>//是easyx.h
-
- void button(int x, int y, int w, int h,const char* text)
- //x,y是初始坐标,w,h是宽度和高度
- {
- setbkmode(TRANSPARENT);//将文字周边的黑框变成与背景相同的色彩
- setfillcolor(BROWN);
- fillroundrect(x, y, x + w, y + h, 10, 10);
-
- //文字居中
- char text_[50] = "button";
- strcpy_s(text_, text);
- int tx = x + (w - textwidth(text)) / 2;
- int ty = y + (h - textheight(text)) / 2;
- outtextxy(tx, ty,text);
- }
-
-
-
- int main()
- {
- initgraph(640, 480, EX_SHOWCONSOLE);
- button(50, 50, 150, 50,"按钮");
-
- ExMessage msg;
- while (1)
- {
- if (peekmessage(&msg, EX_MOUSE));
- {
- switch (msg.message)
- {
- case WM_LBUTTONDOWN:
- if (msg.x >= 50 && msg.x <= 50 + 150 && msg.y >= 50 && msg.y <= 50 + 50)
- {
- printf("我被点击了");
-
- }
- break;
- }
- }
- }
-
- getchar();
- closegraph();
- return 0;
- }

键盘操作与物体移动

- #include<stdio.h>
- #include<graphics.h>
- #include<conio.h>//使用_getch()
- int main()
- {
- initgraph(640, 480, SHOWCONSOLE);
-
- int x=0, y = 0;
- while (1)
- {
- cleardevice();//清屏后不会有拖尾
-
- //双缓冲绘图,需要放在绘图代码前和后,这样图片就不会一直闪了
- BeginBatchDraw();//批量绘图
- IMAGE img;//将图片放在清屏后不会消失
- loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
- putimage(0, 0, &img);
-
- setfillcolor(BROWN);
- fillcircle(x,y,20);
- EndBatchDraw();//绘图结束
- //若用FlushBatchDraw代替,就不用beginbatchdraw
-
-
-
- char key = _getch();//键盘移动
- switch (key)
- {
- case 72://上键
- case 'w':
- case 'W':
- y -= 5;//向上y的坐标减少
- printf("上键\n");
- break;
- case 80://下键
- case 's':
- case 'S':
- y += 5;
- printf("下键\n");
- break;
- case 75://左键
- case 'a':
- case 'A':
- x -= 5;
- printf("左键\n");
- break;
- case 77://右键
- case 'd':
- case 'D':
- x += 5;
- printf("右键\n");
- break;
- }
-
- printf("%d,%c",key,key);
- }
-
-
-
- getchar();
- closegraph();
- return 0;
- }
这样移动无法做到斜着移动,使用getasynckeystate可以实现
- #include<stdio.h>
- #include<graphics.h>
- #include<conio.h>//使用_getch()
- int main()
- {
- initgraph(640, 480, SHOWCONSOLE);
-
- int x = 0, y = 0;
- while (1)
- {
- cleardevice();//清屏后不会有拖尾
-
- BeginBatchDraw();
- IMAGE img;//将图片放在清屏后不会消失
- loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
- putimage(0, 0, &img);
-
- setfillcolor(BROWN);
- fillcircle(x, y, 20);
- EndBatchDraw();
-
-
- if (GetAsyncKeyState(VK_UP))//上键
- y -= 5;
-
- if (GetAsyncKeyState(VK_DOWN))//下键
- y += 5;
-
- if (GetAsyncKeyState(VK_LEFT))//左键{
- x -= 5;
-
- if (GetAsyncKeyState(VK_RIGHT))//右键{
- x += 5;
- }
-
-
-
-
-
- getchar();
- closegraph();
- return 0;
- }
播放音乐

- #include<stdio.h>
- #include<graphics.h>
- #include<mmsystem.h>//包含多媒体设备接口头文件
- #pragma comment(lib,"winmm.lib")//加载静态库
-
- void BGM()
- {
- //打开音乐,播放 alias取别名 repeat 重复播放
- mciSendString("open ./thedown.mp3 alias bgm", 0, 0, 0);
- mciSendString("play bgm repeat", 0, 0, 0);
- if (0)//关闭音乐
- {
- mciSendString("close bgm ", 0, 0, 0);
- }
- }
- int main()
- {
- initgraph(640, 480, SHOWCONSOLE);
- BGM();
- int x = 0, y = 0;
- while (1)
- {
- cleardevice();//清屏后不会有拖尾
-
- BeginBatchDraw();
- IMAGE img;//将图片放在清屏后不会消失
- loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
- putimage(0, 0, &img);
-
- setfillcolor(BROWN);
- fillcircle(x, y, 20);
- EndBatchDraw();
-
-
- if (GetAsyncKeyState(VK_UP))//上键
- y -= 5;
-
- if (GetAsyncKeyState(VK_DOWN))//下键
- y += 5;
-
- if (GetAsyncKeyState(VK_LEFT))//左键{
- x -= 5;
-
- if (GetAsyncKeyState(VK_RIGHT))//右键{
- x += 5;
- }
-
-
-
-
-
- getchar();
- closegraph();
- return 0;
- }
修改窗口标题,弹出对话框:

- #include<stdio.h>
- #include<graphics.h>//嵌入了windows.h
- #include<mmsystem.h>//包含多媒体设备接口头文件
- #pragma comment(lib,"winmm.lib")//加载静态库
-
- void BGM()
- {
- //打开音乐,播放 alias取别名 repeat 重复播放
- mciSendString("open ./thedown.mp3 alias bgm", 0, 0, 0);
- mciSendString("play bgm repeat", 0, 0, 0);
- if (0)//关闭音乐
- {
- mciSendString("close bgm ", 0, 0, 0);
- }
- }
- void change()
- {
- //获取窗口句柄 hnd是随便定义的变量
- HWND hnd = GetHWnd();
- //设置窗口标题
- SetWindowText(hnd, "游戏");
- //弹出窗口,提示用户操作
- //如果不想要黑框框,就不要int isok
- int isok=MessageBox(hnd, "充钱给你再玩一次", "提示", MB_OKCANCEL);
- if (isok == IDOK)
- {
- printf("恭喜!");
- }
- else if (IDCANCEL == isok)
- {
- printf("无语!");
- }
- }
- int main()
- {
- initgraph(640, 480, SHOWCONSOLE);
- //BGM();
- change();
- int x = 0, y = 0;
- while (1)
- {
- cleardevice();//清屏后不会有拖尾
-
- BeginBatchDraw();
- IMAGE img;//将图片放在清屏后不会消失
- loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
- putimage(0, 0, &img);
-
- setfillcolor(BROWN);
- fillcircle(x, y, 20);
- EndBatchDraw();
-
-
- if (GetAsyncKeyState(VK_UP))//上键
- y -= 5;
-
- if (GetAsyncKeyState(VK_DOWN))//下键
- y += 5;
-
- if (GetAsyncKeyState(VK_LEFT))//左键{
- x -= 5;
-
- if (GetAsyncKeyState(VK_RIGHT))//右键{
- x += 5;
- }
-
-
-
-
-
- getchar();
- closegraph();
- return 0;
- }